Update app.py
Browse files
app.py
CHANGED
@@ -13,6 +13,42 @@ from typing import Optional, Union
|
|
13 |
import gradio as gr
|
14 |
import markdown
|
15 |
from openai import AzureOpenAI
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
def base64_to_image(
|
18 |
base64_str: str,
|
@@ -428,12 +464,45 @@ def create_demo():
|
|
428 |
extracted_images = gr.Gallery(label="Extracted Images")
|
429 |
with gr.Column(scale=1):
|
430 |
extracted_json = gr.JSON(label="Extracted JSON")
|
|
|
|
|
|
|
|
|
|
|
431 |
|
|
|
|
|
|
|
|
|
432 |
# 处理提交
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
433 |
submit_btn.click(
|
434 |
-
fn=
|
435 |
inputs=[model_name, api_key, base_url, question, image_input],
|
436 |
-
outputs=[output, extracted_images, extracted_json]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
437 |
)
|
438 |
|
439 |
# 示例部分
|
|
|
13 |
import gradio as gr
|
14 |
import markdown
|
15 |
from openai import AzureOpenAI
|
16 |
+
import zipfile
|
17 |
+
import tempfile
|
18 |
+
|
19 |
+
def export_to_zip(images, conversations):
|
20 |
+
"""
|
21 |
+
将图像和对话数据导出为ZIP文件
|
22 |
+
|
23 |
+
Args:
|
24 |
+
images: 提取的图像列表
|
25 |
+
conversations: 对话JSON数据
|
26 |
+
|
27 |
+
Returns:
|
28 |
+
生成的ZIP文件路径
|
29 |
+
"""
|
30 |
+
# 创建临时目录
|
31 |
+
temp_dir = tempfile.mkdtemp()
|
32 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
33 |
+
zip_filename = os.path.join(temp_dir, f"export_{timestamp}.zip")
|
34 |
+
|
35 |
+
# 创建ZIP文件
|
36 |
+
with zipfile.ZipFile(zip_filename, 'w') as zipf:
|
37 |
+
# 保存图像
|
38 |
+
for i, img in enumerate(images):
|
39 |
+
img_path = os.path.join(temp_dir, f"image_{i}.png")
|
40 |
+
img.save(img_path)
|
41 |
+
zipf.write(img_path, f"images/image_{i}.png")
|
42 |
+
os.remove(img_path) # 删除临时图像文件
|
43 |
+
|
44 |
+
# 保存对话数据
|
45 |
+
json_path = os.path.join(temp_dir, "conversations.json")
|
46 |
+
with open(json_path, 'w', encoding='utf-8') as f:
|
47 |
+
json.dump(conversations, f, ensure_ascii=False, indent=4)
|
48 |
+
zipf.write(json_path, "conversations.json")
|
49 |
+
os.remove(json_path) # 删除临时JSON文件
|
50 |
+
|
51 |
+
return zip_filename
|
52 |
|
53 |
def base64_to_image(
|
54 |
base64_str: str,
|
|
|
464 |
extracted_images = gr.Gallery(label="Extracted Images")
|
465 |
with gr.Column(scale=1):
|
466 |
extracted_json = gr.JSON(label="Extracted JSON")
|
467 |
+
|
468 |
+
# 添加导出按钮
|
469 |
+
with gr.Row():
|
470 |
+
export_btn = gr.Button("Export to ZIP")
|
471 |
+
download_file = gr.File(label="Download ZIP")
|
472 |
|
473 |
+
# 存储当前结果的状态变量
|
474 |
+
current_images = gr.State([])
|
475 |
+
current_json = gr.State(None)
|
476 |
+
|
477 |
# 处理提交
|
478 |
+
def handle_submit(model, key, url, q, img):
|
479 |
+
html, images, json_data = o3_chat(model, key, url, q, img)
|
480 |
+
return html, images, json_data, images, json_data
|
481 |
+
|
482 |
+
# # 处理提交
|
483 |
+
# submit_btn.click(
|
484 |
+
# fn=o3_chat,
|
485 |
+
# inputs=[model_name, api_key, base_url, question, image_input],
|
486 |
+
# outputs=[output, extracted_images, extracted_json]
|
487 |
+
# )
|
488 |
+
|
489 |
submit_btn.click(
|
490 |
+
fn=handle_submit,
|
491 |
inputs=[model_name, api_key, base_url, question, image_input],
|
492 |
+
outputs=[output, extracted_images, extracted_json, current_images, current_json]
|
493 |
+
)
|
494 |
+
|
495 |
+
# 处理导出
|
496 |
+
def handle_export(images, conversations):
|
497 |
+
if not images or conversations is None:
|
498 |
+
return None
|
499 |
+
zip_path = export_to_zip(images, conversations)
|
500 |
+
return zip_path
|
501 |
+
|
502 |
+
export_btn.click(
|
503 |
+
fn=handle_export,
|
504 |
+
inputs=[current_images, current_json],
|
505 |
+
outputs=[download_file]
|
506 |
)
|
507 |
|
508 |
# 示例部分
|