Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,31 +1,28 @@
|
|
1 |
-
# Copyright (c) Alibaba Cloud.
|
2 |
-
#
|
3 |
-
# This source code is licensed under the license found in the
|
4 |
-
# LICENSE file in the root directory of this source tree.
|
5 |
import os
|
6 |
import numpy as np
|
7 |
-
from urllib3.exceptions import HTTPError
|
8 |
-
os.system('pip install dashscope modelscope oss2 -U')
|
9 |
-
|
10 |
-
from argparse import ArgumentParser
|
11 |
-
from pathlib import Path
|
12 |
-
|
13 |
-
import copy
|
14 |
-
import gradio as gr
|
15 |
-
import oss2
|
16 |
-
import os
|
17 |
import re
|
18 |
import secrets
|
19 |
import tempfile
|
20 |
import requests
|
21 |
from http import HTTPStatus
|
|
|
|
|
|
|
|
|
|
|
22 |
from dashscope import MultiModalConversation
|
23 |
import dashscope
|
|
|
|
|
|
|
|
|
|
|
24 |
API_KEY = os.environ['API_KEY']
|
25 |
ENDPOINT = os.environ['ENDPOINT']
|
26 |
AK_ID = os.environ['AK_ID']
|
27 |
AK = os.environ['AK']
|
28 |
BUCKET_NAME = os.environ['BUCKET_NAME']
|
|
|
29 |
dashscope.api_key = API_KEY
|
30 |
endpoint = ENDPOINT
|
31 |
auth = oss2.Auth(AK_ID, AK)
|
@@ -35,27 +32,19 @@ REVISION = 'v1.0.4'
|
|
35 |
BOX_TAG_PATTERN = r"<box>([\s\S]*?)</box>"
|
36 |
PUNCTUATION = "!?。"#$%&'()*+,-/:;<=>@[\]^_`{|}~⦅⦆「」、、〃》「」『』【】〔〕〖〗〘〙〚〛〜〝〞〟〰〾〿–—‘’‛“”„‟…‧﹏."
|
37 |
|
38 |
-
|
39 |
def _get_args():
|
40 |
parser = ArgumentParser()
|
41 |
parser.add_argument("--revision", type=str, default=REVISION)
|
42 |
parser.add_argument("--cpu-only", action="store_true", help="Run demo with CPU only")
|
43 |
-
|
44 |
-
parser.add_argument("--
|
45 |
-
|
46 |
-
parser.add_argument("--
|
47 |
-
|
48 |
-
parser.add_argument("--server-port", type=int, default=7860,
|
49 |
-
help="Demo server port.")
|
50 |
-
parser.add_argument("--server-name", type=str, default="127.0.0.1",
|
51 |
-
help="Demo server name.")
|
52 |
-
|
53 |
-
args = parser.parse_args()
|
54 |
-
return args
|
55 |
|
56 |
def _parse_text(text):
|
57 |
lines = text.split("\n")
|
58 |
-
lines = [line for line in lines if line
|
59 |
count = 0
|
60 |
for i, line in enumerate(lines):
|
61 |
if "```" in line:
|
@@ -64,46 +53,29 @@ def _parse_text(text):
|
|
64 |
if count % 2 == 1:
|
65 |
lines[i] = f'<pre><code class="language-{items[-1]}">'
|
66 |
else:
|
67 |
-
lines[i] =
|
68 |
else:
|
69 |
-
if i > 0:
|
70 |
-
|
71 |
-
|
72 |
-
line = line.replace("<", "<")
|
73 |
-
line = line.replace(">", ">")
|
74 |
-
line = line.replace(" ", " ")
|
75 |
-
line = line.replace("*", "*")
|
76 |
-
line = line.replace("_", "_")
|
77 |
-
line = line.replace("-", "-")
|
78 |
-
line = line.replace(".", ".")
|
79 |
-
line = line.replace("!", "!")
|
80 |
-
line = line.replace("(", "(")
|
81 |
-
line = line.replace(")", ")")
|
82 |
-
line = line.replace("$", "$")
|
83 |
lines[i] = "<br>" + line
|
84 |
-
|
85 |
-
return text
|
86 |
-
|
87 |
|
88 |
def _remove_image_special(text):
|
89 |
text = text.replace('<ref>', '').replace('</ref>', '')
|
90 |
return re.sub(r'<box>.*?(</box>|$)', '', text)
|
91 |
|
92 |
-
|
93 |
def is_video_file(filename):
|
94 |
video_extensions = ['.mp4', '.avi', '.mkv', '.mov', '.wmv', '.flv', '.webm', '.mpeg']
|
95 |
return any(filename.lower().endswith(ext) for ext in video_extensions)
|
96 |
|
97 |
-
|
98 |
def _launch_demo(args):
|
99 |
-
uploaded_file_dir = os.environ.get("GRADIO_TEMP_DIR") or str(
|
100 |
-
Path(tempfile.gettempdir()) / "gradio"
|
101 |
-
)
|
102 |
|
103 |
def predict(_chatbot, task_history):
|
104 |
chat_query = _chatbot[-1][0]
|
105 |
query = task_history[-1][0]
|
106 |
-
if
|
107 |
_chatbot.pop()
|
108 |
task_history.pop()
|
109 |
return _chatbot
|
@@ -127,43 +99,32 @@ def _launch_demo(args):
|
|
127 |
messages.append({'role': 'assistant', 'content': [{'text': a}]})
|
128 |
content = []
|
129 |
messages.pop()
|
130 |
-
responses = MultiModalConversation.call(
|
131 |
-
model='qwen-vl-max-0809', messages=messages, stream=True,
|
132 |
-
)
|
133 |
for response in responses:
|
134 |
-
if
|
135 |
raise HTTPError(f'response.code: {response.code}\nresponse.message: {response.message}')
|
136 |
-
|
137 |
-
response_text = []
|
138 |
-
for ele in response:
|
139 |
-
if 'text' in ele:
|
140 |
-
response_text.append(ele['text'])
|
141 |
-
elif 'box' in ele:
|
142 |
-
response_text.append(ele['box'])
|
143 |
response_text = ''.join(response_text)
|
144 |
_chatbot[-1] = (_parse_text(chat_query), _remove_image_special(response_text))
|
145 |
yield _chatbot
|
146 |
-
|
147 |
-
|
148 |
-
result_image = response[-1]['result_image']
|
149 |
resp = requests.get(result_image)
|
150 |
os.makedirs(uploaded_file_dir, exist_ok=True)
|
151 |
-
|
152 |
-
filename = os.path.join(uploaded_file_dir, name)
|
153 |
with open(filename, 'wb') as f:
|
154 |
f.write(resp.content)
|
155 |
-
|
156 |
_chatbot.append((None, (filename,)))
|
157 |
else:
|
158 |
-
|
159 |
-
_chatbot[-1] = (_parse_text(chat_query),
|
160 |
-
full_response = _parse_text(
|
161 |
-
|
162 |
task_history[-1] = (query, full_response)
|
163 |
print("Qwen2-VL-Chat: " + _parse_text(full_response))
|
164 |
yield _chatbot
|
165 |
|
166 |
-
|
167 |
def regenerate(_chatbot, task_history):
|
168 |
if not task_history:
|
169 |
return _chatbot
|
@@ -182,17 +143,17 @@ def _launch_demo(args):
|
|
182 |
|
183 |
def add_text(history, task_history, text):
|
184 |
task_text = text
|
185 |
-
history = history
|
186 |
-
task_history = task_history
|
187 |
-
history
|
188 |
-
task_history
|
189 |
return history, task_history, ""
|
190 |
|
191 |
def add_file(history, task_history, file):
|
192 |
-
history = history
|
193 |
-
task_history = task_history
|
194 |
-
history
|
195 |
-
task_history
|
196 |
return history, task_history
|
197 |
|
198 |
def reset_user_input():
|
@@ -203,12 +164,9 @@ def _launch_demo(args):
|
|
203 |
return []
|
204 |
|
205 |
with gr.Blocks() as demo:
|
206 |
-
gr.Markdown("""
|
207 |
-
<p align="center"><img src="https://modelscope.oss-cn-beijing.aliyuncs.com/resource/qwen.png" style="height: 80px"/><p>""")
|
208 |
gr.Markdown("""<center><font size=8>Qwen2-VL-72B</center>""")
|
209 |
-
gr.Markdown(
|
210 |
-
"""\
|
211 |
-
<center><font size=3>This WebUI is based on Qwen2-VL-72B, developed by Alibaba Cloud.</center>""")
|
212 |
gr.Markdown("""<center><font size=3>本WebUI基于Qwen2-VL-72B。</center>""")
|
213 |
|
214 |
chatbot = gr.Chatbot(label='Qwen2-VL-72B', elem_classes="control-height", height=500)
|
@@ -221,34 +179,28 @@ def _launch_demo(args):
|
|
221 |
regen_btn = gr.Button("🤔️ Regenerate (重试)")
|
222 |
empty_bin = gr.Button("🧹 Clear History (清除历史)")
|
223 |
|
224 |
-
submit_btn.click(add_text, [chatbot, task_history, query], [chatbot, task_history],concurrency_limit
|
225 |
predict, [chatbot, task_history], [chatbot], show_progress=True
|
226 |
)
|
227 |
-
submit_btn.click(reset_user_input, [], [query],concurrency_limit
|
228 |
-
empty_bin.click(reset_state, [task_history], [chatbot], show_progress=True,concurrency_limit
|
229 |
-
regen_btn.click(regenerate, [chatbot, task_history], [chatbot], show_progress=True,concurrency_limit
|
230 |
-
addfile_btn.upload(add_file, [chatbot, task_history, addfile_btn], [chatbot, task_history], show_progress=True,concurrency_limit
|
231 |
-
|
232 |
-
gr.Markdown("""
|
233 |
-
|
234 |
-
|
235 |
-
including hate speech, violence, pornography, deception, etc. \
|
236 |
-
(注:本演示受Qwen2-VL的许可协议限制。我们强烈建议,用户不应传播及不应允许他人传播以下内容,\
|
237 |
-
包括但不限于仇恨言论、暴力、色情、欺诈相关的有害信息。)""")
|
238 |
-
|
239 |
-
demo.queue(api_open=False,default_concurrency_limit=40).launch(
|
240 |
share=args.share,
|
241 |
max_threads=40,
|
|
|
242 |
# inbrowser=args.inbrowser,
|
243 |
# server_port=args.server_port,
|
244 |
# server_name=args.server_name,
|
245 |
)
|
246 |
|
247 |
-
|
248 |
def main():
|
249 |
args = _get_args()
|
250 |
_launch_demo(args)
|
251 |
|
252 |
-
|
253 |
if __name__ == '__main__':
|
254 |
-
main()
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
import numpy as np
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
import re
|
4 |
import secrets
|
5 |
import tempfile
|
6 |
import requests
|
7 |
from http import HTTPStatus
|
8 |
+
from argparse import ArgumentParser
|
9 |
+
from pathlib import Path
|
10 |
+
|
11 |
+
import gradio as gr
|
12 |
+
import oss2
|
13 |
from dashscope import MultiModalConversation
|
14 |
import dashscope
|
15 |
+
|
16 |
+
# Install required packages
|
17 |
+
os.system('pip install dashscope modelscope oss2 -U')
|
18 |
+
|
19 |
+
# Load environment variables
|
20 |
API_KEY = os.environ['API_KEY']
|
21 |
ENDPOINT = os.environ['ENDPOINT']
|
22 |
AK_ID = os.environ['AK_ID']
|
23 |
AK = os.environ['AK']
|
24 |
BUCKET_NAME = os.environ['BUCKET_NAME']
|
25 |
+
|
26 |
dashscope.api_key = API_KEY
|
27 |
endpoint = ENDPOINT
|
28 |
auth = oss2.Auth(AK_ID, AK)
|
|
|
32 |
BOX_TAG_PATTERN = r"<box>([\s\S]*?)</box>"
|
33 |
PUNCTUATION = "!?。"#$%&'()*+,-/:;<=>@[\]^_`{|}~⦅⦆「」、、〃》「」『』【】〔〕〖〗〘〙〚〛〜〝〞〟〰〾〿–—‘’‛“”„‟…‧﹏."
|
34 |
|
|
|
35 |
def _get_args():
|
36 |
parser = ArgumentParser()
|
37 |
parser.add_argument("--revision", type=str, default=REVISION)
|
38 |
parser.add_argument("--cpu-only", action="store_true", help="Run demo with CPU only")
|
39 |
+
parser.add_argument("--share", action="store_true", default=False, help="Create a publicly shareable link for the interface.")
|
40 |
+
parser.add_argument("--inbrowser", action="store_true", default=False, help="Automatically launch the interface in a new tab on the default browser.")
|
41 |
+
parser.add_argument("--server-port", type=int, default=7860, help="Demo server port.")
|
42 |
+
parser.add_argument("--server-name", type=str, default="127.0.0.1", help="Demo server name.")
|
43 |
+
return parser.parse_args()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
def _parse_text(text):
|
46 |
lines = text.split("\n")
|
47 |
+
lines = [line for line in lines if line]
|
48 |
count = 0
|
49 |
for i, line in enumerate(lines):
|
50 |
if "```" in line:
|
|
|
53 |
if count % 2 == 1:
|
54 |
lines[i] = f'<pre><code class="language-{items[-1]}">'
|
55 |
else:
|
56 |
+
lines[i] = "<br></code></pre>"
|
57 |
else:
|
58 |
+
if i > 0 and count % 2 == 1:
|
59 |
+
line = re.sub(r'[<>]', lambda x: f'&{x.group(0)};', line)
|
60 |
+
line = line.replace(" ", " ")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
lines[i] = "<br>" + line
|
62 |
+
return "".join(lines)
|
|
|
|
|
63 |
|
64 |
def _remove_image_special(text):
|
65 |
text = text.replace('<ref>', '').replace('</ref>', '')
|
66 |
return re.sub(r'<box>.*?(</box>|$)', '', text)
|
67 |
|
|
|
68 |
def is_video_file(filename):
|
69 |
video_extensions = ['.mp4', '.avi', '.mkv', '.mov', '.wmv', '.flv', '.webm', '.mpeg']
|
70 |
return any(filename.lower().endswith(ext) for ext in video_extensions)
|
71 |
|
|
|
72 |
def _launch_demo(args):
|
73 |
+
uploaded_file_dir = os.environ.get("GRADIO_TEMP_DIR") or str(Path(tempfile.gettempdir()) / "gradio")
|
|
|
|
|
74 |
|
75 |
def predict(_chatbot, task_history):
|
76 |
chat_query = _chatbot[-1][0]
|
77 |
query = task_history[-1][0]
|
78 |
+
if not chat_query:
|
79 |
_chatbot.pop()
|
80 |
task_history.pop()
|
81 |
return _chatbot
|
|
|
99 |
messages.append({'role': 'assistant', 'content': [{'text': a}]})
|
100 |
content = []
|
101 |
messages.pop()
|
102 |
+
responses = MultiModalConversation.call(model='qwen-vl-max-0809', messages=messages, stream=True)
|
|
|
|
|
103 |
for response in responses:
|
104 |
+
if response.status_code != HTTPStatus.OK:
|
105 |
raise HTTPError(f'response.code: {response.code}\nresponse.message: {response.message}')
|
106 |
+
response_content = response.output.choices[0].message.content
|
107 |
+
response_text = [ele.get('text', '') for ele in response_content]
|
|
|
|
|
|
|
|
|
|
|
108 |
response_text = ''.join(response_text)
|
109 |
_chatbot[-1] = (_parse_text(chat_query), _remove_image_special(response_text))
|
110 |
yield _chatbot
|
111 |
+
if len(response_content) > 1:
|
112 |
+
result_image = response_content[-1]['result_image']
|
|
|
113 |
resp = requests.get(result_image)
|
114 |
os.makedirs(uploaded_file_dir, exist_ok=True)
|
115 |
+
filename = os.path.join(uploaded_file_dir, f"tmp{secrets.token_hex(20)}.jpg")
|
|
|
116 |
with open(filename, 'wb') as f:
|
117 |
f.write(resp.content)
|
118 |
+
response_text = ''.join(r.get('box', '') for r in response_content[:-1])
|
119 |
_chatbot.append((None, (filename,)))
|
120 |
else:
|
121 |
+
response_text = response_content[0]['text']
|
122 |
+
_chatbot[-1] = (_parse_text(chat_query), response_text)
|
123 |
+
full_response = _parse_text(response_text)
|
|
|
124 |
task_history[-1] = (query, full_response)
|
125 |
print("Qwen2-VL-Chat: " + _parse_text(full_response))
|
126 |
yield _chatbot
|
127 |
|
|
|
128 |
def regenerate(_chatbot, task_history):
|
129 |
if not task_history:
|
130 |
return _chatbot
|
|
|
143 |
|
144 |
def add_text(history, task_history, text):
|
145 |
task_text = text
|
146 |
+
history = history or []
|
147 |
+
task_history = task_history or []
|
148 |
+
history.append((_parse_text(text), None))
|
149 |
+
task_history.append((task_text, None))
|
150 |
return history, task_history, ""
|
151 |
|
152 |
def add_file(history, task_history, file):
|
153 |
+
history = history or []
|
154 |
+
task_history = task_history or []
|
155 |
+
history.append(((file.name,), None))
|
156 |
+
task_history.append(((file.name,), None))
|
157 |
return history, task_history
|
158 |
|
159 |
def reset_user_input():
|
|
|
164 |
return []
|
165 |
|
166 |
with gr.Blocks() as demo:
|
167 |
+
gr.Markdown("""<p align="center"><img src="https://modelscope.oss-cn-beijing.aliyuncs.com/resource/qwen.png" style="height: 80px"/></p>""")
|
|
|
168 |
gr.Markdown("""<center><font size=8>Qwen2-VL-72B</center>""")
|
169 |
+
gr.Markdown("""<center><font size=3>This WebUI is based on Qwen2-VL-72B, developed by Alibaba Cloud.</center>""")
|
|
|
|
|
170 |
gr.Markdown("""<center><font size=3>本WebUI基于Qwen2-VL-72B。</center>""")
|
171 |
|
172 |
chatbot = gr.Chatbot(label='Qwen2-VL-72B', elem_classes="control-height", height=500)
|
|
|
179 |
regen_btn = gr.Button("🤔️ Regenerate (重试)")
|
180 |
empty_bin = gr.Button("🧹 Clear History (清除历史)")
|
181 |
|
182 |
+
submit_btn.click(add_text, [chatbot, task_history, query], [chatbot, task_history], concurrency_limit=40).then(
|
183 |
predict, [chatbot, task_history], [chatbot], show_progress=True
|
184 |
)
|
185 |
+
submit_btn.click(reset_user_input, [], [query], concurrency_limit=40)
|
186 |
+
empty_bin.click(reset_state, [task_history], [chatbot], show_progress=True, concurrency_limit=40)
|
187 |
+
regen_btn.click(regenerate, [chatbot, task_history], [chatbot], show_progress=True, concurrency_limit=40)
|
188 |
+
addfile_btn.upload(add_file, [chatbot, task_history, addfile_btn], [chatbot, task_history], show_progress=True, concurrency_limit=40)
|
189 |
+
|
190 |
+
gr.Markdown("""<font size=2>Note: This demo is governed by the original license of Qwen2-VL. We strongly advise users not to knowingly generate or allow others to knowingly generate harmful content, including hate speech, violence, pornography, deception, etc. (注:本演示受Qwen2-VL的许可协议限制。我们强烈建议,用户不应传播及不应允许他人传播以下内容,包括但不限于仇恨言论、暴力、色情、欺诈相关的有害信息。)""")
|
191 |
+
|
192 |
+
demo.queue(api_open=False, default_concurrency_limit=40).launch(
|
|
|
|
|
|
|
|
|
|
|
193 |
share=args.share,
|
194 |
max_threads=40,
|
195 |
+
# Uncomment if needed:
|
196 |
# inbrowser=args.inbrowser,
|
197 |
# server_port=args.server_port,
|
198 |
# server_name=args.server_name,
|
199 |
)
|
200 |
|
|
|
201 |
def main():
|
202 |
args = _get_args()
|
203 |
_launch_demo(args)
|
204 |
|
|
|
205 |
if __name__ == '__main__':
|
206 |
+
main()
|