Spaces:
Build error
Build error
Zenith Wang
commited on
Commit
·
284099f
1
Parent(s):
64b282a
Revert to stable Gradio 4.19.2 and redesign interface without MultimodalTextbox
Browse files- README.md +1 -1
- app.py +92 -66
- requirements.txt +1 -1
README.md
CHANGED
@@ -4,7 +4,7 @@ emoji: 🤖
|
|
4 |
colorFrom: purple
|
5 |
colorTo: blue
|
6 |
sdk: gradio
|
7 |
-
sdk_version: 4.
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
license: mit
|
|
|
4 |
colorFrom: purple
|
5 |
colorTo: blue
|
6 |
sdk: gradio
|
7 |
+
sdk_version: 4.19.2
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
license: mit
|
app.py
CHANGED
@@ -22,6 +22,10 @@ def image_to_base64(image):
|
|
22 |
image.save(buffered, format="PNG")
|
23 |
img_str = base64.b64encode(buffered.getvalue()).decode('utf-8')
|
24 |
return img_str
|
|
|
|
|
|
|
|
|
25 |
|
26 |
return None
|
27 |
|
@@ -40,59 +44,36 @@ def extract_cot_and_answer(text):
|
|
40 |
# 如果没有reasoning标签,整个响应就是答案
|
41 |
return "", text
|
42 |
|
43 |
-
def
|
44 |
-
"""
|
45 |
-
|
46 |
-
# 创建包含图片和文本的消息
|
47 |
-
return f'<img src="{image_path}" style="max-width: 200px; max-height: 200px; border-radius: 8px; margin-bottom: 10px;"><br>{message_text}'
|
48 |
-
return message_text
|
49 |
-
|
50 |
-
def call_step_api_stream(message, history):
|
51 |
-
"""调用Step API进行流式对话,支持多模态输入"""
|
52 |
-
print(f"[DEBUG] Starting API call - Message type: {type(message)}")
|
53 |
|
54 |
-
if not message:
|
55 |
-
print("[DEBUG] No message provided")
|
56 |
yield history, "", ""
|
57 |
return
|
58 |
|
59 |
if not STEP_API_KEY:
|
60 |
print("[DEBUG] API key not configured")
|
61 |
error_msg = "❌ API key not configured. Please add STEP_API_KEY in Settings."
|
62 |
-
history.append([message
|
63 |
yield history, "", ""
|
64 |
return
|
65 |
|
66 |
print(f"[DEBUG] API Key exists: {bool(STEP_API_KEY)}")
|
67 |
|
68 |
-
#
|
69 |
-
|
70 |
image_content = None
|
71 |
-
display_message = ""
|
72 |
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
# 处理图片文件
|
79 |
-
if files and len(files) > 0:
|
80 |
-
image_path = files[0] # 取第一张图片
|
81 |
-
try:
|
82 |
-
img = Image.open(image_path)
|
83 |
-
image_content = image_to_base64(img)
|
84 |
-
# 创建显示消息,包含图片缩略图
|
85 |
-
display_message = format_message_with_image(text_content, image_path)
|
86 |
print(f"[DEBUG] Image processed successfully")
|
87 |
-
|
88 |
-
|
89 |
-
display_message = text_content
|
90 |
-
else:
|
91 |
-
display_message = text_content
|
92 |
-
else:
|
93 |
-
# 纯文本消息
|
94 |
-
text_content = str(message)
|
95 |
-
display_message = text_content
|
96 |
|
97 |
# 添加用户消息到历史
|
98 |
history.append([display_message, ""])
|
@@ -101,14 +82,22 @@ def call_step_api_stream(message, history):
|
|
101 |
# 构造API消息
|
102 |
messages = []
|
103 |
|
104 |
-
#
|
105 |
for h in history[:-1]: # 不包含当前消息
|
106 |
if h[0]: # 用户消息
|
107 |
-
#
|
108 |
-
user_text =
|
109 |
-
|
110 |
-
|
111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
112 |
|
113 |
# 构造当前消息
|
114 |
if image_content:
|
@@ -116,12 +105,13 @@ def call_step_api_stream(message, history):
|
|
116 |
current_content = [
|
117 |
{"type": "image_url", "image_url": {"url": f"data:image/jpg;base64,{image_content}", "detail": "high"}}
|
118 |
]
|
119 |
-
if
|
120 |
-
current_content.append({"type": "text", "text":
|
121 |
messages.append({"role": "user", "content": current_content})
|
122 |
else:
|
123 |
# 纯文本
|
124 |
-
|
|
|
125 |
|
126 |
print(f"[DEBUG] Messages count: {len(messages)}")
|
127 |
|
@@ -174,7 +164,8 @@ def call_step_api_stream(message, history):
|
|
174 |
# 没有CoT,直接显示答案
|
175 |
history[-1][1] = current_answer
|
176 |
|
177 |
-
|
|
|
178 |
yield history, current_cot, current_answer
|
179 |
|
180 |
if not full_response:
|
@@ -183,6 +174,8 @@ def call_step_api_stream(message, history):
|
|
183 |
yield history, "", ""
|
184 |
else:
|
185 |
print(f"[DEBUG] Final response length: {len(full_response)} chars")
|
|
|
|
|
186 |
|
187 |
except Exception as e:
|
188 |
print(f"[DEBUG] API request failed: {e}")
|
@@ -191,9 +184,9 @@ def call_step_api_stream(message, history):
|
|
191 |
history[-1][1] = f"❌ API request failed: {str(e)}"
|
192 |
yield history, "", ""
|
193 |
|
194 |
-
def
|
195 |
-
"""Clear
|
196 |
-
return [], None
|
197 |
|
198 |
# 创建Gradio界面
|
199 |
with gr.Blocks(title="Step-3", theme=gr.themes.Soft()) as demo:
|
@@ -206,23 +199,34 @@ with gr.Blocks(title="Step-3", theme=gr.themes.Soft()) as demo:
|
|
206 |
with gr.Column(scale=2):
|
207 |
# 对话界面
|
208 |
chatbot = gr.Chatbot(
|
209 |
-
height=
|
210 |
show_label=False,
|
211 |
elem_id="chatbot",
|
212 |
-
bubble_full_width=False
|
213 |
-
render_markdown=True
|
214 |
)
|
215 |
|
216 |
with gr.Row():
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
226 |
|
227 |
with gr.Column(scale=1):
|
228 |
# CoT推理过程展示
|
@@ -247,16 +251,38 @@ with gr.Blocks(title="Step-3", theme=gr.themes.Soft()) as demo:
|
|
247 |
)
|
248 |
|
249 |
# 事件处理
|
|
|
|
|
|
|
|
|
|
|
|
|
250 |
msg.submit(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
251 |
call_step_api_stream,
|
252 |
-
[msg, chatbot],
|
253 |
[chatbot, cot_display, answer_display]
|
254 |
)
|
255 |
|
256 |
clear_btn.click(
|
257 |
-
|
258 |
None,
|
259 |
-
[chatbot, msg]
|
260 |
)
|
261 |
|
262 |
# 页脚
|
|
|
22 |
image.save(buffered, format="PNG")
|
23 |
img_str = base64.b64encode(buffered.getvalue()).decode('utf-8')
|
24 |
return img_str
|
25 |
+
elif isinstance(image, str) and os.path.exists(image):
|
26 |
+
# 如果是文件路径
|
27 |
+
with open(image, "rb") as image_file:
|
28 |
+
return base64.b64encode(image_file.read()).decode('utf-8')
|
29 |
|
30 |
return None
|
31 |
|
|
|
44 |
# 如果没有reasoning标签,整个响应就是答案
|
45 |
return "", text
|
46 |
|
47 |
+
def call_step_api_stream(message, history, image=None):
|
48 |
+
"""调用Step API进行流式对话"""
|
49 |
+
print(f"[DEBUG] Starting API call - Message: {message}, Has Image: {image is not None}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
|
51 |
+
if not message and not image:
|
52 |
+
print("[DEBUG] No message or image provided")
|
53 |
yield history, "", ""
|
54 |
return
|
55 |
|
56 |
if not STEP_API_KEY:
|
57 |
print("[DEBUG] API key not configured")
|
58 |
error_msg = "❌ API key not configured. Please add STEP_API_KEY in Settings."
|
59 |
+
history.append([message or "[Image]", error_msg])
|
60 |
yield history, "", ""
|
61 |
return
|
62 |
|
63 |
print(f"[DEBUG] API Key exists: {bool(STEP_API_KEY)}")
|
64 |
|
65 |
+
# 处理消息和图片
|
66 |
+
display_message = message or ""
|
67 |
image_content = None
|
|
|
68 |
|
69 |
+
if image:
|
70 |
+
try:
|
71 |
+
image_content = image_to_base64(image)
|
72 |
+
if image_content:
|
73 |
+
display_message = f"[Image uploaded] {message}" if message else "[Image uploaded]"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
print(f"[DEBUG] Image processed successfully")
|
75 |
+
except Exception as e:
|
76 |
+
print(f"[DEBUG] Failed to process image: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
|
78 |
# 添加用户消息到历史
|
79 |
history.append([display_message, ""])
|
|
|
82 |
# 构造API消息
|
83 |
messages = []
|
84 |
|
85 |
+
# 添加历史对话(只保留文本,不包含标记)
|
86 |
for h in history[:-1]: # 不包含当前消息
|
87 |
if h[0]: # 用户消息
|
88 |
+
# 移除[Image uploaded]标记
|
89 |
+
user_text = h[0].replace("[Image uploaded] ", "").replace("[Image uploaded]", "")
|
90 |
+
if user_text:
|
91 |
+
messages.append({"role": "user", "content": user_text})
|
92 |
+
if h[1] and not h[1].startswith("❌"): # 助手回复(排除错误消息)
|
93 |
+
# 提取纯文本内容
|
94 |
+
assistant_text = h[1]
|
95 |
+
# 如果包含格式化的CoT和Answer,提取完整内容
|
96 |
+
if "**Reasoning Process:**" in assistant_text:
|
97 |
+
# 移除格式化标记,保留原始内容
|
98 |
+
assistant_text = re.sub(r'\*\*.*?\*\*', '', assistant_text)
|
99 |
+
assistant_text = assistant_text.replace("💭", "").replace("📝", "").replace("---", "").strip()
|
100 |
+
messages.append({"role": "assistant", "content": assistant_text})
|
101 |
|
102 |
# 构造当前消息
|
103 |
if image_content:
|
|
|
105 |
current_content = [
|
106 |
{"type": "image_url", "image_url": {"url": f"data:image/jpg;base64,{image_content}", "detail": "high"}}
|
107 |
]
|
108 |
+
if message:
|
109 |
+
current_content.append({"type": "text", "text": message})
|
110 |
messages.append({"role": "user", "content": current_content})
|
111 |
else:
|
112 |
# 纯文本
|
113 |
+
if message:
|
114 |
+
messages.append({"role": "user", "content": message})
|
115 |
|
116 |
print(f"[DEBUG] Messages count: {len(messages)}")
|
117 |
|
|
|
164 |
# 没有CoT,直接显示答案
|
165 |
history[-1][1] = current_answer
|
166 |
|
167 |
+
if chunk_count % 5 == 0: # 每5个chunk更新一次,减少更新频率
|
168 |
+
print(f"[DEBUG] Processed {chunk_count} chunks")
|
169 |
yield history, current_cot, current_answer
|
170 |
|
171 |
if not full_response:
|
|
|
174 |
yield history, "", ""
|
175 |
else:
|
176 |
print(f"[DEBUG] Final response length: {len(full_response)} chars")
|
177 |
+
# 最终更新
|
178 |
+
yield history, current_cot, current_answer
|
179 |
|
180 |
except Exception as e:
|
181 |
print(f"[DEBUG] API request failed: {e}")
|
|
|
184 |
history[-1][1] = f"❌ API request failed: {str(e)}"
|
185 |
yield history, "", ""
|
186 |
|
187 |
+
def clear_all():
|
188 |
+
"""Clear all components"""
|
189 |
+
return [], None, "", "", ""
|
190 |
|
191 |
# 创建Gradio界面
|
192 |
with gr.Blocks(title="Step-3", theme=gr.themes.Soft()) as demo:
|
|
|
199 |
with gr.Column(scale=2):
|
200 |
# 对话界面
|
201 |
chatbot = gr.Chatbot(
|
202 |
+
height=500,
|
203 |
show_label=False,
|
204 |
elem_id="chatbot",
|
205 |
+
bubble_full_width=False
|
|
|
206 |
)
|
207 |
|
208 |
with gr.Row():
|
209 |
+
with gr.Column(scale=6):
|
210 |
+
# 文本输入框
|
211 |
+
msg = gr.Textbox(
|
212 |
+
placeholder="Type your message here...",
|
213 |
+
show_label=False,
|
214 |
+
lines=2,
|
215 |
+
max_lines=4,
|
216 |
+
container=False,
|
217 |
+
elem_id="msg"
|
218 |
+
)
|
219 |
+
with gr.Column(scale=2):
|
220 |
+
# 图片上传
|
221 |
+
image_input = gr.Image(
|
222 |
+
label="Upload Image",
|
223 |
+
type="filepath",
|
224 |
+
height=80,
|
225 |
+
scale=1
|
226 |
+
)
|
227 |
+
with gr.Column(scale=1):
|
228 |
+
send_btn = gr.Button("Send", variant="primary", scale=1)
|
229 |
+
clear_btn = gr.Button("Clear", scale=1)
|
230 |
|
231 |
with gr.Column(scale=1):
|
232 |
# CoT推理过程展示
|
|
|
251 |
)
|
252 |
|
253 |
# 事件处理
|
254 |
+
def on_submit(message, history, image):
|
255 |
+
if message or image:
|
256 |
+
return "", history, None
|
257 |
+
return message, history, image
|
258 |
+
|
259 |
+
# 提交消息
|
260 |
msg.submit(
|
261 |
+
on_submit,
|
262 |
+
[msg, chatbot, image_input],
|
263 |
+
[msg, chatbot, image_input],
|
264 |
+
queue=False
|
265 |
+
).then(
|
266 |
+
call_step_api_stream,
|
267 |
+
[msg, chatbot, image_input],
|
268 |
+
[chatbot, cot_display, answer_display]
|
269 |
+
)
|
270 |
+
|
271 |
+
send_btn.click(
|
272 |
+
on_submit,
|
273 |
+
[msg, chatbot, image_input],
|
274 |
+
[msg, chatbot, image_input],
|
275 |
+
queue=False
|
276 |
+
).then(
|
277 |
call_step_api_stream,
|
278 |
+
[msg, chatbot, image_input],
|
279 |
[chatbot, cot_display, answer_display]
|
280 |
)
|
281 |
|
282 |
clear_btn.click(
|
283 |
+
clear_all,
|
284 |
None,
|
285 |
+
[chatbot, image_input, msg, cot_display, answer_display]
|
286 |
)
|
287 |
|
288 |
# 页脚
|
requirements.txt
CHANGED
@@ -1,3 +1,3 @@
|
|
1 |
-
gradio==4.
|
2 |
openai==1.12.0
|
3 |
Pillow==10.2.0
|
|
|
1 |
+
gradio==4.19.2
|
2 |
openai==1.12.0
|
3 |
Pillow==10.2.0
|