Rurouni-II commited on
Commit
e4a2845
·
verified ·
1 Parent(s): f2cb330
Files changed (1) hide show
  1. app.py +10 -5
app.py CHANGED
@@ -93,8 +93,10 @@ class ImageInference:
93
  if use_cpu:
94
  print("Using CPU fallback mode (BLIP captioner + OCR)")
95
  try:
96
- from transformers import pipeline
97
- self.cpu_captioner = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base", device=-1)
 
 
98
  except Exception as exc:
99
  raise RuntimeError(
100
  "CPU fallback initialization failed. Ensure `torch` and `transformers` are installed. "
@@ -143,8 +145,11 @@ class ImageInference:
143
 
144
  # Run lightweight captioner
145
  try:
146
- caption_result = self.cpu_captioner(image)
147
- caption = caption_result[0].get("generated_text") or str(caption_result[0])
 
 
 
148
  except Exception as exc:
149
  caption = f"(caption failed: {exc})"
150
 
@@ -430,4 +435,4 @@ if __name__ == "__main__":
430
  theme=gr.themes.Soft(),
431
  css=CUSTOM_CSS,
432
  ssr_mode=False,
433
- )
 
93
  if use_cpu:
94
  print("Using CPU fallback mode (BLIP captioner + OCR)")
95
  try:
96
+ from transformers import AutoProcessor, AutoModelForVision2Seq
97
+ processor = AutoProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
98
+ model = AutoModelForVision2Seq.from_pretrained("Salesforce/blip-image-captioning-base")
99
+ self.cpu_captioner = {"processor": processor, "model": model}
100
  except Exception as exc:
101
  raise RuntimeError(
102
  "CPU fallback initialization failed. Ensure `torch` and `transformers` are installed. "
 
145
 
146
  # Run lightweight captioner
147
  try:
148
+ processor = self.cpu_captioner["processor"]
149
+ model = self.cpu_captioner["model"]
150
+ inputs = processor(images=image, return_tensors="pt")
151
+ outputs = model.generate(**inputs)
152
+ caption = processor.decode(outputs[0], skip_special_tokens=True)
153
  except Exception as exc:
154
  caption = f"(caption failed: {exc})"
155
 
 
435
  theme=gr.themes.Soft(),
436
  css=CUSTOM_CSS,
437
  ssr_mode=False,
438
+ )