Update app.py
Browse files
    	
        app.py
    CHANGED
    
    | @@ -47,119 +47,23 @@ def generate_caption(image_path, question): | |
| 47 | 
             
                        full_response += token
         | 
| 48 | 
             
                        yield full_response
         | 
| 49 |  | 
| 50 | 
            -
             | 
| 51 | 
            -
             | 
| 52 | 
             
            # 创建Gradio界面
         | 
| 53 | 
            -
            title | 
| 54 | 
            -
             | 
| 55 | 
            -
                 | 
| 56 | 
            -
                 | 
| 57 | 
            -
             | 
| 58 | 
            -
            )
         | 
|  | |
|  | |
|  | |
| 59 |  | 
| 60 | 
            -
            with gr.Blocks(title=title, theme=theme) as demo:
         | 
| 61 | 
            -
                # ================= 头部区域 =================
         | 
| 62 | 
            -
                gr.Markdown(f"""
         | 
| 63 | 
            -
                <div style="text-align: center;">
         | 
| 64 | 
            -
                    <h1 style="color: #2E86C1; border-bottom: 3px solid #AED6F1; padding-bottom: 10px;">🖼️ {title}</h1>
         | 
| 65 | 
            -
                    <p style="color: #616A6B;">上传图片并输入问题,体验腾讯混元视觉大模型的图像理解能力</p>
         | 
| 66 | 
            -
                </div>
         | 
| 67 | 
            -
                """)
         | 
| 68 | 
            -
                
         | 
| 69 | 
            -
                # ================= 主体区域 =================
         | 
| 70 | 
            -
                with gr.Row(variant="panel"):
         | 
| 71 | 
            -
                    # 左侧输入列
         | 
| 72 | 
            -
                    with gr.Column(scale=3):
         | 
| 73 | 
            -
                        with gr.Group(label="输入区域"):
         | 
| 74 | 
            -
                            image_input = gr.Image(
         | 
| 75 | 
            -
                                type="filepath",
         | 
| 76 | 
            -
                                label="上传图片",
         | 
| 77 | 
            -
                                height=400,
         | 
| 78 | 
            -
                                show_download_button=False,
         | 
| 79 | 
            -
                                elem_classes="preview-box"
         | 
| 80 | 
            -
                            )
         | 
| 81 | 
            -
                            question_input = gr.Textbox(
         | 
| 82 | 
            -
                                label="问题描述",
         | 
| 83 | 
            -
                                placeholder="请输入关于图片的问题...",
         | 
| 84 | 
            -
                                value="请详细描述图片中的场景、人物和细节",
         | 
| 85 | 
            -
                                lines=2
         | 
| 86 | 
            -
                            )
         | 
| 87 | 
            -
                            with gr.Row():
         | 
| 88 | 
            -
                                clear_btn = gr.Button("清空", variant="secondary")
         | 
| 89 | 
            -
                                submit_btn = gr.Button("生成描述", variant="primary")
         | 
| 90 | 
            -
                    
         | 
| 91 | 
            -
                    # 右侧输出列
         | 
| 92 | 
            -
                    with gr.Column(scale=4):
         | 
| 93 | 
            -
                        with gr.Group(label="生成结果"):
         | 
| 94 | 
            -
                            output = gr.Textbox(
         | 
| 95 | 
            -
                                label="描述内容",
         | 
| 96 | 
            -
                                interactive=False,
         | 
| 97 | 
            -
                                show_copy_button=True,
         | 
| 98 | 
            -
                                lines=12,
         | 
| 99 | 
            -
                                max_lines=20,
         | 
| 100 | 
            -
                                autoscroll=True
         | 
| 101 | 
            -
                            )
         | 
| 102 | 
            -
                
         | 
| 103 | 
            -
                # ================= 示例区域 =================
         | 
| 104 | 
            -
                with gr.Accordion("🖼️ 点击查看示例", open=False):
         | 
| 105 | 
            -
                    with gr.Row():
         | 
| 106 | 
            -
                        gr.Examples(
         | 
| 107 | 
            -
                            examples=[
         | 
| 108 | 
            -
                                ["tencent.png", "图片中的天气状况如何?"],
         | 
| 109 | 
            -
                                ["tencent.png", "描述参会人员的衣着特征"]
         | 
| 110 | 
            -
                            ],
         | 
| 111 | 
            -
                            inputs=[image_input, question_input],
         | 
| 112 | 
            -
                            label="快速示例"
         | 
| 113 | 
            -
                        )
         | 
| 114 | 
            -
                
         | 
| 115 | 
            -
                # ================= 交互逻辑 =================
         | 
| 116 | 
             
                submit_btn.click(
         | 
| 117 | 
             
                    fn=generate_caption,
         | 
| 118 | 
             
                    inputs=[image_input, question_input],
         | 
| 119 | 
            -
                    outputs=output | 
| 120 | 
            -
                    api_name="generate"
         | 
| 121 | 
             
                )
         | 
| 122 | 
            -
                
         | 
| 123 | 
            -
                clear_btn.click(
         | 
| 124 | 
            -
                    fn=lambda: [None, "", ""],
         | 
| 125 | 
            -
                    outputs=[image_input, question_input, output],
         | 
| 126 | 
            -
                    queue=False
         | 
| 127 | 
            -
                )
         | 
| 128 | 
            -
             | 
| 129 | 
            -
            # ================= 自定义样式 =================
         | 
| 130 | 
            -
            css = """
         | 
| 131 | 
            -
            .preview-box img {border-radius: 10px; box-shadow: 0 4px 6px rgba(0,0,0,0.1);}
         | 
| 132 | 
            -
            .preview-box:hover img {transform: scale(1.02);}
         | 
| 133 | 
            -
            button#generate {transition: all 0.3s ease;}
         | 
| 134 | 
            -
            """
         | 
| 135 | 
            -
            demo.css = css
         | 
| 136 |  | 
| 137 | 
             
            if __name__ == "__main__":
         | 
| 138 | 
             
                demo.queue(default_concurrency_limit=100)
         | 
| 139 | 
            -
                demo.launch(
         | 
| 140 | 
            -
                    server_port=7860,
         | 
| 141 | 
            -
                    show_error=True,
         | 
| 142 | 
            -
                    favicon_path="favicon.ico",
         | 
| 143 | 
            -
                    max_threads=100
         | 
| 144 | 
            -
                )
         | 
| 145 | 
            -
             | 
| 146 | 
            -
            # # 创建Gradio界面
         | 
| 147 | 
            -
            # title="Hunyuan-Vision图生文Demo"
         | 
| 148 | 
            -
            # with gr.Blocks(title=title) as demo:
         | 
| 149 | 
            -
            #     gr.Markdown(f"# 🖼️ {title}")
         | 
| 150 | 
            -
            #     with gr.Row():
         | 
| 151 | 
            -
            #         with gr.Column():
         | 
| 152 | 
            -
            #             image_input = gr.Image(type="filepath", label="上传图片")
         | 
| 153 | 
            -
            #             question_input = gr.Textbox(label="输入问题", value="请描述图片内容")
         | 
| 154 | 
            -
            #             submit_btn = gr.Button("生成描述")
         | 
| 155 | 
            -
            #         output = gr.Textbox(label="描述结果", interactive=False)
         | 
| 156 | 
            -
             | 
| 157 | 
            -
            #     submit_btn.click(
         | 
| 158 | 
            -
            #         fn=generate_caption,
         | 
| 159 | 
            -
            #         inputs=[image_input, question_input],
         | 
| 160 | 
            -
            #         outputs=output
         | 
| 161 | 
            -
            #     )
         | 
| 162 | 
            -
             | 
| 163 | 
            -
            # if __name__ == "__main__":
         | 
| 164 | 
            -
            #     demo.queue(default_concurrency_limit=100)
         | 
| 165 | 
            -
            #     demo.launch(max_threads=100)
         | 
|  | |
| 47 | 
             
                        full_response += token
         | 
| 48 | 
             
                        yield full_response
         | 
| 49 |  | 
|  | |
|  | |
| 50 | 
             
            # 创建Gradio界面
         | 
| 51 | 
            +
            title="Hunyuan-Vision图生文Demo"
         | 
| 52 | 
            +
            with gr.Blocks(title=title) as demo:
         | 
| 53 | 
            +
                gr.Markdown(f"# 🖼️ {title}")
         | 
| 54 | 
            +
                with gr.Row():
         | 
| 55 | 
            +
                    with gr.Column():
         | 
| 56 | 
            +
                        image_input = gr.Image(type="filepath", label="上传图片")
         | 
| 57 | 
            +
                        question_input = gr.Textbox(label="输入问题", value="请描述图片内容")
         | 
| 58 | 
            +
                        submit_btn = gr.Button("生成描述")
         | 
| 59 | 
            +
                    output = gr.Textbox(label="描述结果", interactive=False)
         | 
| 60 |  | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 61 | 
             
                submit_btn.click(
         | 
| 62 | 
             
                    fn=generate_caption,
         | 
| 63 | 
             
                    inputs=[image_input, question_input],
         | 
| 64 | 
            +
                    outputs=output
         | 
|  | |
| 65 | 
             
                )
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 66 |  | 
| 67 | 
             
            if __name__ == "__main__":
         | 
| 68 | 
             
                demo.queue(default_concurrency_limit=100)
         | 
| 69 | 
            +
                demo.launch(max_threads=100)
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  |