Spaces:
Running
Running
Muratbeser
commited on
Commit
•
62a1cd3
1
Parent(s):
639ac95
ws
Browse files
app.py
CHANGED
@@ -1,100 +1,137 @@
|
|
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 |
from PIL import Image
|
|
|
|
|
11 |
|
12 |
-
|
13 |
-
# 设置API密钥
|
14 |
YOUR_API_TOKEN = os.getenv('YOUR_API_TOKEN')
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
)
|
24 |
os.makedirs(uploaded_file_dir, exist_ok=True)
|
25 |
-
|
26 |
-
#
|
27 |
name = f"tmp{secrets.token_hex(20)}.jpg"
|
28 |
filename = os.path.join(uploaded_file_dir, name)
|
29 |
-
|
30 |
-
|
|
|
31 |
new_img = Image.new('RGB', size=(image.width, image.height), color=(255, 255, 255))
|
32 |
new_img.paste(image, (0, 0), mask=image)
|
33 |
image = new_img
|
|
|
|
|
34 |
image.save(filename)
|
35 |
-
|
36 |
-
#
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
}
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
]
|
46 |
-
}
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
|
99 |
css = """
|
100 |
#qwen-md .katex-display { display: inline; }
|
@@ -102,68 +139,30 @@ css = """
|
|
102 |
#qwen-md .katex-display>.katex>.katex-html { display: inline; }
|
103 |
"""
|
104 |
|
105 |
-
|
106 |
-
_state["tab_index"] = e.index
|
107 |
-
|
108 |
-
|
109 |
-
# 创建Gradio接口
|
110 |
with gr.Blocks(css=css) as demo:
|
111 |
gr.HTML("""\
|
112 |
-
<p align="center"><img src="https://modelscope.oss-cn-beijing.aliyuncs.com/resource/qwen.png" style="height: 60px"
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
state = gr.State({"tab_index": 0})
|
118 |
with gr.Row():
|
119 |
with gr.Column():
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
with gr.Tab("Sketch"):
|
124 |
-
input_sketchpad = gr.Sketchpad(type="pil", label="Sketch", layers=False)
|
125 |
-
input_tabs.select(fn=tabs_select, inputs=[state])
|
126 |
-
input_text = gr.Textbox(label="input your question")
|
127 |
with gr.Row():
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
with gr.Column():
|
132 |
-
submit_btn = gr.Button("Submit", variant="primary")
|
133 |
with gr.Column():
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
}, {
|
144 |
-
"left": "\\begin\{align\}",
|
145 |
-
"right": "\\end\{align\}",
|
146 |
-
"display": True
|
147 |
-
}, {
|
148 |
-
"left": "\\begin\{alignat\}",
|
149 |
-
"right": "\\end\{alignat\}",
|
150 |
-
"display": True
|
151 |
-
}, {
|
152 |
-
"left": "\\begin\{gather\}",
|
153 |
-
"right": "\\end\{gather\}",
|
154 |
-
"display": True
|
155 |
-
}, {
|
156 |
-
"left": "\\begin\{CD\}",
|
157 |
-
"right": "\\end\{CD\}",
|
158 |
-
"display": True
|
159 |
-
}, {
|
160 |
-
"left": "\\[",
|
161 |
-
"right": "\\]",
|
162 |
-
"display": True
|
163 |
-
}],
|
164 |
-
elem_id="qwen-md")
|
165 |
-
submit_btn.click(
|
166 |
-
fn=math_chat_bot,
|
167 |
-
inputs=[*input_image, input_sketchpad, input_text, state],
|
168 |
-
outputs=output_md)
|
169 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import os
|
|
|
|
|
3 |
import tempfile
|
4 |
from pathlib import Path
|
5 |
import secrets
|
|
|
|
|
6 |
from PIL import Image
|
7 |
+
import requests
|
8 |
+
import json
|
9 |
|
10 |
+
# API key setup
|
|
|
11 |
YOUR_API_TOKEN = os.getenv('YOUR_API_TOKEN')
|
12 |
+
|
13 |
+
# API Endpoints (Gerçek API URL'lerinizi buraya ekleyin)
|
14 |
+
IMAGE_DESCRIPTION_API_ENDPOINT = "https://api.actual-qwen-model.com/v1/image-description"
|
15 |
+
MATH_RESPONSE_API_ENDPOINT = "https://api.actual-qwen-model.com/v1/math-response"
|
16 |
+
|
17 |
+
def process_image(image, should_convert=False):
|
18 |
+
# Geçici dizin oluşturma
|
19 |
+
uploaded_file_dir = os.environ.get("GRADIO_TEMP_DIR") or str(Path(tempfile.gettempdir()) / "gradio")
|
|
|
20 |
os.makedirs(uploaded_file_dir, exist_ok=True)
|
21 |
+
|
22 |
+
# Geçici dosya adı oluşturma
|
23 |
name = f"tmp{secrets.token_hex(20)}.jpg"
|
24 |
filename = os.path.join(uploaded_file_dir, name)
|
25 |
+
|
26 |
+
# İsteğe bağlı olarak resmi dönüştürme
|
27 |
+
if should_convert:
|
28 |
new_img = Image.new('RGB', size=(image.width, image.height), color=(255, 255, 255))
|
29 |
new_img.paste(image, (0, 0), mask=image)
|
30 |
image = new_img
|
31 |
+
|
32 |
+
# Resmi kaydetme
|
33 |
image.save(filename)
|
34 |
+
|
35 |
+
# API isteği hazırlama
|
36 |
+
headers = {
|
37 |
+
"Authorization": f"Bearer {YOUR_API_TOKEN}",
|
38 |
+
"Content-Type": "application/json"
|
39 |
+
}
|
40 |
+
data = {
|
41 |
+
"model": "qwen-vl-max-0809",
|
42 |
+
"messages": [
|
43 |
+
{
|
44 |
+
'role': 'system',
|
45 |
+
'content': [{'text': 'You are a helpful assistant.'}]
|
46 |
+
},
|
47 |
+
{
|
48 |
+
'role': 'user',
|
49 |
+
'content': [
|
50 |
+
{'image': f'file://{filename}'},
|
51 |
+
{'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.'}
|
52 |
+
]
|
53 |
+
}
|
54 |
]
|
55 |
+
}
|
56 |
+
|
57 |
+
try:
|
58 |
+
# API isteğini gönderme
|
59 |
+
response = requests.post(IMAGE_DESCRIPTION_API_ENDPOINT, headers=headers, json=data)
|
60 |
+
response.raise_for_status()
|
61 |
+
response_data = response.json()
|
62 |
+
|
63 |
+
# Açıklamayı çıkarma
|
64 |
+
description = response_data["output"]["choices"][0]["message"]["content"]
|
65 |
+
|
66 |
+
except Exception as e:
|
67 |
+
description = f"Error processing image: {str(e)}"
|
68 |
+
|
69 |
+
finally:
|
70 |
+
# Geçici dosyayı silme
|
71 |
+
os.remove(filename)
|
72 |
+
|
73 |
+
return description
|
74 |
+
|
75 |
+
def get_math_response(image_descriptions, user_question):
|
76 |
+
if not image_descriptions:
|
77 |
+
return "No image descriptions provided."
|
78 |
+
|
79 |
+
# API isteği hazırlama
|
80 |
+
headers = {
|
81 |
+
"Authorization": f"Bearer {YOUR_API_TOKEN}",
|
82 |
+
"Content-Type": "application/json"
|
83 |
+
}
|
84 |
+
content = "Image descriptions:\n" + "\n".join(image_descriptions) + f"\n\nUser question: {user_question}"
|
85 |
+
data = {
|
86 |
+
"model": "qwen2.5-math-72b-instruct",
|
87 |
+
"messages": [
|
88 |
+
{'role': 'system', 'content': 'You are a helpful math assistant.'},
|
89 |
+
{'role': 'user', 'content': content}
|
90 |
+
],
|
91 |
+
"result_format": "message",
|
92 |
+
"stream": False # Streaming'i kapattık
|
93 |
+
}
|
94 |
+
|
95 |
+
try:
|
96 |
+
# API isteğini gönderme
|
97 |
+
response = requests.post(MATH_RESPONSE_API_ENDPOINT, headers=headers, json=data)
|
98 |
+
response.raise_for_status()
|
99 |
+
response_data = response.json()
|
100 |
+
|
101 |
+
# Yanıtı çıkarma
|
102 |
+
answer = response_data["output"]["choices"][0]["message"]["content"]
|
103 |
+
|
104 |
+
except Exception as e:
|
105 |
+
answer = f"Error generating response: {str(e)}"
|
106 |
+
|
107 |
+
return answer
|
108 |
+
|
109 |
+
def math_chat_bot(images, sketchpad, question, chat_history):
|
110 |
+
image_descriptions = []
|
111 |
+
|
112 |
+
# Yeni resimleri işleme
|
113 |
+
if images is not None:
|
114 |
+
for image in images:
|
115 |
+
if image:
|
116 |
+
img = Image.open(image.name) if hasattr(image, 'name') else image
|
117 |
+
description = process_image(img)
|
118 |
+
image_descriptions.append(description)
|
119 |
+
|
120 |
+
# Sketchpad varsa işle
|
121 |
+
if sketchpad and sketchpad["composite"]:
|
122 |
+
sketch_image = sketchpad["composite"]
|
123 |
+
sketch_description = process_image(sketch_image, should_convert=True)
|
124 |
+
image_descriptions.append(sketch_description)
|
125 |
+
|
126 |
+
# Matematik yanıtını al
|
127 |
+
math_response = get_math_response(image_descriptions, question)
|
128 |
+
|
129 |
+
# Sohbet geçmişine ekleme
|
130 |
+
if chat_history is None:
|
131 |
+
chat_history = []
|
132 |
+
chat_history.append((question, math_response))
|
133 |
+
|
134 |
+
return chat_history
|
135 |
|
136 |
css = """
|
137 |
#qwen-md .katex-display { display: inline; }
|
|
|
139 |
#qwen-md .katex-display>.katex>.katex-html { display: inline; }
|
140 |
"""
|
141 |
|
142 |
+
# Gradio arayüzünü oluşturma
|
|
|
|
|
|
|
|
|
143 |
with gr.Blocks(css=css) as demo:
|
144 |
gr.HTML("""\
|
145 |
+
<p align="center"><img src="https://modelscope.oss-cn-beijing.aliyuncs.com/resource/qwen.png" style="height: 60px"/></p>
|
146 |
+
<center><font size=8>📖 Qwen2.5-Math Demo</font></center>
|
147 |
+
<center><font size=3>This WebUI is based on Qwen2-VL for OCR and Qwen2.5-Math for mathematical reasoning. You can input either images or texts of mathematical or arithmetic problems.</font></center>
|
148 |
+
""")
|
149 |
+
|
|
|
150 |
with gr.Row():
|
151 |
with gr.Column():
|
152 |
+
input_images = gr.File(file_count="multiple", label="Upload Images")
|
153 |
+
input_sketchpad = gr.Sketchpad(type="pil", label="Sketch", layers=False)
|
154 |
+
input_text = gr.Textbox(label="Input your question")
|
|
|
|
|
|
|
|
|
155 |
with gr.Row():
|
156 |
+
clear_btn = gr.ClearButton([input_images, input_sketchpad, input_text])
|
157 |
+
submit_btn = gr.Button("Submit", variant="primary")
|
158 |
+
|
|
|
|
|
159 |
with gr.Column():
|
160 |
+
chat_output = gr.Chatbot(label="Chat History", elem_id="qwen-md")
|
161 |
+
|
162 |
+
submit_btn.click(
|
163 |
+
fn=math_chat_bot,
|
164 |
+
inputs=[input_images, input_sketchpad, input_text, chat_output],
|
165 |
+
outputs=chat_output
|
166 |
+
)
|
167 |
+
|
168 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|