Spaces:
Running
Running
littlebird13
commited on
Commit
•
6cc94b6
1
Parent(s):
8313a0c
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
|
4 |
+
os.system('pip install dashscope -U')
|
5 |
+
import tempfile
|
6 |
+
from pathlib import Path
|
7 |
+
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(
|
20 |
+
Path(tempfile.gettempdir()) / "gradio"
|
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 |
+
# 调用qwen-vl-max-0809模型处理图片
|
32 |
+
messages = [{
|
33 |
+
'role': 'system',
|
34 |
+
'content': [{'text': 'You are a helpful assistant.'}]
|
35 |
+
}, {
|
36 |
+
'role': 'user',
|
37 |
+
'content': [
|
38 |
+
{'image': f'file://{filename}'},
|
39 |
+
{'text': 'Please describe the math-related content in this image, ensuring that any LaTeX formulas are correctly transcribed. Non-mathematical details do not need to be described.'}
|
40 |
+
]
|
41 |
+
}]
|
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"]
|
49 |
+
|
50 |
+
def get_math_response(image_description, user_question):
|
51 |
+
global math_messages
|
52 |
+
if not math_messages:
|
53 |
+
math_messages.append({'role': 'system', 'content': 'You are a helpful math assistant.'})
|
54 |
+
math_messages = math_messages[:1] + math_messages[1:][-4:]
|
55 |
+
if image_description is not None:
|
56 |
+
content = f'Image description: {image_description}\n\n'
|
57 |
+
else:
|
58 |
+
content = ''
|
59 |
+
query = f"{content}User question: {user_question}"
|
60 |
+
math_messages.append({'role': 'user', 'content': query})
|
61 |
+
response = Generation.call(
|
62 |
+
model="qwen2-math-72b-instruct",
|
63 |
+
messages=math_messages,
|
64 |
+
result_format='message',
|
65 |
+
stream=True
|
66 |
+
)
|
67 |
+
answer = None
|
68 |
+
for resp in response:
|
69 |
+
if resp.output is None:
|
70 |
+
continue
|
71 |
+
answer = resp.output.choices[0].message.content
|
72 |
+
yield answer.replace("\\", "\\\\")
|
73 |
+
print(f'query: {query}\nanswer: {answer}')
|
74 |
+
if answer is None:
|
75 |
+
math_messages.pop()
|
76 |
+
else:
|
77 |
+
math_messages.append({'role': 'assistant', 'content': answer})
|
78 |
+
|
79 |
+
|
80 |
+
def math_chat_bot(image, question):
|
81 |
+
if image is not None:
|
82 |
+
image_description = process_image(image)
|
83 |
+
else:
|
84 |
+
image_description = None
|
85 |
+
yield from get_math_response(image_description, question)
|
86 |
+
|
87 |
+
css = """
|
88 |
+
#qwen-md .katex-display { display: inline; }
|
89 |
+
#qwen-md .katex-display>.katex { display: inline; }
|
90 |
+
#qwen-md .katex-display>.katex>.katex-html { display: inline; }
|
91 |
+
"""
|
92 |
+
|
93 |
+
# 创建Gradio接口
|
94 |
+
iface = gr.Interface(
|
95 |
+
css=css,
|
96 |
+
fn=math_chat_bot,
|
97 |
+
inputs=[
|
98 |
+
gr.Image(type="pil", label="upload image"),
|
99 |
+
gr.Textbox(label="input your question")
|
100 |
+
],
|
101 |
+
outputs=gr.Markdown(label="answer", latex_delimiters=[
|
102 |
+
{"left": "\\(", "right": "\\)", "display": True},
|
103 |
+
{"left": "\\begin\{equation\}", "right": "\\end\{equation\}", "display": True},
|
104 |
+
{"left": "\\begin\{align\}", "right": "\\end\{align\}", "display": True},
|
105 |
+
{"left": "\\begin\{alignat\}", "right": "\\end\{alignat\}", "display": True},
|
106 |
+
{"left": "\\begin\{gather\}", "right": "\\end\{gather\}", "display": True},
|
107 |
+
{"left": "\\begin\{CD\}", "right": "\\end\{CD\}", "display": True},
|
108 |
+
{"left": "\\[", "right": "\\]", "display": True}
|
109 |
+
], elem_id="qwen-md"),
|
110 |
+
title="Qwen2-math demo",
|
111 |
+
allow_flagging='never',
|
112 |
+
description="upload a math question image and ask your question.(Supports only formulas and text, not geometry etc.)"
|
113 |
+
)
|
114 |
+
|
115 |
+
# 启动Gradio应用
|
116 |
+
iface.launch()
|