for i, (query, response) in enumerate(history): message(query, avatar_style="retro", key=str(i) + "_user") message(response, avatar_style="identicon", key=str(i)) 我想这里呈现历史信息时,用一些简单的符号,帮我设计一下
时间: 2024-04-10 08:33:49 浏览: 23
当呈现历史信息时,你可以使用一些简单的符号来区分用户和机器人的对话。下面是一个示例:
```python
for i, (query, response) in enumerate(history):
message(query, avatar_style="retro", key=str(i) + "_user")
message(response, avatar_style="identicon", key=str(i))
st.write("---") # 添加分隔线
```
在这个示例中,我添加了一个分隔线来区分每个对话的历史记录。你也可以使用其他符号,例如"==="或者"***"等等,根据你的喜好来设计。
希望这个设计可以满足你的需求!如有其他问题,请随时提问。
相关问题
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"])逐句解析代码
这段代码是一个基于 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 中。
将下面的缺失值填充处理代码优化并写出代码过程 update_value = [] for key in ['pm10', '温度', '湿度', '风速', '风向']: # 插值填充缺失数据 col = e[key].copy() bool_na = col.isna().copy() for (idx, na) in enumerate(bool_na.to_list()): if na: pre_value = post_value = None pre_index = post_index = idx while pre_value is None and idx > 0: pre_index -= 1 if pre_index == 0: break if not bool_na.iloc[pre_index]: pre_value = col.iloc[pre_index] while post_value is None and idx < (len(col) - 1): post_index += 1 if post_index == len(col): break if not bool_na.iloc[post_index]: post_value = col.iloc[post_index] if pre_value is not None and post_value is not None: col.iloc[idx] = (pre_value + post_value) / 2 bool_na.iloc[idx] = False update_value.append((idx, key, col.iloc[idx])) for (idx, key, v) in update_value: e.loc[idx, key] = v
这段代码可以进行如下优化:
1. 使用 pandas 内置的插值函数进行填充,可以使用 interpolate() 函数或 bfill() 和 ffill() 函数进行填充,可以根据数据的特性选择不同的插值方法。
2. 可以使用 apply() 函数对列进行操作,避免使用循环。
下面是代码优化后的过程:
1. 导入 pandas 库并读取数据
```python
import pandas as pd
e = pd.read_csv('data.csv')
```
2. 使用 interpolate() 函数进行填充
```python
e.interpolate(inplace=True)
```
3. 使用 apply() 函数对列进行操作
```python
update_value = []
def fill_na(col):
bool_na = col.isna().copy()
for (idx, na) in enumerate(bool_na.to_list()):
if na:
pre_value = post_value = None
pre_index = post_index = idx
while pre_value is None and idx > 0:
pre_index -= 1
if pre_index == 0:
break
if not bool_na.iloc[pre_index]:
pre_value = col.iloc[pre_index]
while post_value is None and idx < (len(col) - 1):
post_index += 1
if post_index == len(col):
break
if not bool_na.iloc[post_index]:
post_value = col.iloc[post_index]
if pre_value is not None and post_value is not None:
col.iloc[idx] = (pre_value + post_value) / 2
bool_na.iloc[idx] = False
update_value.append((idx, col.name, col.iloc[idx]))
for key in ['pm10', '温度', '湿度', '风速', '风向']:
e[key].apply(fill_na)
for (idx, key, v) in update_value:
e.loc[idx, key] = v
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)