Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import Qwen2_5_VLForConditionalGeneration, AutoProcessor
|
2 |
+
import gradio as gr
|
3 |
+
|
4 |
+
# 加载模型和处理器
|
5 |
+
model = Qwen2_5_VLForConditionalGeneration.from_pretrained("Qwen/Qwen2.5-VL-7B-Instruct", device_map="auto")
|
6 |
+
processor = AutoProcessor.from_pretrained("Qwen/Qwen2.5-VL-7B-Instruct")
|
7 |
+
|
8 |
+
# 定义识别和分析函数
|
9 |
+
def recognize_and_analyze(image, text_prompt):
|
10 |
+
messages = [
|
11 |
+
{
|
12 |
+
"role": "user",
|
13 |
+
"content": [
|
14 |
+
{"type": "image", "image": image},
|
15 |
+
{"type": "text", "text": text_prompt},
|
16 |
+
],
|
17 |
+
}
|
18 |
+
]
|
19 |
+
|
20 |
+
# 处理视觉信息
|
21 |
+
inputs = processor(images=image, return_tensors="pt")
|
22 |
+
inputs = inputs.to(model.device)
|
23 |
+
|
24 |
+
# 生成输出
|
25 |
+
generated_ids = model.generate(**inputs)
|
26 |
+
output_text = processor.batch_decode(generated_ids, skip_special_tokens=True)
|
27 |
+
return output_text[0]
|
28 |
+
|
29 |
+
# 设置Gradio界面
|
30 |
+
interface = gr.Interface(
|
31 |
+
fn=recognize_and_analyze,
|
32 |
+
inputs=[
|
33 |
+
gr.inputs.Image(type="filepath", label="上传图像"),
|
34 |
+
gr.inputs.Textbox(label="输入描述文本"),
|
35 |
+
],
|
36 |
+
outputs=gr.outputs.Textbox(label="识别结果"),
|
37 |
+
title="Qwen2.5-VL 物体识别与分析",
|
38 |
+
description="上传图像并输入描述文本以获取识别和分析结果。",
|
39 |
+
)
|
40 |
+
|
41 |
+
# 启动Gradio应用
|
42 |
+
if __name__ == "__main__":
|
43 |
+
interface.launch()
|