Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,12 +8,10 @@ import secrets
|
|
| 8 |
import dashscope
|
| 9 |
from dashscope import MultiModalConversation, Generation
|
| 10 |
|
| 11 |
-
# 设置API密钥
|
| 12 |
YOUR_API_TOKEN = os.getenv('YOUR_API_TOKEN')
|
| 13 |
dashscope.api_key = YOUR_API_TOKEN
|
| 14 |
math_messages = []
|
| 15 |
def process_image(image):
|
| 16 |
-
# 获取上传文件的目录
|
| 17 |
global math_messages
|
| 18 |
math_messages = [] # reset when upload image
|
| 19 |
uploaded_file_dir = os.environ.get("GRADIO_TEMP_DIR") or str(
|
|
@@ -21,14 +19,12 @@ def process_image(image):
|
|
| 21 |
)
|
| 22 |
os.makedirs(uploaded_file_dir, exist_ok=True)
|
| 23 |
|
| 24 |
-
# 创建临时文件路径
|
| 25 |
name = f"tmp{secrets.token_hex(20)}.jpg"
|
| 26 |
filename = os.path.join(uploaded_file_dir, name)
|
| 27 |
-
# 保存上传的图片
|
| 28 |
image.save(filename)
|
| 29 |
|
| 30 |
|
| 31 |
-
#
|
| 32 |
messages = [{
|
| 33 |
'role': 'system',
|
| 34 |
'content': [{'text': 'You are a helpful assistant.'}]
|
|
@@ -42,7 +38,6 @@ def process_image(image):
|
|
| 42 |
|
| 43 |
response = MultiModalConversation.call(model='qwen-vl-max-0809', messages=messages)
|
| 44 |
|
| 45 |
-
# 清理临时文件
|
| 46 |
os.remove(filename)
|
| 47 |
|
| 48 |
return response.output.choices[0]["message"]["content"]
|
|
@@ -90,7 +85,7 @@ css = """
|
|
| 90 |
#qwen-md .katex-display>.katex>.katex-html { display: inline; }
|
| 91 |
"""
|
| 92 |
|
| 93 |
-
#
|
| 94 |
iface = gr.Interface(
|
| 95 |
css=css,
|
| 96 |
fn=math_chat_bot,
|
|
@@ -107,10 +102,13 @@ iface = gr.Interface(
|
|
| 107 |
{"left": "\\begin\{CD\}", "right": "\\end\{CD\}", "display": True},
|
| 108 |
{"left": "\\[", "right": "\\]", "display": True}
|
| 109 |
], elem_id="qwen-md"),
|
| 110 |
-
title="Qwen2
|
| 111 |
allow_flagging='never',
|
| 112 |
-
description=
|
|
|
|
|
|
|
|
|
|
| 113 |
)
|
| 114 |
|
| 115 |
-
#
|
| 116 |
iface.launch()
|
|
|
|
| 8 |
import dashscope
|
| 9 |
from dashscope import MultiModalConversation, Generation
|
| 10 |
|
|
|
|
| 11 |
YOUR_API_TOKEN = os.getenv('YOUR_API_TOKEN')
|
| 12 |
dashscope.api_key = YOUR_API_TOKEN
|
| 13 |
math_messages = []
|
| 14 |
def process_image(image):
|
|
|
|
| 15 |
global math_messages
|
| 16 |
math_messages = [] # reset when upload image
|
| 17 |
uploaded_file_dir = os.environ.get("GRADIO_TEMP_DIR") or str(
|
|
|
|
| 19 |
)
|
| 20 |
os.makedirs(uploaded_file_dir, exist_ok=True)
|
| 21 |
|
|
|
|
| 22 |
name = f"tmp{secrets.token_hex(20)}.jpg"
|
| 23 |
filename = os.path.join(uploaded_file_dir, name)
|
|
|
|
| 24 |
image.save(filename)
|
| 25 |
|
| 26 |
|
| 27 |
+
# Use qwen-vl-max-0809 for OCR
|
| 28 |
messages = [{
|
| 29 |
'role': 'system',
|
| 30 |
'content': [{'text': 'You are a helpful assistant.'}]
|
|
|
|
| 38 |
|
| 39 |
response = MultiModalConversation.call(model='qwen-vl-max-0809', messages=messages)
|
| 40 |
|
|
|
|
| 41 |
os.remove(filename)
|
| 42 |
|
| 43 |
return response.output.choices[0]["message"]["content"]
|
|
|
|
| 85 |
#qwen-md .katex-display>.katex>.katex-html { display: inline; }
|
| 86 |
"""
|
| 87 |
|
| 88 |
+
# Crate interface
|
| 89 |
iface = gr.Interface(
|
| 90 |
css=css,
|
| 91 |
fn=math_chat_bot,
|
|
|
|
| 102 |
{"left": "\\begin\{CD\}", "right": "\\end\{CD\}", "display": True},
|
| 103 |
{"left": "\\[", "right": "\\]", "display": True}
|
| 104 |
], elem_id="qwen-md"),
|
| 105 |
+
title=gr.Markdown("""<center><font size=8>📖 Qwen2 Math Demo</center>"""),
|
| 106 |
allow_flagging='never',
|
| 107 |
+
description=gr.Markdown(
|
| 108 |
+
"""\
|
| 109 |
+
<center><font size=3>This WebUI is based on Qwen2-VL for OCR and Qwen2-Math for mathematical reasoning. You can input either images or texts of mathematical or arithmetic problems.</center><br>\
|
| 110 |
+
""")
|
| 111 |
)
|
| 112 |
|
| 113 |
+
# Launch gradio application
|
| 114 |
iface.launch()
|