Update app.py
Browse files
    	
        app.py
    CHANGED
    
    | @@ -6,6 +6,43 @@ import base64 | |
| 6 | 
             
            from io import BytesIO
         | 
| 7 | 
             
            import gradio as gr
         | 
| 8 | 
             
            import markdown
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 9 |  | 
| 10 | 
             
            def base64_to_image(
         | 
| 11 | 
             
                base64_str: str, 
         | 
| @@ -156,6 +193,15 @@ with gr.Blocks(title="ChatGPT Conversation Visualizer", css="div.prose * {color: | |
| 156 |  | 
| 157 | 
             
                with gr.Row():
         | 
| 158 | 
             
                    output = gr.HTML(label="对话内容")
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 159 |  | 
| 160 | 
             
                visualize_button.click(
         | 
| 161 | 
             
                    fn=upload_and_process,
         | 
|  | |
| 6 | 
             
            from io import BytesIO
         | 
| 7 | 
             
            import gradio as gr
         | 
| 8 | 
             
            import markdown
         | 
| 9 | 
            +
            import zipfile
         | 
| 10 | 
            +
            import tempfile
         | 
| 11 | 
            +
            from datetime import datetime
         | 
| 12 | 
            +
             | 
| 13 | 
            +
            def export_to_zip(images, conversations):
         | 
| 14 | 
            +
                """
         | 
| 15 | 
            +
                将图像和对话数据导出为ZIP文件
         | 
| 16 | 
            +
                
         | 
| 17 | 
            +
                Args:
         | 
| 18 | 
            +
                    images: 提取的图像列表
         | 
| 19 | 
            +
                    conversations: 对话JSON数据
         | 
| 20 | 
            +
                
         | 
| 21 | 
            +
                Returns:
         | 
| 22 | 
            +
                    生成的ZIP文件路径
         | 
| 23 | 
            +
                """
         | 
| 24 | 
            +
                # 创建临时目录
         | 
| 25 | 
            +
                temp_dir = tempfile.mkdtemp()
         | 
| 26 | 
            +
                timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
         | 
| 27 | 
            +
                zip_filename = os.path.join(temp_dir, f"export_{timestamp}.zip")
         | 
| 28 | 
            +
                
         | 
| 29 | 
            +
                # 创建ZIP文件
         | 
| 30 | 
            +
                with zipfile.ZipFile(zip_filename, 'w') as zipf:
         | 
| 31 | 
            +
                    # 保存图像
         | 
| 32 | 
            +
                    for i, img in enumerate(images):
         | 
| 33 | 
            +
                        img_path = os.path.join(temp_dir, f"image_{i}.png")
         | 
| 34 | 
            +
                        img.save(img_path)
         | 
| 35 | 
            +
                        zipf.write(img_path, f"images/image_{i}.png")
         | 
| 36 | 
            +
                        os.remove(img_path)  # 删除临时图像文件
         | 
| 37 | 
            +
                    
         | 
| 38 | 
            +
                    # 保存对话数据
         | 
| 39 | 
            +
                    json_path = os.path.join(temp_dir, "conversations.json")
         | 
| 40 | 
            +
                    with open(json_path, 'w', encoding='utf-8') as f:
         | 
| 41 | 
            +
                        json.dump(conversations, f, ensure_ascii=False, indent=4)
         | 
| 42 | 
            +
                    zipf.write(json_path, "conversations.json")
         | 
| 43 | 
            +
                    os.remove(json_path)  # 删除临时JSON文件
         | 
| 44 | 
            +
                
         | 
| 45 | 
            +
                return zip_filename
         | 
| 46 |  | 
| 47 | 
             
            def base64_to_image(
         | 
| 48 | 
             
                base64_str: str, 
         | 
|  | |
| 193 |  | 
| 194 | 
             
                with gr.Row():
         | 
| 195 | 
             
                    output = gr.HTML(label="对话内容")
         | 
| 196 | 
            +
             | 
| 197 | 
            +
                # 添加导出按钮
         | 
| 198 | 
            +
                with gr.Row():
         | 
| 199 | 
            +
                    export_btn = gr.Button("Export to ZIP")
         | 
| 200 | 
            +
                    download_file = gr.File(label="Download ZIP")
         | 
| 201 | 
            +
             | 
| 202 | 
            +
                # 存储当前结果的状态变量
         | 
| 203 | 
            +
                current_images = gr.State([])
         | 
| 204 | 
            +
                current_json = gr.State(None)
         | 
| 205 |  | 
| 206 | 
             
                visualize_button.click(
         | 
| 207 | 
             
                    fn=upload_and_process,
         | 

