赞
踩
注释:作者基于下图的环境搭建没什么问题 ,还需要modelscope>=1.9.1 ,gradio直接安装最新的
- import torch
- import gradio as gr
- import torch.nn as nn
- from modelscope import snapshot_download, Model
-
- model_dir = snapshot_download(
- "baichuan-inc/Baichuan2-13B-Chat", revision='v1.0.1')
-
-
- model = Model.from_pretrained(
- model_dir, device_map="auto", torch_dtype=torch.float16, trust_remote_code=True)
-
-
- def clear_session():
- return []
-
-
- def predict(input, history):
- if history is None:
- history = []
- model_input = []
- for chat in history:
- model_input.append({"role": "user", "content": chat[0]})
- model_input.append({"role": "assistant", "content": chat[1]})
- model_input.append({"role": "user", "content": input})
- print(model_input)
- response = model(model_input)["response"]
- history.append((input, response))
- history = history[-20:]
- return '', history
-
-
- block = gr.Blocks()
-
- with block as demo:
- gr.Markdown("""<h1><center>Baichuan2-13B-Chat</center></h1>
- <center>Baichuan2-13B-Chat为Baichuan2-13B系列模型中对齐后的版本,预训练模型可见Baichuan2-13B-Base</center>
- """)
- chatbot = gr.Chatbot(label='Baichuan2-13B-Chat')
- message = gr.Textbox()
- message.submit(predict,
- inputs=[message, chatbot],
- outputs=[message, chatbot])
- with gr.Row():
- clear_history = gr.Button("声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/189634推荐阅读
相关标签
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。