Dibiddo commited on
Commit
b061fe3
·
verified ·
1 Parent(s): 0ea52a7

Update app.py

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