Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -4,47 +4,53 @@ from qwen_vl_utils import process_vision_info
|
|
4 |
import gradio as gr
|
5 |
|
6 |
# 加载模型和处理器
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
|
|
|
|
|
|
13 |
|
14 |
# 定义处理函数
|
15 |
def recognize_and_analyze(image, text_prompt):
|
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 |
# 设置 Gradio 界面
|
50 |
interface = gr.Interface(
|
|
|
4 |
import gradio as gr
|
5 |
|
6 |
# 加载模型和处理器
|
7 |
+
try:
|
8 |
+
model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
|
9 |
+
"Qwen/Qwen2.5-VL-7B-Instruct",
|
10 |
+
torch_dtype="auto",
|
11 |
+
device_map="auto"
|
12 |
+
)
|
13 |
+
processor = AutoProcessor.from_pretrained("Qwen/Qwen2.5-VL-7B-Instruct")
|
14 |
+
except Exception as e:
|
15 |
+
print(f"模型加载失败: {e}")
|
16 |
|
17 |
# 定义处理函数
|
18 |
def recognize_and_analyze(image, text_prompt):
|
19 |
+
try:
|
20 |
+
messages = [
|
21 |
+
{
|
22 |
+
"role": "user",
|
23 |
+
"content": [
|
24 |
+
{"type": "image", "image": image},
|
25 |
+
{"type": "text", "text": text_prompt},
|
26 |
+
],
|
27 |
+
}
|
28 |
+
]
|
29 |
|
30 |
+
# 准备推理输入数据
|
31 |
+
text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
32 |
+
image_inputs, video_inputs = process_vision_info(messages)
|
33 |
+
inputs = processor(
|
34 |
+
text=[text],
|
35 |
+
images=image_inputs,
|
36 |
+
videos=video_inputs,
|
37 |
+
padding=True,
|
38 |
+
return_tensors="pt",
|
39 |
+
)
|
40 |
+
inputs = inputs.to(model.device)
|
41 |
|
42 |
+
# 推理:生成输出文本
|
43 |
+
generated_ids = model.generate(**inputs, max_new_tokens=128)
|
44 |
+
generated_ids_trimmed = [
|
45 |
+
out_ids[len(in_ids):] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
|
46 |
+
]
|
47 |
+
output_text = processor.batch_decode(
|
48 |
+
generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
|
49 |
+
)
|
50 |
+
|
51 |
+
return output_text[0]
|
52 |
+
except Exception as e:
|
53 |
+
return f"处理过程中出现错误: {e}"
|
54 |
|
55 |
# 设置 Gradio 界面
|
56 |
interface = gr.Interface(
|