Spaces:
Runtime error
Runtime error
update theme and predict fun
Browse files
app.py
CHANGED
@@ -15,13 +15,6 @@ from transformers import M2M100ForConditionalGeneration, M2M100Tokenizer
|
|
15 |
model_chtoen = M2M100ForConditionalGeneration.from_pretrained("facebook/m2m100_418M")
|
16 |
tokenizer_chtoen = M2M100Tokenizer.from_pretrained("facebook/m2m100_418M")
|
17 |
|
18 |
-
# Define function to generate model predictions and update the history
|
19 |
-
def predict_glm(input, history=[]):
|
20 |
-
response, history = model_glm.chat(tokenizer_glm, input, history)
|
21 |
-
# translate Chinese to English
|
22 |
-
history = [(query, translate_Chinese_English(response)) for query, response in history]
|
23 |
-
return history, history #[history] + updates
|
24 |
-
|
25 |
def translate_Chinese_English(chinese_text):
|
26 |
# translate Chinese to English
|
27 |
tokenizer_chtoen.src_lang = "zh"
|
@@ -30,45 +23,21 @@ def translate_Chinese_English(chinese_text):
|
|
30 |
trans_eng_text = tokenizer_chtoen.batch_decode(generated_tokens, skip_special_tokens=True)
|
31 |
return trans_eng_text[0]
|
32 |
|
33 |
-
# Define generator to stream model predictions
|
34 |
-
def predict_glm_stream_old(input, history=[]): #, top_p, temperature):
|
35 |
-
top_p = 1.0
|
36 |
-
temperature = 1.0
|
37 |
-
for response, history in model_glm.stream_chat(tokenizer_glm, input, history, top_p=1.0, temperature=1.0): #max_length=max_length,
|
38 |
-
print(f"In for loop resonse is ^^- {response}")
|
39 |
-
print(f"In for loop history is ^^- {history}")
|
40 |
-
# translate Chinese to English
|
41 |
-
history = [(query, translate_Chinese_English(response)) for query, response in history]
|
42 |
-
print(f"In for loop translated history is ^^- {history}")
|
43 |
-
yield history, history #[history] + updates
|
44 |
|
45 |
# Define function to generate model predictions and update the history
|
46 |
-
def predict_glm_stream(input, history=[]): #, top_p, temperature):
|
47 |
-
|
|
|
|
|
|
|
48 |
print(f"In for loop resonse is ^^- {response}")
|
49 |
print(f"In for loop updates is ^^- {updates}")
|
50 |
# translate Chinese to English
|
51 |
#history = [(query, translate_Chinese_English(response)) for query, response in history]
|
52 |
-
print(f"
|
53 |
-
|
54 |
-
yield history+updates
|
55 |
-
|
56 |
-
|
57 |
-
"""
|
58 |
-
def predict(input, max_length, top_p, temperature, history=None):
|
59 |
-
if history is None:
|
60 |
-
history = []
|
61 |
-
for response, history in model.stream_chat(tokenizer, input, history, max_length=max_length, top_p=top_p,
|
62 |
-
temperature=temperature):
|
63 |
-
updates = []
|
64 |
-
for query, response in history:
|
65 |
-
updates.append(gr.update(visible=True, value="user:" + query)) #用户
|
66 |
-
updates.append(gr.update(visible=True, value="ChatGLM-6B:" + response))
|
67 |
-
if len(updates) < MAX_BOXES:
|
68 |
-
updates = updates + [gr.Textbox.update(visible=False)] * (MAX_BOXES - len(updates))
|
69 |
-
yield [history] + updates
|
70 |
-
"""
|
71 |
|
|
|
72 |
def reset_textbox():
|
73 |
return gr.update(value="")
|
74 |
|
@@ -79,23 +48,24 @@ def reset_chat(chatbot, state):
|
|
79 |
return None, []
|
80 |
|
81 |
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
Assistant: <utterance>
|
90 |
-
...
|
91 |
-
```
|
92 |
-
In this app, you can explore the outputs of multiple LLMs when prompted in similar ways.
|
93 |
"""
|
94 |
|
|
|
|
|
|
|
|
|
|
|
95 |
with gr.Blocks(css="""#col_container {margin-left: auto; margin-right: auto;}
|
96 |
-
#chatglm {height: 520px; overflow: auto;} """ ) as demo:
|
97 |
gr.HTML(title)
|
98 |
-
|
99 |
with gr.Column(): #(scale=10):
|
100 |
with gr.Box():
|
101 |
with gr.Row():
|
@@ -110,27 +80,24 @@ with gr.Blocks(css="""#col_container {margin-left: auto; margin-right: auto;}
|
|
110 |
with gr.Box():
|
111 |
chatbot_glm = gr.Chatbot(elem_id="chatglm", label='THUDM-ChatGLM6B')
|
112 |
|
113 |
-
#with gr.Column(): #(scale=2, elem_id='parameters'):
|
114 |
with gr.Box():
|
115 |
gr.HTML("Parameters for ChatGLM-6B", visible=True)
|
116 |
-
top_p = gr.Slider(minimum=-0, maximum=1.0,value=
|
117 |
-
temperature = gr.Slider(minimum=-0, maximum=5.0, value=
|
118 |
-
#top_k = gr.Slider( minimum=1, maximum=50, value=50, step=1, interactive=True, label="Top-k", visible=False)
|
119 |
-
#repetition_penalty = gr.Slider( minimum=0.1, maximum=3.0, value=1.01, step=0.01, interactive=True, label="Repetition Penalty", visible=False)
|
120 |
|
|
|
|
|
|
|
121 |
inputs.submit(reset_textbox, [], [inputs])
|
122 |
-
|
123 |
|
124 |
-
inputs.submit( predict_glm_stream,
|
125 |
-
[inputs, chatbot_glm, ], #[inputs, state_glm, ],
|
126 |
-
[chatbot_glm],) #[chatbot_glm, state_glm],)
|
127 |
b1.click( predict_glm_stream,
|
128 |
-
[inputs,
|
129 |
-
[chatbot_glm],)
|
|
|
130 |
|
131 |
-
#b2.click(reset_chat, [
|
132 |
-
b2.click(
|
133 |
|
134 |
-
gr.HTML('''<center><a href="https://huggingface.co/spaces/ysharma/
|
135 |
gr.Markdown(description)
|
136 |
demo.queue(concurrency_count=16).launch(height= 800, debug=True)
|
|
|
15 |
model_chtoen = M2M100ForConditionalGeneration.from_pretrained("facebook/m2m100_418M")
|
16 |
tokenizer_chtoen = M2M100Tokenizer.from_pretrained("facebook/m2m100_418M")
|
17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
def translate_Chinese_English(chinese_text):
|
19 |
# translate Chinese to English
|
20 |
tokenizer_chtoen.src_lang = "zh"
|
|
|
23 |
trans_eng_text = tokenizer_chtoen.batch_decode(generated_tokens, skip_special_tokens=True)
|
24 |
return trans_eng_text[0]
|
25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
# Define function to generate model predictions and update the history
|
28 |
+
def predict_glm_stream(input, top_p, temperature, history=[]): #, top_p, temperature):
|
29 |
+
print(f"1 outside for loop OG history is ^^- {history}")
|
30 |
+
history = list(map(tuple, history))
|
31 |
+
print(f"2 outside for loop OG history is ^^- {history}")
|
32 |
+
for response, updates in model_glm.stream_chat(tokenizer_glm, input, history, top_p=top_p, temperature=temperature): #history
|
33 |
print(f"In for loop resonse is ^^- {response}")
|
34 |
print(f"In for loop updates is ^^- {updates}")
|
35 |
# translate Chinese to English
|
36 |
#history = [(query, translate_Chinese_English(response)) for query, response in history]
|
37 |
+
print(f"*******")
|
38 |
+
yield updates #history+updates
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
+
|
41 |
def reset_textbox():
|
42 |
return gr.update(value="")
|
43 |
|
|
|
48 |
return None, []
|
49 |
|
50 |
|
51 |
+
title = """<h1 align="center"> 🚀CHatGLM-6B - A Streaming Chatbot with Gradio</h1>
|
52 |
+
<h2 align="center">Enhance User Experience with Streaming and customizable Gradio Themes</h2>"""
|
53 |
+
header = """<center>Find on Huggingface at <a href="https://huggingface.co/THUDM/chatglm-6b" target="_blank">THUDM/chatglm-6b</a>, and <a href="https://github.com/THUDM/ChatGLM-6B" target="_blank">here</a> on Github.<center>"""
|
54 |
+
description = """
|
55 |
+
ChatGLM-6B is an open-source, Chinese-English bilingual dialogue language model based on the General Language Model (GLM) architecture with 6.2 billion parameters.
|
56 |
+
|
57 |
+
However, due to the small size of ChatGLM-6B, it is currently known to have considerable limitations, such as factual/mathematical logic errors, possible generation of harmful/biased content, weak contextual ability, self-awareness confusion, and Generate content that completely contradicts Chinese instructions for English instructions. Please understand these issues before use to avoid misunderstandings. A larger ChatGLM based on the 130 billion parameter GLM-130B is under development in internal testing.
|
|
|
|
|
|
|
|
|
58 |
"""
|
59 |
|
60 |
+
theme = gr.themes.Default(#color contructors
|
61 |
+
primary_hue="violet",
|
62 |
+
secondary_hue="indigo",
|
63 |
+
neutral_hue="purple")
|
64 |
+
|
65 |
with gr.Blocks(css="""#col_container {margin-left: auto; margin-right: auto;}
|
66 |
+
#chatglm {height: 520px; overflow: auto;} """, theme=theme ) as demo:
|
67 |
gr.HTML(title)
|
68 |
+
gr.HTML(header)
|
69 |
with gr.Column(): #(scale=10):
|
70 |
with gr.Box():
|
71 |
with gr.Row():
|
|
|
80 |
with gr.Box():
|
81 |
chatbot_glm = gr.Chatbot(elem_id="chatglm", label='THUDM-ChatGLM6B')
|
82 |
|
|
|
83 |
with gr.Box():
|
84 |
gr.HTML("Parameters for ChatGLM-6B", visible=True)
|
85 |
+
top_p = gr.Slider(minimum=-0, maximum=1.0,value=1, step=0.05,interactive=True, label="Top-p", visible=True)
|
86 |
+
temperature = gr.Slider(minimum=-0, maximum=5.0, value=1, step=0.1, interactive=True, label="Temperature", visible=True)
|
|
|
|
|
87 |
|
88 |
+
inputs.submit( predict_glm_stream,
|
89 |
+
[inputs, top_p, temperature, chatbot_glm ],
|
90 |
+
[chatbot_glm],)
|
91 |
inputs.submit(reset_textbox, [], [inputs])
|
|
|
92 |
|
|
|
|
|
|
|
93 |
b1.click( predict_glm_stream,
|
94 |
+
[inputs, top_p, temperature, chatbot_glm ],
|
95 |
+
[chatbot_glm],)
|
96 |
+
b1.click(reset_textbox, [], [inputs])
|
97 |
|
98 |
+
#b2.click(reset_chat, [chatbot_glm, state_glm], [chatbot_glm, state_glm])
|
99 |
+
b2.click(lambda: None, None, chatbot_glm, queue=False)
|
100 |
|
101 |
+
gr.HTML('''<center><a href="https://huggingface.co/spaces/ysharma/ChatGLM-6b_Gradio_Streaming?duplicate=true"><img src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a>Duplicate the Space and run securely with your OpenAI API Key</center>''')
|
102 |
gr.Markdown(description)
|
103 |
demo.queue(concurrency_count=16).launch(height= 800, debug=True)
|