TangYiJay commited on
Commit
04ec2fd
·
verified ·
1 Parent(s): 323984f
Files changed (1) hide show
  1. app.py +10 -7
app.py CHANGED
@@ -1,13 +1,17 @@
1
  from transformers import AutoProcessor, AutoModelForVision2Seq
2
  from PIL import Image
3
- import torch
4
  import gradio as gr
 
5
 
6
  MODEL_ID = "HuggingFaceM4/idefics2-8b"
7
 
8
- # Load model and processor
 
 
 
9
  processor = AutoProcessor.from_pretrained(MODEL_ID)
10
- model = AutoModelForVision2Seq.from_pretrained(MODEL_ID, torch_dtype=torch.float16, device_map="auto")
 
11
 
12
  def analyze_images(base_img, target_img, user_prompt):
13
  if base_img is None or target_img is None:
@@ -16,9 +20,8 @@ def analyze_images(base_img, target_img, user_prompt):
16
  images = [base_img, target_img]
17
  prompt = f"Ignore the first image (base image). Analyze the second image: {user_prompt}"
18
 
19
- inputs = processor(images=images, text=prompt, return_tensors="pt").to(model.device)
20
  output = model.generate(**inputs, max_new_tokens=200)
21
-
22
  result = processor.decode(output[0], skip_special_tokens=True)
23
  return result
24
 
@@ -30,8 +33,8 @@ demo = gr.Interface(
30
  gr.Textbox(label="Prompt", placeholder="Describe what to analyze...")
31
  ],
32
  outputs=gr.Textbox(label="Model Output"),
33
- title="Image Comparison with IDEFICS2-8B",
34
- description="Upload two images. The model will ignore the base image and analyze the target image according to your prompt."
35
  )
36
 
37
  if __name__ == "__main__":
 
1
  from transformers import AutoProcessor, AutoModelForVision2Seq
2
  from PIL import Image
 
3
  import gradio as gr
4
+ import torch
5
 
6
  MODEL_ID = "HuggingFaceM4/idefics2-8b"
7
 
8
+ # 强制使用 CPU 模式
9
+ device = "cpu"
10
+
11
+ # 加载模型与处理器(关闭 float16 避免 CPU 报错)
12
  processor = AutoProcessor.from_pretrained(MODEL_ID)
13
+ model = AutoModelForVision2Seq.from_pretrained(MODEL_ID, torch_dtype=torch.float32, device_map=None)
14
+ model.to(device)
15
 
16
  def analyze_images(base_img, target_img, user_prompt):
17
  if base_img is None or target_img is None:
 
20
  images = [base_img, target_img]
21
  prompt = f"Ignore the first image (base image). Analyze the second image: {user_prompt}"
22
 
23
+ inputs = processor(images=images, text=prompt, return_tensors="pt").to(device)
24
  output = model.generate(**inputs, max_new_tokens=200)
 
25
  result = processor.decode(output[0], skip_special_tokens=True)
26
  return result
27
 
 
33
  gr.Textbox(label="Prompt", placeholder="Describe what to analyze...")
34
  ],
35
  outputs=gr.Textbox(label="Model Output"),
36
+ title="Image Comparison (IDEFICS2-8B, CPU Mode)",
37
+ description="Upload two images. The model will ignore the base image and analyze the second according to your prompt."
38
  )
39
 
40
  if __name__ == "__main__":