Spaces:
Running
Running
zRzRzRzRzRzRzR
commited on
Commit
·
bba4030
1
Parent(s):
325c020
hf
Browse files
app.py
CHANGED
@@ -44,7 +44,7 @@ class GLM45Model:
|
|
44 |
if reasoning_content and not skip_think:
|
45 |
reasoning_escaped = html.escape(reasoning_content).replace("\n", "<br>")
|
46 |
think_html = (
|
47 |
-
"<details open><summary style='cursor:pointer;font-weight:bold;color:#007acc;'
|
48 |
"<div style='color:#555555;line-height:1.6;padding:15px;border-left:4px solid #007acc;margin:10px 0;background-color:#f0f7ff;border-radius:4px;'>"
|
49 |
+ reasoning_escaped +
|
50 |
"</div></details>"
|
@@ -61,15 +61,16 @@ class GLM45Model:
|
|
61 |
def _build_messages(self, raw_hist, sys_prompt):
|
62 |
msgs = []
|
63 |
if sys_prompt.strip():
|
64 |
-
msgs.append({"role": "system", "content":
|
65 |
|
66 |
for h in raw_hist:
|
67 |
if h["role"] == "user":
|
68 |
-
msgs.append({"role": "user", "content":
|
69 |
else:
|
70 |
-
|
71 |
-
if
|
72 |
-
|
|
|
73 |
return msgs
|
74 |
|
75 |
def stream_generate(self, raw_hist, sys_prompt, thinking_enabled=True, temperature=1.0):
|
@@ -109,7 +110,11 @@ def chat(msg, raw_hist, sys_prompt, thinking_enabled, temperature):
|
|
109 |
raw_hist = []
|
110 |
|
111 |
raw_hist.append({"role": "user", "content": msg.strip()})
|
112 |
-
place = {
|
|
|
|
|
|
|
|
|
113 |
raw_hist.append(place)
|
114 |
|
115 |
yield raw_hist, copy.deepcopy(raw_hist), ""
|
@@ -118,10 +123,15 @@ def chat(msg, raw_hist, sys_prompt, thinking_enabled, temperature):
|
|
118 |
for chunk in glm45.stream_generate(raw_hist[:-1], sys_prompt, thinking_enabled, temperature):
|
119 |
if stop_generation:
|
120 |
break
|
121 |
-
|
|
|
|
|
|
|
|
|
122 |
yield raw_hist, copy.deepcopy(raw_hist), ""
|
123 |
except Exception as e:
|
124 |
-
place["content"] = f"
|
|
|
125 |
yield raw_hist, copy.deepcopy(raw_hist), ""
|
126 |
|
127 |
|
@@ -132,6 +142,17 @@ def reset():
|
|
132 |
return [], [], ""
|
133 |
|
134 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
135 |
demo = gr.Blocks(title="GLM-4.5 API Demo", theme=gr.themes.Soft())
|
136 |
|
137 |
with demo:
|
@@ -139,8 +160,8 @@ with demo:
|
|
139 |
"<div style='text-align:center;font-size:32px;font-weight:bold;margin-bottom:10px;'>GLM-4.5 API Demo</div>"
|
140 |
"<div style='text-align:center;color:red;font-size:16px;margin-bottom:20px;'>"
|
141 |
"This demo uses the API version of the service for faster response speeds.<br>"
|
142 |
-
"Only chat functionality is supported. For tool usage, MCP support, and web search, please refer to the API documentation.</div>"
|
143 |
-
"<div style='text-align:center;'><a href='https://
|
144 |
"<a href='https://github.com/zai-org/GLM-4.5'>Github</a> | "
|
145 |
"<a href='http://z.ai/blog/glm-4.5'>Blog</a> | "
|
146 |
"<a href='https://docs.bigmodel.cn/cn/guide/models/text/glm-4.5'>API Docs</a></div>"
|
@@ -179,13 +200,20 @@ with demo:
|
|
179 |
)
|
180 |
sys = gr.Textbox(label="System Prompt", lines=6)
|
181 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
182 |
send.click(
|
183 |
-
|
184 |
inputs=[textbox, raw_history, sys, thinking_toggle, temperature_slider],
|
185 |
outputs=[chatbox, raw_history, textbox]
|
186 |
)
|
187 |
textbox.submit(
|
188 |
-
|
189 |
inputs=[textbox, raw_history, sys, thinking_toggle, temperature_slider],
|
190 |
outputs=[chatbox, raw_history, textbox]
|
191 |
)
|
|
|
44 |
if reasoning_content and not skip_think:
|
45 |
reasoning_escaped = html.escape(reasoning_content).replace("\n", "<br>")
|
46 |
think_html = (
|
47 |
+
"<details open><summary style='cursor:pointer;font-weight:bold;color:#007acc;'>Thinking</summary>"
|
48 |
"<div style='color:#555555;line-height:1.6;padding:15px;border-left:4px solid #007acc;margin:10px 0;background-color:#f0f7ff;border-radius:4px;'>"
|
49 |
+ reasoning_escaped +
|
50 |
"</div></details>"
|
|
|
61 |
def _build_messages(self, raw_hist, sys_prompt):
|
62 |
msgs = []
|
63 |
if sys_prompt.strip():
|
64 |
+
msgs.append({"role": "system", "content": sys_prompt.strip()})
|
65 |
|
66 |
for h in raw_hist:
|
67 |
if h["role"] == "user":
|
68 |
+
msgs.append({"role": "user", "content": h["content"]})
|
69 |
else:
|
70 |
+
msg = {"role": "assistant", "content": h.get("content", "")}
|
71 |
+
if h.get("reasoning_content"):
|
72 |
+
msg["reasoning_content"] = h.get("reasoning_content")
|
73 |
+
msgs.append(msg)
|
74 |
return msgs
|
75 |
|
76 |
def stream_generate(self, raw_hist, sys_prompt, thinking_enabled=True, temperature=1.0):
|
|
|
110 |
raw_hist = []
|
111 |
|
112 |
raw_hist.append({"role": "user", "content": msg.strip()})
|
113 |
+
place = {
|
114 |
+
"role": "assistant",
|
115 |
+
"content": "",
|
116 |
+
"reasoning_content": ""
|
117 |
+
}
|
118 |
raw_hist.append(place)
|
119 |
|
120 |
yield raw_hist, copy.deepcopy(raw_hist), ""
|
|
|
123 |
for chunk in glm45.stream_generate(raw_hist[:-1], sys_prompt, thinking_enabled, temperature):
|
124 |
if stop_generation:
|
125 |
break
|
126 |
+
|
127 |
+
place["content"] = glm45.accumulated_content
|
128 |
+
place["reasoning_content"] = glm45.accumulated_reasoning
|
129 |
+
place["display_content"] = chunk
|
130 |
+
|
131 |
yield raw_hist, copy.deepcopy(raw_hist), ""
|
132 |
except Exception as e:
|
133 |
+
place["content"] = f"Error: {str(e)}"
|
134 |
+
place["display_content"] = f"<div style='color: red;'>Error: {html.escape(str(e))}</div>"
|
135 |
yield raw_hist, copy.deepcopy(raw_hist), ""
|
136 |
|
137 |
|
|
|
142 |
return [], [], ""
|
143 |
|
144 |
|
145 |
+
def format_history_for_display(raw_hist):
|
146 |
+
display_hist = []
|
147 |
+
for msg in raw_hist:
|
148 |
+
if msg["role"] == "user":
|
149 |
+
display_hist.append({"role": "user", "content": msg["content"]})
|
150 |
+
else:
|
151 |
+
content = msg.get("display_content", msg.get("content", ""))
|
152 |
+
display_hist.append({"role": "assistant", "content": content})
|
153 |
+
return display_hist
|
154 |
+
|
155 |
+
|
156 |
demo = gr.Blocks(title="GLM-4.5 API Demo", theme=gr.themes.Soft())
|
157 |
|
158 |
with demo:
|
|
|
160 |
"<div style='text-align:center;font-size:32px;font-weight:bold;margin-bottom:10px;'>GLM-4.5 API Demo</div>"
|
161 |
"<div style='text-align:center;color:red;font-size:16px;margin-bottom:20px;'>"
|
162 |
"This demo uses the API version of the service for faster response speeds.<br>"
|
163 |
+
"Only chat functionality with 64K token length is supported. For tool usage, MCP support, and web search, please refer to the API documentation.</div>"
|
164 |
+
"<div style='text-align:center;'><a href='https://modelscope.cn/collections/GLM-45-b8693e2a08984f'>Model</a> | "
|
165 |
"<a href='https://github.com/zai-org/GLM-4.5'>Github</a> | "
|
166 |
"<a href='http://z.ai/blog/glm-4.5'>Blog</a> | "
|
167 |
"<a href='https://docs.bigmodel.cn/cn/guide/models/text/glm-4.5'>API Docs</a></div>"
|
|
|
200 |
)
|
201 |
sys = gr.Textbox(label="System Prompt", lines=6)
|
202 |
|
203 |
+
|
204 |
+
def chat_wrapper(msg, raw_hist, sys_prompt, thinking_enabled, temperature):
|
205 |
+
for hist, raw_hist_updated, textbox_value in chat(msg, raw_hist, sys_prompt, thinking_enabled, temperature):
|
206 |
+
display_hist = format_history_for_display(hist)
|
207 |
+
yield display_hist, raw_hist_updated, textbox_value
|
208 |
+
|
209 |
+
|
210 |
send.click(
|
211 |
+
chat_wrapper,
|
212 |
inputs=[textbox, raw_history, sys, thinking_toggle, temperature_slider],
|
213 |
outputs=[chatbox, raw_history, textbox]
|
214 |
)
|
215 |
textbox.submit(
|
216 |
+
chat_wrapper,
|
217 |
inputs=[textbox, raw_history, sys, thinking_toggle, temperature_slider],
|
218 |
outputs=[chatbox, raw_history, textbox]
|
219 |
)
|