Spaces:
Running
Running
Zenith Wang
commited on
Commit
·
ac8b8e5
1
Parent(s):
762ea56
Add optional image upload support with better UI layout
Browse files
app.py
CHANGED
|
@@ -27,45 +27,49 @@ def image_to_base64(image):
|
|
| 27 |
|
| 28 |
return None
|
| 29 |
|
| 30 |
-
def process_message(message, history, system_prompt, temperature, max_tokens, top_p):
|
| 31 |
-
"""
|
| 32 |
print(f"[DEBUG] Processing message: {message[:100] if message else 'None'}...")
|
|
|
|
| 33 |
|
| 34 |
-
if not message:
|
| 35 |
-
print("[DEBUG] No message provided, skipping")
|
| 36 |
yield history
|
| 37 |
return
|
| 38 |
|
| 39 |
if not STEP_API_KEY:
|
| 40 |
print("[DEBUG] No API key configured")
|
| 41 |
-
|
|
|
|
|
|
|
| 42 |
yield history
|
| 43 |
return
|
| 44 |
|
| 45 |
-
#
|
| 46 |
-
image_pattern = r'<img[^>]+src="([^">]+)"'
|
| 47 |
-
image_match = re.search(image_pattern, message)
|
| 48 |
image_content = None
|
| 49 |
-
text_content = message
|
| 50 |
|
| 51 |
-
if
|
| 52 |
-
# 提取图片路径
|
| 53 |
-
image_path = image_match.group(1)
|
| 54 |
-
text_content = re.sub(image_pattern, '', message).strip()
|
| 55 |
-
|
| 56 |
# 转换图片为base64
|
| 57 |
try:
|
| 58 |
-
|
| 59 |
-
# 已经是base64格式
|
| 60 |
-
image_content = image_path.split(',')[1]
|
| 61 |
-
else:
|
| 62 |
-
image_content = image_to_base64(image_path)
|
| 63 |
print(f"[DEBUG] Image processed successfully")
|
| 64 |
except Exception as e:
|
| 65 |
print(f"[DEBUG] Failed to process image: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
# 添加到历史
|
| 68 |
-
history.append([
|
| 69 |
yield history
|
| 70 |
|
| 71 |
# 构建API消息
|
|
@@ -240,15 +244,23 @@ with gr.Blocks(title="Step-3 Chat", theme=gr.themes.Soft()) as demo:
|
|
| 240 |
|
| 241 |
# 输入区域
|
| 242 |
with gr.Row():
|
| 243 |
-
with gr.Column(scale=
|
| 244 |
msg = gr.Textbox(
|
| 245 |
label="Message",
|
| 246 |
-
placeholder="Type your message here...
|
| 247 |
lines=2,
|
| 248 |
max_lines=10,
|
| 249 |
show_label=False,
|
| 250 |
elem_id="message-textbox"
|
| 251 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 252 |
with gr.Column(scale=1, min_width=100):
|
| 253 |
submit_btn = gr.Button("Send", variant="primary")
|
| 254 |
|
|
@@ -307,20 +319,22 @@ with gr.Blocks(title="Step-3 Chat", theme=gr.themes.Soft()) as demo:
|
|
| 307 |
)
|
| 308 |
|
| 309 |
# 事件处理函数
|
| 310 |
-
def user_submit(message, history):
|
| 311 |
"""用户提交消息时的处理"""
|
| 312 |
print(f"[DEBUG] user_submit called with message: {message[:50] if message else 'None'}...")
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
|
|
|
| 317 |
|
| 318 |
-
def bot_response(history, saved_message, system_prompt, temperature, max_tokens, top_p):
|
| 319 |
"""生成机器人响应"""
|
| 320 |
print(f"[DEBUG] bot_response called with saved_message: {saved_message[:50] if saved_message else 'None'}...")
|
| 321 |
-
|
|
|
|
| 322 |
# 使用生成器处理消息
|
| 323 |
-
for updated_history in process_message(saved_message, history, system_prompt, temperature, max_tokens, top_p):
|
| 324 |
yield updated_history
|
| 325 |
else:
|
| 326 |
yield history
|
|
@@ -337,38 +351,39 @@ with gr.Blocks(title="Step-3 Chat", theme=gr.themes.Soft()) as demo:
|
|
| 337 |
return new_history, last_message
|
| 338 |
return history, ""
|
| 339 |
|
| 340 |
-
#
|
| 341 |
saved_msg = gr.State("")
|
|
|
|
| 342 |
|
| 343 |
# 提交消息 - Enter键
|
| 344 |
msg.submit(
|
| 345 |
user_submit,
|
| 346 |
-
[msg, chatbot],
|
| 347 |
-
[msg, chatbot, saved_msg],
|
| 348 |
queue=False
|
| 349 |
).then(
|
| 350 |
bot_response,
|
| 351 |
-
[chatbot, saved_msg, system_prompt, temperature, max_tokens, top_p],
|
| 352 |
chatbot
|
| 353 |
)
|
| 354 |
|
| 355 |
# 提交消息 - Send按钮
|
| 356 |
submit_btn.click(
|
| 357 |
user_submit,
|
| 358 |
-
[msg, chatbot],
|
| 359 |
-
[msg, chatbot, saved_msg],
|
| 360 |
queue=False
|
| 361 |
).then(
|
| 362 |
bot_response,
|
| 363 |
-
[chatbot, saved_msg, system_prompt, temperature, max_tokens, top_p],
|
| 364 |
chatbot
|
| 365 |
)
|
| 366 |
|
| 367 |
# 清空对话
|
| 368 |
clear_btn.click(
|
| 369 |
-
lambda: ([], ""),
|
| 370 |
None,
|
| 371 |
-
[chatbot, msg]
|
| 372 |
)
|
| 373 |
|
| 374 |
# 撤销最后一条
|
|
@@ -385,7 +400,7 @@ with gr.Blocks(title="Step-3 Chat", theme=gr.themes.Soft()) as demo:
|
|
| 385 |
[chatbot, saved_msg]
|
| 386 |
).then(
|
| 387 |
bot_response,
|
| 388 |
-
[chatbot, saved_msg, system_prompt, temperature, max_tokens, top_p],
|
| 389 |
chatbot
|
| 390 |
)
|
| 391 |
|
|
|
|
| 27 |
|
| 28 |
return None
|
| 29 |
|
| 30 |
+
def process_message(message, history, image, system_prompt, temperature, max_tokens, top_p):
|
| 31 |
+
"""处理消息并生成响应,支持可选的图片输入"""
|
| 32 |
print(f"[DEBUG] Processing message: {message[:100] if message else 'None'}...")
|
| 33 |
+
print(f"[DEBUG] Has image: {image is not None}")
|
| 34 |
|
| 35 |
+
if not message and not image:
|
| 36 |
+
print("[DEBUG] No message or image provided, skipping")
|
| 37 |
yield history
|
| 38 |
return
|
| 39 |
|
| 40 |
if not STEP_API_KEY:
|
| 41 |
print("[DEBUG] No API key configured")
|
| 42 |
+
error_msg = "❌ API key not configured. Please add STEP_API_KEY in Settings."
|
| 43 |
+
display_msg = f"[Image] {message}" if image and message else "[Image]" if image else message
|
| 44 |
+
history.append([display_msg, error_msg])
|
| 45 |
yield history
|
| 46 |
return
|
| 47 |
|
| 48 |
+
# 处理图片
|
|
|
|
|
|
|
| 49 |
image_content = None
|
| 50 |
+
text_content = message or ""
|
| 51 |
|
| 52 |
+
if image:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
# 转换图片为base64
|
| 54 |
try:
|
| 55 |
+
image_content = image_to_base64(image)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
print(f"[DEBUG] Image processed successfully")
|
| 57 |
except Exception as e:
|
| 58 |
print(f"[DEBUG] Failed to process image: {e}")
|
| 59 |
+
history.append([message or "[Image]", f"❌ Failed to process image: {str(e)}"])
|
| 60 |
+
yield history
|
| 61 |
+
return
|
| 62 |
+
|
| 63 |
+
# 构造显示消息
|
| 64 |
+
if image and message:
|
| 65 |
+
display_message = f"🖼️ [Image] {message}"
|
| 66 |
+
elif image:
|
| 67 |
+
display_message = "🖼️ [Image]"
|
| 68 |
+
else:
|
| 69 |
+
display_message = message
|
| 70 |
|
| 71 |
# 添加到历史
|
| 72 |
+
history.append([display_message, ""])
|
| 73 |
yield history
|
| 74 |
|
| 75 |
# 构建API消息
|
|
|
|
| 244 |
|
| 245 |
# 输入区域
|
| 246 |
with gr.Row():
|
| 247 |
+
with gr.Column(scale=8):
|
| 248 |
msg = gr.Textbox(
|
| 249 |
label="Message",
|
| 250 |
+
placeholder="Type your message here...",
|
| 251 |
lines=2,
|
| 252 |
max_lines=10,
|
| 253 |
show_label=False,
|
| 254 |
elem_id="message-textbox"
|
| 255 |
)
|
| 256 |
+
with gr.Column(scale=2):
|
| 257 |
+
image_input = gr.Image(
|
| 258 |
+
label="Upload Image (Optional)",
|
| 259 |
+
type="filepath",
|
| 260 |
+
height=100,
|
| 261 |
+
interactive=True,
|
| 262 |
+
show_label=True
|
| 263 |
+
)
|
| 264 |
with gr.Column(scale=1, min_width=100):
|
| 265 |
submit_btn = gr.Button("Send", variant="primary")
|
| 266 |
|
|
|
|
| 319 |
)
|
| 320 |
|
| 321 |
# 事件处理函数
|
| 322 |
+
def user_submit(message, history, image):
|
| 323 |
"""用户提交消息时的处理"""
|
| 324 |
print(f"[DEBUG] user_submit called with message: {message[:50] if message else 'None'}...")
|
| 325 |
+
print(f"[DEBUG] user_submit called with image: {image is not None}")
|
| 326 |
+
if message or image:
|
| 327 |
+
# 清空输入,保存消息和图片用于后续处理
|
| 328 |
+
return gr.update(value=""), history, gr.update(value=None), message, image
|
| 329 |
+
return gr.update(value=message), history, gr.update(value=image), message, image
|
| 330 |
|
| 331 |
+
def bot_response(history, saved_message, saved_image, system_prompt, temperature, max_tokens, top_p):
|
| 332 |
"""生成机器人响应"""
|
| 333 |
print(f"[DEBUG] bot_response called with saved_message: {saved_message[:50] if saved_message else 'None'}...")
|
| 334 |
+
print(f"[DEBUG] bot_response called with saved_image: {saved_image is not None}")
|
| 335 |
+
if saved_message or saved_image:
|
| 336 |
# 使用生成器处理消息
|
| 337 |
+
for updated_history in process_message(saved_message, history, saved_image, system_prompt, temperature, max_tokens, top_p):
|
| 338 |
yield updated_history
|
| 339 |
else:
|
| 340 |
yield history
|
|
|
|
| 351 |
return new_history, last_message
|
| 352 |
return history, ""
|
| 353 |
|
| 354 |
+
# 创建隐藏的组件来存储消息和图片
|
| 355 |
saved_msg = gr.State("")
|
| 356 |
+
saved_img = gr.State(None)
|
| 357 |
|
| 358 |
# 提交消息 - Enter键
|
| 359 |
msg.submit(
|
| 360 |
user_submit,
|
| 361 |
+
[msg, chatbot, image_input],
|
| 362 |
+
[msg, chatbot, image_input, saved_msg, saved_img],
|
| 363 |
queue=False
|
| 364 |
).then(
|
| 365 |
bot_response,
|
| 366 |
+
[chatbot, saved_msg, saved_img, system_prompt, temperature, max_tokens, top_p],
|
| 367 |
chatbot
|
| 368 |
)
|
| 369 |
|
| 370 |
# 提交消息 - Send按钮
|
| 371 |
submit_btn.click(
|
| 372 |
user_submit,
|
| 373 |
+
[msg, chatbot, image_input],
|
| 374 |
+
[msg, chatbot, image_input, saved_msg, saved_img],
|
| 375 |
queue=False
|
| 376 |
).then(
|
| 377 |
bot_response,
|
| 378 |
+
[chatbot, saved_msg, saved_img, system_prompt, temperature, max_tokens, top_p],
|
| 379 |
chatbot
|
| 380 |
)
|
| 381 |
|
| 382 |
# 清空对话
|
| 383 |
clear_btn.click(
|
| 384 |
+
lambda: ([], "", None),
|
| 385 |
None,
|
| 386 |
+
[chatbot, msg, image_input]
|
| 387 |
)
|
| 388 |
|
| 389 |
# 撤销最后一条
|
|
|
|
| 400 |
[chatbot, saved_msg]
|
| 401 |
).then(
|
| 402 |
bot_response,
|
| 403 |
+
[chatbot, saved_msg, saved_img, system_prompt, temperature, max_tokens, top_p],
|
| 404 |
chatbot
|
| 405 |
)
|
| 406 |
|