当前位置: 首页> 文旅> 美景 > Gradio导入AIGC大模型创建web端智能体聊天机器人,python(2)

Gradio导入AIGC大模型创建web端智能体聊天机器人,python(2)

时间:2025/7/12 5:32:11来源:https://blog.csdn.net/zhangphil/article/details/142031038 浏览次数:0次

Gradio导入AIGC大模型创建web端智能体聊天机器人,python(2)

选用这个大模型:

https://huggingface.co/HuggingFaceTB/SmolLM-1.7B-Instructicon-default.png?t=O83Ahttps://huggingface.co/HuggingFaceTB/SmolLM-1.7B-Instruct原因是该模型相对比较小(3~4GB),不必下载太多太大的模型文件(效果好些的大模型动辄几十GB甚至上百GB,参数多嘛),仅作跑通Gradio结合大模型制作聊天机器人示例。

import gradio as gr
from transformers import AutoModelForCausalLM, AutoTokenizercheckpoint = "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 resultdef 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

关键字:Gradio导入AIGC大模型创建web端智能体聊天机器人,python(2)

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: