File size: 2,667 Bytes
1876937
82a64c8
 
1876937
82a64c8
 
 
1876937
82a64c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1876937
82a64c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1876937
82a64c8
1876937
82a64c8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import gradio as gr
from simple_inference import predict_location
import os

# 设置默认模型和引擎
DEFAULT_MODEL = "Qwen/Qwen2.5-VL-7B-Instruct"
DEFAULT_ENGINE = "transformers"

def process_image(image, model_name, inference_engine):
    """
    处理上传的图片并返回预测结果
    """
    # 保存上传的图片到临时文件
    temp_path = "temp_image.jpg"
    image.save(temp_path)
    
    try:
        # 调用预测函数
        result = predict_location(
            image_path=temp_path,
            model_name=model_name,
            inference_engine=inference_engine
        )
        
        # 删除临时文件
        os.remove(temp_path)
        
        return result
    except Exception as e:
        # 删除临时文件
        if os.path.exists(temp_path):
            os.remove(temp_path)
        return f"发生错误: {str(e)}"

# 创建Gradio界面
with gr.Blocks(title="位置识别系统") as demo:
    gr.Markdown("# 🏞️ 图片位置识别系统")
    gr.Markdown("上传一张图片,系统将识别图片拍摄的国家和地区。")
    
    with gr.Row():
        with gr.Column():
            # 图片上传组件
            image_input = gr.Image(type="pil", label="上传图片")
            
            # 模型选择
            model_name = gr.Textbox(
                label="模型名称",
                value=DEFAULT_MODEL,
                info="输入Hugging Face模型ID或本地模型路径"
            )
            
            # 推理引擎选择
            inference_engine = gr.Dropdown(
                choices=["transformers", "vllm"],
                value=DEFAULT_ENGINE,
                label="推理引擎"
            )
            
            # 提交按钮
            submit_btn = gr.Button("开始识别", variant="primary")
        
        with gr.Column():
            # 结果显示
            output = gr.Textbox(
                label="识别结果",
                lines=10,
                placeholder="识别结果将显示在这里..."
            )
    
    # 设置提交按钮的回调函数
    submit_btn.click(
        fn=process_image,
        inputs=[image_input, model_name, inference_engine],
        outputs=output
    )
    
    # 添加示例
    gr.Examples(
        examples=[
            ["examples/example1.jpg", DEFAULT_MODEL, DEFAULT_ENGINE],
            ["examples/example2.jpg", DEFAULT_MODEL, DEFAULT_ENGINE],
        ],
        inputs=[image_input, model_name, inference_engine],
        outputs=output,
        fn=process_image,
        cache_examples=True,
    )

# 启动应用
if __name__ == "__main__":
    demo.launch(share=True)