5 Star 42 Fork 10

Gitee 极速下载 / ChatGLM-6B

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/THUDM/ChatGLM-6B
克隆/下载
cli_demo_vision.py 1.94 KB
一键复制 编辑 原始数据 按行查看 历史
duzx16 提交于 2023-05-18 00:47 . Update cli demo
import os
import platform
import signal
import sys
from transformers import AutoTokenizer, AutoModel
import readline
tokenizer = AutoTokenizer.from_pretrained("THUDM/visualglm-6b", trust_remote_code=True)
model = AutoModel.from_pretrained("THUDM/visualglm-6b", trust_remote_code=True).half().cuda()
model = model.eval()
os_name = platform.system()
clear_command = 'cls' if os_name == 'Windows' else 'clear'
stop_stream = False
def build_prompt(history, prefix):
prompt = prefix
for query, response in history:
prompt += f"\n\n用户:{query}"
prompt += f"\n\nChatGLM-6B:{response}"
return prompt
def signal_handler(signal, frame):
global stop_stream
stop_stream = True
def main():
global stop_stream
while True:
history = []
prefix = "欢迎使用 VisualGLM-6B 模型,输入图片路径和内容即可进行对话,clear 清空对话历史,stop 终止程序"
print(prefix)
image_path = input("\n请输入图片路径:")
if image_path == "stop":
break
prefix = prefix + "\n" + image_path
query = "描述这张图片。"
while True:
count = 0
for response, history in model.stream_chat(tokenizer, image_path, query, history=history):
if stop_stream:
stop_stream = False
break
else:
count += 1
if count % 8 == 0:
os.system(clear_command)
print(build_prompt(history, prefix), flush=True)
signal.signal(signal.SIGINT, signal_handler)
os.system(clear_command)
print(build_prompt(history, prefix), flush=True)
query = input("\n用户:")
if query.strip() == "clear":
break
if query.strip() == "stop":
sys.exit(0)
if __name__ == "__main__":
main()
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mirrors/ChatGLM-6B.git
git@gitee.com:mirrors/ChatGLM-6B.git
mirrors
ChatGLM-6B
ChatGLM-6B
main

搜索帮助

344bd9b3 5694891 D2dac590 5694891