Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
This script is a simple web demo based on Streamlit, showcasing the use of the ChatGLM3-6B model. For a more comprehensive web demo,
|
3 |
+
it is recommended to use 'composite_demo'.
|
4 |
+
|
5 |
+
Usage:
|
6 |
+
- Run the script using Streamlit: `streamlit run web_demo_streamlit.py`
|
7 |
+
- Adjust the model parameters from the sidebar.
|
8 |
+
- Enter questions in the chat input box and interact with the ChatGLM3-6B model.
|
9 |
+
|
10 |
+
Note: Ensure 'streamlit' and 'transformers' libraries are installed and the required model checkpoints are available.
|
11 |
+
"""
|
12 |
+
|
13 |
+
import os
|
14 |
+
import streamlit as st
|
15 |
+
import torch
|
16 |
+
from transformers import AutoModel, AutoTokenizer
|
17 |
+
|
18 |
+
MODEL_PATH = os.environ.get('MODEL_PATH', 'THUDM/chatglm3-6b')
|
19 |
+
TOKENIZER_PATH = os.environ.get("TOKENIZER_PATH", MODEL_PATH)
|
20 |
+
|
21 |
+
st.set_page_config(
|
22 |
+
page_title="ChatGLM3-6B Streamlit Simple Demo",
|
23 |
+
page_icon=":robot:",
|
24 |
+
layout="wide"
|
25 |
+
)
|
26 |
+
|
27 |
+
|
28 |
+
@st.cache_resource
|
29 |
+
def get_model():
|
30 |
+
|
31 |
+
tokenizer = AutoTokenizer.from_pretrained(TOKENIZER_PATH, trust_remote_code=True)
|
32 |
+
model = AutoModel.from_pretrained(MODEL_PATH, trust_remote_code=True, device_map="auto").eval()
|
33 |
+
return tokenizer, model
|
34 |
+
|
35 |
+
|
36 |
+
# 加载Chatglm3的model和tokenizer
|
37 |
+
tokenizer, model = get_model()
|
38 |
+
|
39 |
+
if "history" not in st.session_state:
|
40 |
+
st.session_state.history = []
|
41 |
+
if "past_key_values" not in st.session_state:
|
42 |
+
st.session_state.past_key_values = None
|
43 |
+
|
44 |
+
max_length = st.sidebar.slider("max_length", 0, 32768, 8192, step=1)
|
45 |
+
top_p = st.sidebar.slider("top_p", 0.0, 1.0, 0.8, step=0.01)
|
46 |
+
temperature = st.sidebar.slider("temperature", 0.0, 1.0, 0.6, step=0.01)
|
47 |
+
|
48 |
+
buttonClean = st.sidebar.button("清理会话历史", key="clean")
|
49 |
+
if buttonClean:
|
50 |
+
st.session_state.history = []
|
51 |
+
st.session_state.past_key_values = None
|
52 |
+
if torch.cuda.is_available():
|
53 |
+
torch.cuda.empty_cache()
|
54 |
+
st.rerun()
|
55 |
+
|
56 |
+
for i, message in enumerate(st.session_state.history):
|
57 |
+
if message["role"] == "user":
|
58 |
+
with st.chat_message(name="user", avatar="user"):
|
59 |
+
st.markdown(message["content"])
|
60 |
+
else:
|
61 |
+
with st.chat_message(name="assistant", avatar="assistant"):
|
62 |
+
st.markdown(message["content"])
|
63 |
+
|
64 |
+
with st.chat_message(name="user", avatar="user"):
|
65 |
+
input_placeholder = st.empty()
|
66 |
+
with st.chat_message(name="assistant", avatar="assistant"):
|
67 |
+
message_placeholder = st.empty()
|
68 |
+
|
69 |
+
prompt_text = st.chat_input("请输入您的问题")
|
70 |
+
if prompt_text:
|
71 |
+
input_placeholder.markdown(prompt_text)
|
72 |
+
history = st.session_state.history
|
73 |
+
past_key_values = st.session_state.past_key_values
|
74 |
+
for response, history, past_key_values in model.stream_chat(
|
75 |
+
tokenizer,
|
76 |
+
prompt_text,
|
77 |
+
history,
|
78 |
+
past_key_values=past_key_values,
|
79 |
+
max_length=max_length,
|
80 |
+
top_p=top_p,
|
81 |
+
temperature=temperature,
|
82 |
+
return_past_key_values=True,
|
83 |
+
):
|
84 |
+
message_placeholder.markdown(response)
|
85 |
+
st.session_state.history = history
|
86 |
+
st.session_state.past_key_values = past_key_values
|