from transformers import AutoModel, AutoTokenizer import streamlit as st from streamlit_chat import message st.set_page_config( page_title="ChatGLM-6b 演示", page_icon=":robot:" ) @st.cache_resource def get_model(): tokenizer = AutoTokenizer.from_pretrained("THUDM/chatglm-6b", trust_remote_code=True) model = AutoModel.from_pretrained("THUDM/chatglm-6b", trust_remote_code=True).half().cuda() model = model.eval() return tokenizer, model MAX_TURNS = 20 MAX_BOXES = MAX_TURNS * 2 def predict(input, max_length, top_p, temperature, history=None): tokenizer, model = get_model() if history is None: history = [] with container: if len(history) > 0: if len(history)>MAX_BOXES: history = history[-MAX_TURNS:] for i, (query, response) in enumerate(history): message(query, avatar_style="big-smile", key=str(i) + "_user") message(response, avatar_style="bottts", key=str(i)) message(input, avatar_style="big-smile", key=str(len(history)) + "_user") st.write("AI正在回复:") with st.empty(): for response, history in model.stream_chat(tokenizer, input, history, max_length=max_length, top_p=top_p, temperature=temperature): query, response = history[-1] st.write(response) return history container = st.container() # create a prompt text for the text generation prompt_text = st.text_area(label="用户命令输入", height = 100, placeholder="请在这儿输入您的命令") max_length = st.sidebar.slider( 'max_length', 0, 4096, 2048, step=1 ) top_p = st.sidebar.slider( 'top_p', 0.0, 1.0, 0.6, step=0.01 ) temperature = st.sidebar.slider( 'temperature', 0.0, 1.0, 0.95, step=0.01 ) if 'state' not in st.session_state: st.session_state['state'] = [] if st.button("发送", key="predict"): with st.spinner("AI正在思考,请稍等........"): # text generation st.session_state["state"] = predict(prompt_text, max_length, top_p, temperature, st.session_state["state"])逐句解析代码
时间: 2023-06-16 18:04:37 浏览: 518
这段代码是一个基于 ChatGLM-6B 模型的 AI 对话系统的 Streamlit 应用程序。它从 transformers 库中导入了 AutoModel 和 AutoTokenizer,以及 Streamlit 库中的 message 函数。然后,它定义了一个用于获取模型和分词器的函数 get_model,并使用 st.cache_resource 装饰器将其缓存,以避免重复加载模型和分词器。接下来,它定义了 MAX_TURNS 和 MAX_BOXES 常量,以及一个 predict 函数,它接受输入、max_length、top_p、temperature 和 history 参数,并返回一个历史记录。predict 函数使用 get_model 函数获取模型和分词器,然后使用 model.stream_chat 方法对输入进行处理,并将输出逐步显示在 Streamlit 应用程序的界面上。最后,它定义了一个包含一个文本区域和一个发送按钮的用户界面,用户可以在文本区域中输入命令,并按下发送按钮将命令发送给 AI。当用户按下发送按钮时,程序将调用 predict 函数来生成 AI 的回复,并将历史记录保存在 st.session_state 中。
相关问题
from transformers import AutoTokenizer, AutoModel报错DLL load failed while importing _sentencepiece
这个错误通常是由于缺少 sentencepiece 库或版本不兼容导致的。您可以尝试使用 pip install sentencepiece 命令安装最新版本的 sentencepiece 库,如果已经安装了,可以尝试更新它。如果问题仍然存在,可以考虑卸载并重新安装 sentencepiece 库。如果您使用的是 Windows 操作系统,还可以尝试从 sentencepiece 的 GitHub 页面下载预编译版本进行安装。如果还有问题,请提供更多的细节信息,以便我们更好地帮助您解决问题。
from transformers import AutoTokenizer, AutoModel失败
请问您遇到了什么错误信息?可能需要检查您的环境是否已经正确安装了 Transformers 库,并且您的模型名称是否正确。另外,建议您先尝试使用 pip install transformers 命令安装最新版本 Transformers 库。如果问题仍然存在,可以提供更多的错误信息帮助我们解决问题。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)