Spaces:
Running
on
Zero
Running
on
Zero
zRzRzRzRzRzRzR
commited on
Commit
·
548989b
1
Parent(s):
e076e33
02
Browse files
app.py
CHANGED
|
@@ -11,11 +11,11 @@ import subprocess
|
|
| 11 |
import tempfile
|
| 12 |
import os
|
| 13 |
import time
|
|
|
|
| 14 |
|
| 15 |
MODEL_PATH = "THUDM/GLM-4.1V-9B-Thinking"
|
| 16 |
stop_generation = False
|
| 17 |
|
| 18 |
-
# Global model and processor
|
| 19 |
processor = AutoProcessor.from_pretrained(MODEL_PATH, use_fast=True)
|
| 20 |
model = Glm4vForConditionalGeneration.from_pretrained(
|
| 21 |
MODEL_PATH,
|
|
@@ -25,12 +25,12 @@ model = Glm4vForConditionalGeneration.from_pretrained(
|
|
| 25 |
)
|
| 26 |
|
| 27 |
|
| 28 |
-
def _strip_html(
|
| 29 |
-
return re.sub(r"<[^>]+>", "",
|
| 30 |
|
| 31 |
|
| 32 |
-
def _wrap_text(
|
| 33 |
-
return [{"type": "text", "text":
|
| 34 |
|
| 35 |
|
| 36 |
def _pdf_to_imgs(pdf_path):
|
|
@@ -68,31 +68,30 @@ def _files_to_content(media):
|
|
| 68 |
out.append({"type": "image", "url": p})
|
| 69 |
elif ext == ".pdf":
|
| 70 |
for p in _pdf_to_imgs(f.name):
|
| 71 |
-
out.append({"type": "image", "
|
| 72 |
return out
|
| 73 |
|
| 74 |
|
| 75 |
-
def _stream_fragment(buf
|
| 76 |
think_html = ""
|
| 77 |
-
if "<think>" in buf:
|
| 78 |
if "</think>" in buf:
|
| 79 |
seg = re.search(r"<think>(.*?)</think>", buf, re.DOTALL)
|
| 80 |
if seg:
|
|
|
|
| 81 |
think_html = (
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
+ "</div></details>"
|
| 86 |
)
|
| 87 |
else:
|
| 88 |
part = buf.split("<think>", 1)[1]
|
|
|
|
| 89 |
think_html = (
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
+ "</div></details>"
|
| 94 |
)
|
| 95 |
-
|
| 96 |
answer_html = ""
|
| 97 |
if "<answer>" in buf:
|
| 98 |
if "</answer>" in buf:
|
|
@@ -101,7 +100,10 @@ def _stream_fragment(buf: str) -> str:
|
|
| 101 |
answer_html = seg.group(1).strip()
|
| 102 |
else:
|
| 103 |
answer_html = buf.split("<answer>", 1)[1]
|
| 104 |
-
|
|
|
|
|
|
|
|
|
|
| 105 |
if not think_html and not answer_html:
|
| 106 |
return _strip_html(buf)
|
| 107 |
return think_html + answer_html
|
|
@@ -109,10 +111,8 @@ def _stream_fragment(buf: str) -> str:
|
|
| 109 |
|
| 110 |
def _build_messages(raw_hist, sys_prompt):
|
| 111 |
msgs = []
|
| 112 |
-
|
| 113 |
if sys_prompt.strip():
|
| 114 |
msgs.append({"role": "system", "content": [{"type": "text", "text": sys_prompt.strip()}]})
|
| 115 |
-
|
| 116 |
for h in raw_hist:
|
| 117 |
if h["role"] == "user":
|
| 118 |
msgs.append({"role": "user", "content": h["content"]})
|
|
@@ -120,8 +120,7 @@ def _build_messages(raw_hist, sys_prompt):
|
|
| 120 |
raw = h["content"]
|
| 121 |
raw = re.sub(r"<think>.*?</think>", "", raw, flags=re.DOTALL)
|
| 122 |
raw = re.sub(r"<details.*?</details>", "", raw, flags=re.DOTALL)
|
| 123 |
-
|
| 124 |
-
msgs.append({"role": "assistant", "content": _wrap_text(clean)})
|
| 125 |
return msgs
|
| 126 |
|
| 127 |
|
|
@@ -138,30 +137,25 @@ def stream_generate(raw_hist, sys_prompt):
|
|
| 138 |
return_tensors="pt",
|
| 139 |
padding=True,
|
| 140 |
).to(model.device)
|
| 141 |
-
|
| 142 |
streamer = TextIteratorStreamer(processor.tokenizer, skip_prompt=True, skip_special_tokens=False)
|
| 143 |
-
|
| 144 |
inputs,
|
| 145 |
max_new_tokens=8192,
|
| 146 |
repetition_penalty=1.1,
|
| 147 |
do_sample=True,
|
| 148 |
top_k=2,
|
| 149 |
-
temperature=
|
| 150 |
-
top_p=1e-5,
|
| 151 |
streamer=streamer,
|
| 152 |
)
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
generation_thread.start()
|
| 156 |
-
|
| 157 |
buf = ""
|
| 158 |
for tok in streamer:
|
| 159 |
if stop_generation:
|
| 160 |
break
|
| 161 |
buf += tok
|
| 162 |
yield _stream_fragment(buf)
|
| 163 |
-
|
| 164 |
-
generation_thread.join()
|
| 165 |
|
| 166 |
|
| 167 |
def format_display_content(content):
|
|
@@ -173,7 +167,6 @@ def format_display_content(content):
|
|
| 173 |
text_parts.append(item["text"])
|
| 174 |
else:
|
| 175 |
file_count += 1
|
| 176 |
-
|
| 177 |
display_text = " ".join(text_parts)
|
| 178 |
if file_count > 0:
|
| 179 |
return f"[{file_count} file(s) uploaded]\n{display_text}"
|
|
@@ -216,39 +209,32 @@ def check_files(files):
|
|
| 216 |
def chat(files, msg, raw_hist, sys_prompt):
|
| 217 |
global stop_generation
|
| 218 |
stop_generation = False
|
| 219 |
-
|
| 220 |
ok, err = check_files(files)
|
| 221 |
if not ok:
|
| 222 |
raw_hist.append({"role": "assistant", "content": err})
|
| 223 |
display_hist = create_display_history(raw_hist)
|
| 224 |
yield display_hist, copy.deepcopy(raw_hist), None, ""
|
| 225 |
return
|
| 226 |
-
|
| 227 |
payload = _files_to_content(files) if files else None
|
| 228 |
if msg.strip():
|
| 229 |
if payload is None:
|
| 230 |
payload = _wrap_text(msg.strip())
|
| 231 |
else:
|
| 232 |
payload.append({"type": "text", "text": msg.strip()})
|
| 233 |
-
|
| 234 |
user_rec = {"role": "user", "content": payload if payload else msg.strip()}
|
| 235 |
if raw_hist is None:
|
| 236 |
raw_hist = []
|
| 237 |
raw_hist.append(user_rec)
|
| 238 |
-
|
| 239 |
place = {"role": "assistant", "content": ""}
|
| 240 |
raw_hist.append(place)
|
| 241 |
-
|
| 242 |
display_hist = create_display_history(raw_hist)
|
| 243 |
yield display_hist, copy.deepcopy(raw_hist), None, ""
|
| 244 |
-
|
| 245 |
for chunk in stream_generate(raw_hist[:-1], sys_prompt):
|
| 246 |
if stop_generation:
|
| 247 |
break
|
| 248 |
place["content"] = chunk
|
| 249 |
display_hist = create_display_history(raw_hist)
|
| 250 |
yield display_hist, copy.deepcopy(raw_hist), None, ""
|
| 251 |
-
|
| 252 |
display_hist = create_display_history(raw_hist)
|
| 253 |
yield display_hist, copy.deepcopy(raw_hist), None, ""
|
| 254 |
|
|
@@ -260,52 +246,27 @@ def reset():
|
|
| 260 |
return [], [], None, ""
|
| 261 |
|
| 262 |
|
| 263 |
-
|
| 264 |
-
details summary{cursor:pointer;font-weight:bold}
|
| 265 |
-
details[open] summary{margin-bottom:10px}"""
|
| 266 |
-
|
| 267 |
-
demo = gr.Blocks(title="GLM-4.1V Chat", theme=gr.themes.Soft(), css=css)
|
| 268 |
with demo:
|
| 269 |
-
gr.Markdown(
|
| 270 |
-
|
| 271 |
-
GLM-4.1V-9B-Thinking Gradio Space🤗
|
| 272 |
-
</div>
|
| 273 |
-
<div style="text-align: center;">
|
| 274 |
-
<a href="https://huggingface.co/THUDM/GLM-4.1V-9B-Thinking">🤗 Model Hub</a> |
|
| 275 |
-
<a href="https://github.com/THUDM/GLM-4.1V-Thinking">🌐 Github</a>
|
| 276 |
-
</div>
|
| 277 |
-
""")
|
| 278 |
-
|
| 279 |
raw_history = gr.State([])
|
| 280 |
-
|
| 281 |
with gr.Row():
|
| 282 |
with gr.Column(scale=7):
|
| 283 |
-
chatbox = gr.Chatbot(
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
height=600,
|
| 287 |
-
elem_classes="chatbot-container",
|
| 288 |
-
)
|
| 289 |
-
textbox = gr.Textbox(label="💭 Message", lines=3)
|
| 290 |
with gr.Row():
|
| 291 |
send = gr.Button("Send", variant="primary")
|
| 292 |
clear = gr.Button("Clear")
|
| 293 |
with gr.Column(scale=3):
|
| 294 |
-
up = gr.File(
|
| 295 |
-
label="📁 Upload",
|
| 296 |
-
file_count="multiple",
|
| 297 |
-
file_types=["file"],
|
| 298 |
-
type="filepath",
|
| 299 |
-
)
|
| 300 |
gr.Markdown("Supports images / videos / PPT / PDF")
|
| 301 |
gr.Markdown(
|
| 302 |
-
"The maximum supported input is 10 images or 1 video/PPT/PDF. During the conversation, video and images cannot be present at the same time."
|
| 303 |
-
)
|
| 304 |
-
sys = gr.Textbox(label="⚙️ System Prompt", lines=6)
|
| 305 |
-
|
| 306 |
send.click(chat, inputs=[up, textbox, raw_history, sys], outputs=[chatbox, raw_history, up, textbox])
|
| 307 |
textbox.submit(chat, inputs=[up, textbox, raw_history, sys], outputs=[chatbox, raw_history, up, textbox])
|
| 308 |
clear.click(reset, outputs=[chatbox, raw_history, up, textbox])
|
| 309 |
-
|
| 310 |
if __name__ == "__main__":
|
| 311 |
-
demo.launch()
|
|
|
|
| 11 |
import tempfile
|
| 12 |
import os
|
| 13 |
import time
|
| 14 |
+
import html
|
| 15 |
|
| 16 |
MODEL_PATH = "THUDM/GLM-4.1V-9B-Thinking"
|
| 17 |
stop_generation = False
|
| 18 |
|
|
|
|
| 19 |
processor = AutoProcessor.from_pretrained(MODEL_PATH, use_fast=True)
|
| 20 |
model = Glm4vForConditionalGeneration.from_pretrained(
|
| 21 |
MODEL_PATH,
|
|
|
|
| 25 |
)
|
| 26 |
|
| 27 |
|
| 28 |
+
def _strip_html(text):
|
| 29 |
+
return re.sub(r"<[^>]+>", "", text).strip()
|
| 30 |
|
| 31 |
|
| 32 |
+
def _wrap_text(text):
|
| 33 |
+
return [{"type": "text", "text": text}]
|
| 34 |
|
| 35 |
|
| 36 |
def _pdf_to_imgs(pdf_path):
|
|
|
|
| 68 |
out.append({"type": "image", "url": p})
|
| 69 |
elif ext == ".pdf":
|
| 70 |
for p in _pdf_to_imgs(f.name):
|
| 71 |
+
out.append({"type": "image", "image_url": p})
|
| 72 |
return out
|
| 73 |
|
| 74 |
|
| 75 |
+
def _stream_fragment(buf, skip_think=False):
|
| 76 |
think_html = ""
|
| 77 |
+
if "<think>" in buf and not skip_think:
|
| 78 |
if "</think>" in buf:
|
| 79 |
seg = re.search(r"<think>(.*?)</think>", buf, re.DOTALL)
|
| 80 |
if seg:
|
| 81 |
+
think_content = seg.group(1).strip().replace("\\n", "\n").replace("\n", "<br>")
|
| 82 |
think_html = (
|
| 83 |
+
"<details open><summary style='cursor:pointer;font-weight:bold;color:#007acc;'>💭 Thinking</summary>"
|
| 84 |
+
"<div style='color:#555555;line-height:1.6;padding:15px;border-left:4px solid #007acc;margin:10px 0;background-color:#f0f7ff;border-radius:4px;'>"
|
| 85 |
+
+ think_content + "</div></details>"
|
|
|
|
| 86 |
)
|
| 87 |
else:
|
| 88 |
part = buf.split("<think>", 1)[1]
|
| 89 |
+
think_content = part.replace("\\n", "\n").replace("\n", "<br>")
|
| 90 |
think_html = (
|
| 91 |
+
"<details open><summary style='cursor:pointer;font-weight:bold;color:#007acc;'>💭 Thinking</summary>"
|
| 92 |
+
"<div style='color:#555555;line-height:1.6;padding:15px;border-left:4px solid #007acc;margin:10px 0;background-color:#f0f7ff;border-radius:4px;'>"
|
| 93 |
+
+ think_content + "</div></details>"
|
|
|
|
| 94 |
)
|
|
|
|
| 95 |
answer_html = ""
|
| 96 |
if "<answer>" in buf:
|
| 97 |
if "</answer>" in buf:
|
|
|
|
| 100 |
answer_html = seg.group(1).strip()
|
| 101 |
else:
|
| 102 |
answer_html = buf.split("<answer>", 1)[1]
|
| 103 |
+
if answer_html:
|
| 104 |
+
answer_html_raw = answer_html.replace("\\n", "\n")
|
| 105 |
+
escaped = html.escape(answer_html_raw)
|
| 106 |
+
answer_html = f"<pre class='code-block'><code class='language-html'>{escaped}</code></pre>"
|
| 107 |
if not think_html and not answer_html:
|
| 108 |
return _strip_html(buf)
|
| 109 |
return think_html + answer_html
|
|
|
|
| 111 |
|
| 112 |
def _build_messages(raw_hist, sys_prompt):
|
| 113 |
msgs = []
|
|
|
|
| 114 |
if sys_prompt.strip():
|
| 115 |
msgs.append({"role": "system", "content": [{"type": "text", "text": sys_prompt.strip()}]})
|
|
|
|
| 116 |
for h in raw_hist:
|
| 117 |
if h["role"] == "user":
|
| 118 |
msgs.append({"role": "user", "content": h["content"]})
|
|
|
|
| 120 |
raw = h["content"]
|
| 121 |
raw = re.sub(r"<think>.*?</think>", "", raw, flags=re.DOTALL)
|
| 122 |
raw = re.sub(r"<details.*?</details>", "", raw, flags=re.DOTALL)
|
| 123 |
+
msgs.append({"role": "assistant", "content": _wrap_text(_strip_html(raw).strip())})
|
|
|
|
| 124 |
return msgs
|
| 125 |
|
| 126 |
|
|
|
|
| 137 |
return_tensors="pt",
|
| 138 |
padding=True,
|
| 139 |
).to(model.device)
|
|
|
|
| 140 |
streamer = TextIteratorStreamer(processor.tokenizer, skip_prompt=True, skip_special_tokens=False)
|
| 141 |
+
gen_kwargs = dict(
|
| 142 |
inputs,
|
| 143 |
max_new_tokens=8192,
|
| 144 |
repetition_penalty=1.1,
|
| 145 |
do_sample=True,
|
| 146 |
top_k=2,
|
| 147 |
+
temperature=0.01,
|
|
|
|
| 148 |
streamer=streamer,
|
| 149 |
)
|
| 150 |
+
thread = threading.Thread(target=model.generate, kwargs=gen_kwargs)
|
| 151 |
+
thread.start()
|
|
|
|
|
|
|
| 152 |
buf = ""
|
| 153 |
for tok in streamer:
|
| 154 |
if stop_generation:
|
| 155 |
break
|
| 156 |
buf += tok
|
| 157 |
yield _stream_fragment(buf)
|
| 158 |
+
thread.join()
|
|
|
|
| 159 |
|
| 160 |
|
| 161 |
def format_display_content(content):
|
|
|
|
| 167 |
text_parts.append(item["text"])
|
| 168 |
else:
|
| 169 |
file_count += 1
|
|
|
|
| 170 |
display_text = " ".join(text_parts)
|
| 171 |
if file_count > 0:
|
| 172 |
return f"[{file_count} file(s) uploaded]\n{display_text}"
|
|
|
|
| 209 |
def chat(files, msg, raw_hist, sys_prompt):
|
| 210 |
global stop_generation
|
| 211 |
stop_generation = False
|
|
|
|
| 212 |
ok, err = check_files(files)
|
| 213 |
if not ok:
|
| 214 |
raw_hist.append({"role": "assistant", "content": err})
|
| 215 |
display_hist = create_display_history(raw_hist)
|
| 216 |
yield display_hist, copy.deepcopy(raw_hist), None, ""
|
| 217 |
return
|
|
|
|
| 218 |
payload = _files_to_content(files) if files else None
|
| 219 |
if msg.strip():
|
| 220 |
if payload is None:
|
| 221 |
payload = _wrap_text(msg.strip())
|
| 222 |
else:
|
| 223 |
payload.append({"type": "text", "text": msg.strip()})
|
|
|
|
| 224 |
user_rec = {"role": "user", "content": payload if payload else msg.strip()}
|
| 225 |
if raw_hist is None:
|
| 226 |
raw_hist = []
|
| 227 |
raw_hist.append(user_rec)
|
|
|
|
| 228 |
place = {"role": "assistant", "content": ""}
|
| 229 |
raw_hist.append(place)
|
|
|
|
| 230 |
display_hist = create_display_history(raw_hist)
|
| 231 |
yield display_hist, copy.deepcopy(raw_hist), None, ""
|
|
|
|
| 232 |
for chunk in stream_generate(raw_hist[:-1], sys_prompt):
|
| 233 |
if stop_generation:
|
| 234 |
break
|
| 235 |
place["content"] = chunk
|
| 236 |
display_hist = create_display_history(raw_hist)
|
| 237 |
yield display_hist, copy.deepcopy(raw_hist), None, ""
|
|
|
|
| 238 |
display_hist = create_display_history(raw_hist)
|
| 239 |
yield display_hist, copy.deepcopy(raw_hist), None, ""
|
| 240 |
|
|
|
|
| 246 |
return [], [], None, ""
|
| 247 |
|
| 248 |
|
| 249 |
+
demo = gr.Blocks(title="GLM-4.1V-9B-Thinking", theme=gr.themes.Soft())
|
|
|
|
|
|
|
|
|
|
|
|
|
| 250 |
with demo:
|
| 251 |
+
gr.Markdown(
|
| 252 |
+
"<div style='text-align:center;font-size:32px;font-weight:bold;margin-bottom:20px;'>GLM-4.1V-9B Thinking</div><div style='text-align:center;'><a href='https://huggingface.co/THUDM/GLM-4.1V-9B-Thinking'>Model Hub</a> | <a href='https://github.com/THUDM/GLM-4.1V-Thinking'>Github</a></div>")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 253 |
raw_history = gr.State([])
|
|
|
|
| 254 |
with gr.Row():
|
| 255 |
with gr.Column(scale=7):
|
| 256 |
+
chatbox = gr.Chatbot(label="Chat", type="messages", height=600, elem_classes="chatbot-container",
|
| 257 |
+
sanitize_html=False, line_breaks=True)
|
| 258 |
+
textbox = gr.Textbox(label="Message", lines=3)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 259 |
with gr.Row():
|
| 260 |
send = gr.Button("Send", variant="primary")
|
| 261 |
clear = gr.Button("Clear")
|
| 262 |
with gr.Column(scale=3):
|
| 263 |
+
up = gr.File(label="Upload Files", file_count="multiple", file_types=["file"], type="filepath")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 264 |
gr.Markdown("Supports images / videos / PPT / PDF")
|
| 265 |
gr.Markdown(
|
| 266 |
+
"The maximum supported input is 10 images or 1 video/PPT/PDF. During the conversation, video and images cannot be present at the same time.")
|
| 267 |
+
sys = gr.Textbox(label="System Prompt", lines=6)
|
|
|
|
|
|
|
| 268 |
send.click(chat, inputs=[up, textbox, raw_history, sys], outputs=[chatbox, raw_history, up, textbox])
|
| 269 |
textbox.submit(chat, inputs=[up, textbox, raw_history, sys], outputs=[chatbox, raw_history, up, textbox])
|
| 270 |
clear.click(reset, outputs=[chatbox, raw_history, up, textbox])
|
|
|
|
| 271 |
if __name__ == "__main__":
|
| 272 |
+
demo.launch(server_port=8000,server_name="0.0.0.0")
|