Muhammad-Izhan commited on
Commit
ae8acec
·
verified ·
1 Parent(s): 5505d0d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -29
app.py CHANGED
@@ -1,30 +1,29 @@
1
- import gradio as gr
2
- from transformers import AutoProcessor, BlipForConditionalGeneration
3
- from PIL import Image
4
- import numpy as np
5
-
6
- # Load BLIP model
7
- processor = AutoProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
8
- model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
9
-
10
- def caption_image(input_image: np.ndarray):
11
- # Convert numpy array to PIL Image
12
- raw_image = Image.fromarray(input_image).convert('RGB')
13
-
14
- # Generate caption
15
- inputs = processor(images=raw_image, text="a photo of", return_tensors="pt")
16
- outputs = model.generate(**inputs, max_new_tokens=50)
17
- caption = processor.decode(outputs[0], skip_special_tokens=True)
18
-
19
- return caption
20
-
21
- # Gradio interface
22
- iface = gr.Interface(
23
- fn=caption_image,
24
- inputs=gr.Image(),
25
- outputs="text",
26
- title="Image Captioning",
27
- examples=["example1.jpg", "example2.jpg"] # Optional: Add sample images
28
- )
29
-
30
  iface.launch()
 
1
+ import gradio as gr
2
+ from transformers import AutoProcessor, BlipForConditionalGeneration
3
+ from PIL import Image
4
+ import numpy as np
5
+
6
+ # Load BLIP model
7
+ processor = AutoProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
8
+ model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
9
+
10
+ def caption_image(input_image: np.ndarray):
11
+ # Convert numpy array to PIL Image
12
+ raw_image = Image.fromarray(input_image).convert('RGB')
13
+
14
+ # Generate caption
15
+ inputs = processor(images=raw_image, text="a photo of", return_tensors="pt")
16
+ outputs = model.generate(**inputs, max_new_tokens=50)
17
+ caption = processor.decode(outputs[0], skip_special_tokens=True)
18
+
19
+ return caption
20
+
21
+ # Gradio interface
22
+ iface = gr.Interface(
23
+ fn=caption_image,
24
+ inputs=gr.Image(),
25
+ outputs="text",
26
+ title="Image Captioning",
27
+ )
28
+
 
29
  iface.launch()