Gradio导入AIGC大模型创建web端智能体聊天机器人,python(2)
Gradio导入AIGC大模型创建web端智能体聊天机器人,python(2)
选用这个大模型:
https://huggingface.co/HuggingFaceTB/SmolLM-1.7B-Instructhttps://huggingface.co/HuggingFaceTB/SmolLM-1.7B-Instruct原因是该模型相对比较小(3~4GB),不必下载太多太大的模型文件(效果好些的大模型动辄几十GB甚至上百GB,参数多嘛),仅作跑通Gradio结合大模型制作聊天机器人示例。
import gradio as gr
from transformers import AutoModelForCausalLM, AutoTokenizer
checkpoint = "HuggingFaceTB/SmolLM-1.7B-Instruct"
device = "cpu" # "cpu" for CPU usage, "gpu" for GPU usage
tokenizer = AutoTokenizer.from_pretrained(checkpoint)
model = AutoModelForCausalLM.from_pretrained(checkpoint).to(device)
def my_response(message, history):
msgs = [{"role": "user", "content": message}]
input_text = tokenizer.apply_chat_template(msgs, tokenize=False)
print(input_text)
inputs = tokenizer.encode(input_text, return_tensors="pt").to(device)
outputs = model.generate(inputs, max_new_tokens=50, temperature=0.2, top_p=0.9, do_sample=True)
result = tokenizer.decode(outputs[0])
print(result)
return result
def mychat():
gr.ChatInterface(my_response).launch()
if __name__ == '__main__':
mychat()
运行后,输出: Running on local URL: http://127.0.0.1:7860
直接打开 http://127.0.0.1:7860
提问:
AIGC大模型回答:
Gradio快速部署构建AIGC的web应用 ,python-CSDN博客文章浏览阅读873次,点赞23次,收藏9次。webui-user.bat启动stable-diffusion-webui报错:RuntimeError: Torch is not able to use GPU,AIGC,Python。webui-user.bat启动stable-diffusion-webui报错:RuntimeError: Torch is not able to use GPU,AIGC,Python-CSDN博客。2、设置 - 系统 - 可选功能 - 更多Windows功能 - 启用或关闭Windows功能。https://blog.csdn.net/zhangphil/article/details/141999273