Spaces:
Running
on
Zero
Running
on
Zero
zRzRzRzRzRzRzR
commited on
Commit
·
a9c9440
1
Parent(s):
3d78630
app.py
CHANGED
|
@@ -1,17 +1,17 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import torch
|
| 3 |
-
from transformers import AutoProcessor, Glm4vForConditionalGeneration, TextIteratorStreamer
|
| 4 |
-
from pathlib import Path
|
| 5 |
-
import threading
|
| 6 |
-
import re
|
| 7 |
import copy
|
| 8 |
-
import
|
| 9 |
-
import
|
| 10 |
import subprocess
|
| 11 |
import tempfile
|
| 12 |
-
import
|
|
|
|
|
|
|
|
|
|
| 13 |
import time
|
| 14 |
import html
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
MODEL_PATH = "THUDM/GLM-4.1V-9B-Thinking"
|
| 17 |
stop_generation = False
|
|
@@ -20,142 +20,142 @@ processor = AutoProcessor.from_pretrained(MODEL_PATH, use_fast=True)
|
|
| 20 |
model = Glm4vForConditionalGeneration.from_pretrained(
|
| 21 |
MODEL_PATH,
|
| 22 |
torch_dtype=torch.bfloat16,
|
| 23 |
-
device_map="auto"
|
| 24 |
-
attn_implementation="sdpa",
|
| 25 |
)
|
| 26 |
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
|
| 32 |
-
def _wrap_text(text):
|
| 33 |
-
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
def
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
doc.close()
|
| 45 |
-
return imgs
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
def _ppt_to_imgs(ppt_path):
|
| 49 |
-
tmp = tempfile.mkdtemp()
|
| 50 |
-
subprocess.run(
|
| 51 |
-
["libreoffice", "--headless", "--convert-to", "pdf", "--outdir", tmp, ppt_path],
|
| 52 |
-
check=True,
|
| 53 |
-
)
|
| 54 |
-
pdf_path = os.path.join(tmp, Path(ppt_path).stem + ".pdf")
|
| 55 |
-
return _pdf_to_imgs(pdf_path)
|
| 56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
-
def
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 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
|
|
|
|
| 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:
|
| 98 |
-
seg = re.search(r"<answer>(.*?)</answer>", buf, re.DOTALL)
|
| 99 |
-
if seg:
|
| 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
|
| 110 |
-
|
| 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"]})
|
| 119 |
-
else:
|
| 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 |
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
|
| 160 |
|
| 161 |
def format_display_content(content):
|
|
@@ -185,6 +185,9 @@ def create_display_history(raw_hist):
|
|
| 185 |
return display_hist
|
| 186 |
|
| 187 |
|
|
|
|
|
|
|
|
|
|
| 188 |
def check_files(files):
|
| 189 |
vids = imgs = ppts = pdfs = 0
|
| 190 |
for f in files or []:
|
|
@@ -215,10 +218,10 @@ def chat(files, msg, raw_hist, sys_prompt):
|
|
| 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()}
|
|
@@ -229,7 +232,7 @@ def chat(files, msg, raw_hist, sys_prompt):
|
|
| 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
|
|
@@ -247,14 +250,27 @@ def reset():
|
|
| 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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 253 |
raw_history = gr.State([])
|
|
|
|
| 254 |
with gr.Row():
|
| 255 |
with gr.Column(scale=7):
|
| 256 |
-
chatbox = gr.Chatbot(
|
| 257 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 258 |
textbox = gr.Textbox(label="Message", lines=3)
|
| 259 |
with gr.Row():
|
| 260 |
send = gr.Button("Send", variant="primary")
|
|
@@ -263,10 +279,25 @@ with demo:
|
|
| 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
|
|
|
|
|
|
|
| 267 |
sys = gr.Textbox(label="System Prompt", lines=6)
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 271 |
if __name__ == "__main__":
|
| 272 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import copy
|
| 2 |
+
import os
|
| 3 |
+
import re
|
| 4 |
import subprocess
|
| 5 |
import tempfile
|
| 6 |
+
import threading
|
| 7 |
+
from pathlib import Path
|
| 8 |
+
import fitz
|
| 9 |
+
import gradio as gr
|
| 10 |
import time
|
| 11 |
import html
|
| 12 |
+
import torch
|
| 13 |
+
from transformers import AutoProcessor, Glm4vForConditionalGeneration, TextIteratorStreamer
|
| 14 |
+
import spaces
|
| 15 |
|
| 16 |
MODEL_PATH = "THUDM/GLM-4.1V-9B-Thinking"
|
| 17 |
stop_generation = False
|
|
|
|
| 20 |
model = Glm4vForConditionalGeneration.from_pretrained(
|
| 21 |
MODEL_PATH,
|
| 22 |
torch_dtype=torch.bfloat16,
|
| 23 |
+
device_map="auto"
|
|
|
|
| 24 |
)
|
| 25 |
|
| 26 |
|
| 27 |
+
class GLM4VModel:
|
| 28 |
+
def _strip_html(self, text: str) -> str:
|
| 29 |
+
return re.sub(r"<[^>]+>", "", text).strip()
|
| 30 |
|
| 31 |
+
def _wrap_text(self, text: str):
|
| 32 |
+
return [{"type": "text", "text": text}]
|
| 33 |
|
| 34 |
+
def _pdf_to_imgs(self, pdf_path):
|
| 35 |
+
doc = fitz.open(pdf_path)
|
| 36 |
+
imgs = []
|
| 37 |
+
for i in range(doc.page_count):
|
| 38 |
+
pix = doc.load_page(i).get_pixmap(dpi=180)
|
| 39 |
+
img_p = os.path.join(tempfile.gettempdir(), f"{Path(pdf_path).stem}_{i}.png")
|
| 40 |
+
pix.save(img_p)
|
| 41 |
+
imgs.append(img_p)
|
| 42 |
+
doc.close()
|
| 43 |
+
return imgs
|
| 44 |
|
| 45 |
+
def _ppt_to_imgs(self, ppt_path):
|
| 46 |
+
tmp = tempfile.mkdtemp()
|
| 47 |
+
subprocess.run(
|
| 48 |
+
["libreoffice", "--headless", "--convert-to", "pdf", "--outdir", tmp, ppt_path],
|
| 49 |
+
check=True,
|
| 50 |
+
)
|
| 51 |
+
pdf_path = os.path.join(tmp, Path(ppt_path).stem + ".pdf")
|
| 52 |
+
return self._pdf_to_imgs(pdf_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
+
def _files_to_content(self, media):
|
| 55 |
+
out = []
|
| 56 |
+
for f in media or []:
|
| 57 |
+
ext = Path(f.name).suffix.lower()
|
| 58 |
+
if ext in [".mp4", ".avi", ".mkv", ".mov", ".wmv", ".flv", ".webm", ".mpeg", ".m4v"]:
|
| 59 |
+
out.append({"type": "video", "url": f.name})
|
| 60 |
+
elif ext in [".jpg", ".jpeg", ".png", ".gif", ".bmp", ".tiff", ".webp"]:
|
| 61 |
+
out.append({"type": "image", "url": f.name})
|
| 62 |
+
elif ext in [".ppt", ".pptx"]:
|
| 63 |
+
for p in self._ppt_to_imgs(f.name):
|
| 64 |
+
out.append({"type": "image", "url": p})
|
| 65 |
+
elif ext == ".pdf":
|
| 66 |
+
for p in self._pdf_to_imgs(f.name):
|
| 67 |
+
out.append({"type": "image", "url": p})
|
| 68 |
+
return out
|
| 69 |
|
| 70 |
+
def _stream_fragment(self, buf: str, skip_think: bool = False):
|
| 71 |
+
think_html = ""
|
| 72 |
+
if "<think>" in buf and not skip_think:
|
| 73 |
+
if "</think>" in buf:
|
| 74 |
+
seg = re.search(r"<think>(.*?)</think>", buf, re.DOTALL)
|
| 75 |
+
if seg:
|
| 76 |
+
think_content = seg.group(1).strip().replace("\\n", "\n").replace("\n", "<br>")
|
| 77 |
+
think_html = (
|
| 78 |
+
"<details open><summary style='cursor:pointer;font-weight:bold;color:#007acc;'>💭 Thinking</summary>"
|
| 79 |
+
"<div style='color:#555555;line-height:1.6;padding:15px;border-left:4px solid #007acc;margin:10px 0;background-color:#f0f7ff;border-radius:4px;'>"
|
| 80 |
+
+ think_content
|
| 81 |
+
+ "</div></details>"
|
| 82 |
+
)
|
| 83 |
+
else:
|
| 84 |
+
part = buf.split("<think>", 1)[1]
|
| 85 |
+
think_content = part.replace("\\n", "\n").replace("\n", "<br>")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
think_html = (
|
| 87 |
"<details open><summary style='cursor:pointer;font-weight:bold;color:#007acc;'>💭 Thinking</summary>"
|
| 88 |
"<div style='color:#555555;line-height:1.6;padding:15px;border-left:4px solid #007acc;margin:10px 0;background-color:#f0f7ff;border-radius:4px;'>"
|
| 89 |
+
+ think_content
|
| 90 |
+
+ "</div></details>"
|
| 91 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
+
answer_html = ""
|
| 94 |
+
if "<answer>" in buf:
|
| 95 |
+
if "</answer>" in buf:
|
| 96 |
+
seg = re.search(r"<answer>(.*?)</answer>", buf, re.DOTALL)
|
| 97 |
+
if seg:
|
| 98 |
+
answer_html = seg.group(1).strip()
|
| 99 |
+
else:
|
| 100 |
+
answer_html = buf.split("<answer>", 1)[1]
|
| 101 |
|
| 102 |
+
if answer_html:
|
| 103 |
+
answer_html_raw = answer_html.replace("\\n", "\n")
|
| 104 |
+
if '<' in answer_html_raw and '>' in answer_html_raw:
|
| 105 |
+
escaped = html.escape(answer_html_raw)
|
| 106 |
+
answer_html = f"<pre class='code-block'><code class='language-html'>{escaped}</code></pre>"
|
| 107 |
+
else:
|
| 108 |
+
answer_html = f"<div style='margin:0.5em 0; white-space: pre-wrap; line-height:1.6;'>{html.escape(answer_html_raw)}</div>"
|
| 109 |
+
|
| 110 |
+
if not think_html and not answer_html:
|
| 111 |
+
return self._strip_html(buf)
|
| 112 |
+
return think_html + answer_html
|
| 113 |
+
|
| 114 |
+
def _build_messages(self, raw_hist, sys_prompt):
|
| 115 |
+
msgs = []
|
| 116 |
+
if sys_prompt.strip():
|
| 117 |
+
msgs.append({"role": "system", "content": [{"type": "text", "text": sys_prompt.strip()}]})
|
| 118 |
+
for h in raw_hist:
|
| 119 |
+
if h["role"] == "user":
|
| 120 |
+
msgs.append({"role": "user", "content": h["content"]})
|
| 121 |
+
else:
|
| 122 |
+
raw = re.sub(r"<think>.*?</think>", "", h["content"], flags=re.DOTALL)
|
| 123 |
+
raw = re.sub(r"<details.*?</details>", "", raw, flags=re.DOTALL)
|
| 124 |
+
msgs.append({"role": "assistant", "content": self._wrap_text(self._strip_html(raw).strip())})
|
| 125 |
+
return msgs
|
| 126 |
+
|
| 127 |
+
@spaces.GPU(duration=120)
|
| 128 |
+
def stream_generate(self, raw_hist, sys_prompt: str):
|
| 129 |
+
global stop_generation
|
| 130 |
+
stop_generation = False
|
| 131 |
+
msgs = self._build_messages(raw_hist, sys_prompt)
|
| 132 |
+
inputs = processor.apply_chat_template(
|
| 133 |
+
msgs,
|
| 134 |
+
tokenize=True,
|
| 135 |
+
add_generation_prompt=True,
|
| 136 |
+
return_dict=True,
|
| 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 self._stream_fragment(buf)
|
| 158 |
+
thread.join()
|
| 159 |
|
| 160 |
|
| 161 |
def format_display_content(content):
|
|
|
|
| 185 |
return display_hist
|
| 186 |
|
| 187 |
|
| 188 |
+
glm4v = GLM4VModel()
|
| 189 |
+
|
| 190 |
+
|
| 191 |
def check_files(files):
|
| 192 |
vids = imgs = ppts = pdfs = 0
|
| 193 |
for f in files or []:
|
|
|
|
| 218 |
display_hist = create_display_history(raw_hist)
|
| 219 |
yield display_hist, copy.deepcopy(raw_hist), None, ""
|
| 220 |
return
|
| 221 |
+
payload = glm4v._files_to_content(files) if files else None
|
| 222 |
if msg.strip():
|
| 223 |
if payload is None:
|
| 224 |
+
payload = glm4v._wrap_text(msg.strip())
|
| 225 |
else:
|
| 226 |
payload.append({"type": "text", "text": msg.strip()})
|
| 227 |
user_rec = {"role": "user", "content": payload if payload else msg.strip()}
|
|
|
|
| 232 |
raw_hist.append(place)
|
| 233 |
display_hist = create_display_history(raw_hist)
|
| 234 |
yield display_hist, copy.deepcopy(raw_hist), None, ""
|
| 235 |
+
for chunk in glm4v.stream_generate(raw_hist[:-1], sys_prompt):
|
| 236 |
if stop_generation:
|
| 237 |
break
|
| 238 |
place["content"] = chunk
|
|
|
|
| 250 |
|
| 251 |
|
| 252 |
demo = gr.Blocks(title="GLM-4.1V-9B-Thinking", theme=gr.themes.Soft())
|
| 253 |
+
|
| 254 |
with demo:
|
| 255 |
gr.Markdown(
|
| 256 |
+
"<div style='text-align:center;font-size:32px;font-weight:bold;margin-bottom:20px;'>GLM-4.1V-9B-Thinking</div>"
|
| 257 |
+
"<div style='text-align:center;'><a href='https://huggingface.co/THUDM/GLM-4.1V-9B-Thinking'>Model Hub</a> | "
|
| 258 |
+
"<a href='https://github.com/THUDM/GLM-4.1V-Thinking'>Github</a> |</div>"
|
| 259 |
+
"<a href='https://arxiv.org/abs/2507.01006'>Paper</a> |</div>"
|
| 260 |
+
"<a href='https://www.bigmodel.cn/dev/api/visual-reasoning-model/GLM-4.1V-Thinking'>API</a> |</div>"
|
| 261 |
+
)
|
| 262 |
raw_history = gr.State([])
|
| 263 |
+
|
| 264 |
with gr.Row():
|
| 265 |
with gr.Column(scale=7):
|
| 266 |
+
chatbox = gr.Chatbot(
|
| 267 |
+
label="Chat",
|
| 268 |
+
type="messages",
|
| 269 |
+
height=600,
|
| 270 |
+
elem_classes="chatbot-container",
|
| 271 |
+
sanitize_html=False,
|
| 272 |
+
line_breaks=True
|
| 273 |
+
)
|
| 274 |
textbox = gr.Textbox(label="Message", lines=3)
|
| 275 |
with gr.Row():
|
| 276 |
send = gr.Button("Send", variant="primary")
|
|
|
|
| 279 |
up = gr.File(label="Upload Files", file_count="multiple", file_types=["file"], type="filepath")
|
| 280 |
gr.Markdown("Supports images / videos / PPT / PDF")
|
| 281 |
gr.Markdown(
|
| 282 |
+
"The maximum supported input is 10 images or 1 video/PPT/PDF(less than 10 pages) in this demo. "
|
| 283 |
+
"During the conversation, video and images cannot be present at the same time."
|
| 284 |
+
)
|
| 285 |
sys = gr.Textbox(label="System Prompt", lines=6)
|
| 286 |
+
|
| 287 |
+
send.click(
|
| 288 |
+
chat,
|
| 289 |
+
inputs=[up, textbox, raw_history, sys],
|
| 290 |
+
outputs=[chatbox, raw_history, up, textbox]
|
| 291 |
+
)
|
| 292 |
+
textbox.submit(
|
| 293 |
+
chat,
|
| 294 |
+
inputs=[up, textbox, raw_history, sys],
|
| 295 |
+
outputs=[chatbox, raw_history, up, textbox]
|
| 296 |
+
)
|
| 297 |
+
clear.click(
|
| 298 |
+
reset,
|
| 299 |
+
outputs=[chatbox, raw_history, up, textbox]
|
| 300 |
+
)
|
| 301 |
+
|
| 302 |
if __name__ == "__main__":
|
| 303 |
+
demo.launch()
|