klizmillares commited on
Commit
2996946
·
verified ·
1 Parent(s): 77d244c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -0
app.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ from PIL import Image
4
+ from transformers import AutoProcessor, BlipForConditionalGeneration
5
+
6
+ # Load the pretrained processor and 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 and convert to RGB
12
+ raw_image = Image.fromarray(input_image).convert('RGB')
13
+
14
+ # Process the image
15
+ inputs = processor(raw_image, return_tensors="pt")
16
+
17
+ # Generate a caption for the image
18
+ out = model.generate(**inputs,max_length=50)
19
+
20
+ # Decode the generated tokens to text
21
+ caption = processor.decode(out[0], skip_special_tokens=True)
22
+
23
+ return caption
24
+
25
+ iface = gr.Interface(
26
+ fn=caption_image,
27
+ inputs=gr.Image(),
28
+ outputs="text",
29
+ title="Image Captioning Kliz Millares",
30
+ description="This is a simple web app for generating captions for images using a trained model."
31
+ )
32
+
33
+ iface.launch()