mursalinir commited on
Commit
5aa3e0d
·
1 Parent(s): ee49de0

gradio app

Browse files
Files changed (1) hide show
  1. app.py +13 -10
app.py CHANGED
@@ -2,6 +2,9 @@ from transformers import InstructBlipProcessor, InstructBlipForConditionalGenera
2
  from PIL import Image
3
  import torch
4
 
 
 
 
5
  device = "cuda" if torch.cuda.is_available() else "cpu"
6
  print(f"Using device: {device}")
7
 
@@ -14,22 +17,22 @@ model = InstructBlipForConditionalGeneration.from_pretrained(
14
  device_map="auto"
15
  )
16
 
17
- # Load your image
18
- image = Image.open("example.jpg").convert("RGB")
19
-
20
  # Prompt to force paragraph-level description
21
  prompt = (
22
  "Describe this image in a detailed paragraph of 5-7 sentences. "
23
  "Mention setting, objects, colors, actions, background details, and possible context."
24
  )
25
 
26
- inputs = processor(images=image, text=prompt, return_tensors="pt").to(device)
 
 
 
27
 
28
- out = model.generate(
29
- **inputs,
30
- max_new_tokens=250, # enough for multi-sentence
31
- temperature=0.7,
32
- top_p=0.9
33
  )
34
 
35
- print(processor.batch_decode(out, skip_special_tokens=True)[0])
 
2
  from PIL import Image
3
  import torch
4
 
5
+ import gradio as gr
6
+
7
+
8
  device = "cuda" if torch.cuda.is_available() else "cpu"
9
  print(f"Using device: {device}")
10
 
 
17
  device_map="auto"
18
  )
19
 
 
 
 
20
  # Prompt to force paragraph-level description
21
  prompt = (
22
  "Describe this image in a detailed paragraph of 5-7 sentences. "
23
  "Mention setting, objects, colors, actions, background details, and possible context."
24
  )
25
 
26
+ def caption_image(image):
27
+ inputs = processor(images=image, text=prompt, return_tensors="pt").to(device)
28
+ out = model.generate(**inputs, max_new_tokens=250, temperature=0.7, top_p=0.9)
29
+ return processor.batch_decode(out, skip_special_tokens=True)[0]
30
 
31
+ demo = gr.Interface(
32
+ fn=caption_image,
33
+ inputs=gr.Image(type="pil"),
34
+ outputs="text",
35
+ title="Image to Paragraph Captioning"
36
  )
37
 
38
+ demo.launch()