mahesh1209 commited on
Commit
5013910
·
verified ·
1 Parent(s): 4f04051

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -12
app.py CHANGED
@@ -1,31 +1,29 @@
1
- # 🖼️ BLIP Image Captioning - Fast, Accurate, CPU-Friendly
2
  import torch
3
  from transformers import BlipProcessor, BlipForConditionalGeneration
4
  from PIL import Image
5
  import gradio as gr
6
 
7
  # Load model and processor
8
- device = "cpu"
9
  processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
10
  model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base").to(device)
11
 
12
- # Captioning function
13
  def caption_image(image):
14
  try:
15
  inputs = processor(images=image, return_tensors="pt").to(device)
16
- out = model.generate(**inputs, max_length=30)
17
- caption = processor.tokenizer.decode(out[0], skip_special_tokens=True)
18
  return caption.capitalize()
19
- except Exception:
20
- return "Could not generate caption. Try a different image."
21
 
22
- # Launch Gradio interface
23
  gr.Interface(
24
  fn=caption_image,
25
  inputs=gr.Image(type="pil", label="Upload Image"),
26
  outputs=gr.Textbox(label="Generated Caption"),
27
- title="🖼️ Image Caption Generator (BLIP)",
28
- description="Accurate, fast image captioning using BLIP. No API keys. CPU-friendly. Instant output.",
29
- examples=["example.jpg"], # Optional: preload sample image
30
- cache_examples=True # Optional: speeds up UX
31
  ).launch()
 
 
1
  import torch
2
  from transformers import BlipProcessor, BlipForConditionalGeneration
3
  from PIL import Image
4
  import gradio as gr
5
 
6
  # Load model and processor
7
+ device = torch.device("cpu")
8
  processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
9
  model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base").to(device)
10
 
11
+ # Captioning function with fallback logic
12
  def caption_image(image):
13
  try:
14
  inputs = processor(images=image, return_tensors="pt").to(device)
15
+ output = model.generate(**inputs, max_length=30)
16
+ caption = processor.tokenizer.decode(output[0], skip_special_tokens=True)
17
  return caption.capitalize()
18
+ except Exception as e:
19
+ return f"⚠️ Error: {str(e)[:100]}"
20
 
21
+ # Gradio UI
22
  gr.Interface(
23
  fn=caption_image,
24
  inputs=gr.Image(type="pil", label="Upload Image"),
25
  outputs=gr.Textbox(label="Generated Caption"),
26
+ title="🖼️ BLIP Image Caption Generator",
27
+ description="Fast, accurate image captioning using BLIP. No API keys. CPU-friendly. Instant output.",
28
+ allow_flagging="never"
 
29
  ).launch()