Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -546,15 +546,22 @@ def process_image_analysis_stream(image_dict, user_prompt, max_new_tokens, top_k
|
|
| 546 |
|
| 547 |
# 检查图像输入
|
| 548 |
if image_dict is None or image_dict.get('image') is None:
|
| 549 |
-
yield [["请先上传图片!", "我需要图片才能进行分析。"]], None
|
| 550 |
return
|
| 551 |
|
| 552 |
# 处理图像
|
| 553 |
image = image_dict['image']
|
| 554 |
if not isinstance(image, str):
|
| 555 |
import tempfile
|
|
|
|
| 556 |
temp_path = os.path.join(tempfile.gettempdir(), f"temp_image_{hash(str(image))}.png")
|
| 557 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 558 |
image = temp_path
|
| 559 |
|
| 560 |
# 处理提示词
|
|
@@ -631,14 +638,14 @@ def process_image_analysis_stream(image_dict, user_prompt, max_new_tokens, top_k
|
|
| 631 |
thread.start()
|
| 632 |
|
| 633 |
# 初始化聊天历史
|
| 634 |
-
chat_history = [[user_prompt, ""]]
|
| 635 |
yield chat_history, None
|
| 636 |
|
| 637 |
# 流式接收输出,参考mm_qwen2vl.py的bot函数
|
| 638 |
full_response = ""
|
| 639 |
for new_token in streamer:
|
| 640 |
full_response += new_token
|
| 641 |
-
chat_history[-1][1] = full_response
|
| 642 |
yield chat_history, None
|
| 643 |
|
| 644 |
# 处理完成后的文件生成
|
|
|
|
| 546 |
|
| 547 |
# 检查图像输入
|
| 548 |
if image_dict is None or image_dict.get('image') is None:
|
| 549 |
+
yield [[{"role": "user", "content": "请先上传图片!"}, {"role": "assistant", "content": "我需要图片才能进行分析。"}]], None
|
| 550 |
return
|
| 551 |
|
| 552 |
# 处理图像
|
| 553 |
image = image_dict['image']
|
| 554 |
if not isinstance(image, str):
|
| 555 |
import tempfile
|
| 556 |
+
from PIL import Image as PILImage
|
| 557 |
temp_path = os.path.join(tempfile.gettempdir(), f"temp_image_{hash(str(image))}.png")
|
| 558 |
+
# 将 numpy 数组转换为 PIL Image
|
| 559 |
+
if hasattr(image, 'shape'): # 确认是 numpy 数组
|
| 560 |
+
pil_img = PILImage.fromarray(image)
|
| 561 |
+
pil_img.save(temp_path)
|
| 562 |
+
else:
|
| 563 |
+
# 如果已经是 PIL Image
|
| 564 |
+
image.save(temp_path)
|
| 565 |
image = temp_path
|
| 566 |
|
| 567 |
# 处理提示词
|
|
|
|
| 638 |
thread.start()
|
| 639 |
|
| 640 |
# 初始化聊天历史
|
| 641 |
+
chat_history = [[{"role": "user", "content": user_prompt}, {"role": "assistant", "content": ""}]]
|
| 642 |
yield chat_history, None
|
| 643 |
|
| 644 |
# 流式接收输出,参考mm_qwen2vl.py的bot函数
|
| 645 |
full_response = ""
|
| 646 |
for new_token in streamer:
|
| 647 |
full_response += new_token
|
| 648 |
+
chat_history[-1][1]["content"] = full_response
|
| 649 |
yield chat_history, None
|
| 650 |
|
| 651 |
# 处理完成后的文件生成
|