Not-Grim-Refer
commited on
Commit
•
bb97cbe
1
Parent(s):
81bc518
Update app.py
Browse files
app.py
CHANGED
@@ -1,74 +1,54 @@
|
|
1 |
import streamlit as st
|
2 |
-
from
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
st.
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
""
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
""
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
# Get the user's question from the input text box
|
56 |
-
user_question = st.text_input("What do you want to ask about", placeholder="Input your question here")
|
57 |
-
|
58 |
-
if user_question:
|
59 |
-
# Add the user's question to the queue
|
60 |
-
queue.put(user_question)
|
61 |
-
|
62 |
-
# Check if there are any waiting users
|
63 |
-
if not queue.empty():
|
64 |
-
# Get the next user's question from the queue
|
65 |
-
query = queue.get()
|
66 |
-
|
67 |
-
# Generate a response to the user's question
|
68 |
-
response = chat(query)
|
69 |
-
|
70 |
-
# Display the response to the user
|
71 |
-
st.write(response, unsafe_allow_html=True)
|
72 |
-
|
73 |
-
if __name__ == '__main__':
|
74 |
-
main()
|
|
|
1 |
import streamlit as st
|
2 |
+
from transformers import AutoModel, AutoTokenizer
|
3 |
+
import mdtex2html
|
4 |
+
from utils import load_model_on_gpus
|
5 |
+
|
6 |
+
st.set_page_config(page_title="ChatGLM2-6B", page_icon=":robot:")
|
7 |
+
|
8 |
+
st.header("ChatGLM2-6B")
|
9 |
+
|
10 |
+
tokenizer = AutoTokenizer.from_pretrained("THUDM/chatglm2-6b", trust_remote_code=True)
|
11 |
+
model = AutoModel.from_pretrained("THUDM/chatglm2-6b", trust_remote_code=True).cuda()
|
12 |
+
# Load model on multiple GPUs
|
13 |
+
#model = load_model_on_gpus("THUDM/chatglm2-6b", num_gpus=2)
|
14 |
+
model = model.eval()
|
15 |
+
|
16 |
+
def postprocess(chat):
|
17 |
+
for i, (user, response) in enumerate(chat):
|
18 |
+
chat[i] = (mdtex2html.convert(user), mdtex2html.convert(response))
|
19 |
+
return chat
|
20 |
+
|
21 |
+
user_input = st.text_area("Input:", height=200, placeholder="Ask me anything!")
|
22 |
+
if user_input:
|
23 |
+
history = st.session_state.get('history', [])
|
24 |
+
|
25 |
+
max_length = st.slider("Max Length:", 0, 32768, 8192, 1)
|
26 |
+
top_p = st.slider("Top P:", 0.0, 1.0, 0.8, 0.01)
|
27 |
+
temperature = st.slider("Temperature:", 0.0, 1.0, 0.95, 0.01)
|
28 |
+
|
29 |
+
if 'past_key_values' not in st.session_state:
|
30 |
+
st.session_state['past_key_values'] = None
|
31 |
+
|
32 |
+
with st.spinner("Thinking..."):
|
33 |
+
response = model.generate(tokenizer.encode(user_input),
|
34 |
+
max_length=max_length,
|
35 |
+
top_p=top_p,
|
36 |
+
temperature=temperature,
|
37 |
+
return_dict_in_generate=True,
|
38 |
+
output_scores=True,
|
39 |
+
return_past_key_values=True,
|
40 |
+
past_key_values=st.session_state.past_key_values)
|
41 |
+
|
42 |
+
st.session_state.past_key_values = response.past_key_values
|
43 |
+
|
44 |
+
history.append((user_input, response.sequences[0]))
|
45 |
+
history = postprocess(history)
|
46 |
+
|
47 |
+
for user, chatbot in history:
|
48 |
+
message = f"**Human:** {user}" if user else ""
|
49 |
+
response = f"**AI:** {chatbot}" if chatbot else ""
|
50 |
+
st.markdown(message + response, unsafe_allow_html=True)
|
51 |
+
|
52 |
+
if st.button("Clear History"):
|
53 |
+
st.session_state['history'] = []
|
54 |
+
st.session_state['past_key_values'] = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|