Janiator commited on
Commit
1df5447
1 Parent(s): 932473a

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
+ # Define the caption_image function that takes an input image and returns a caption
11
+ def caption_image(input_image: np.ndarray):
12
+ # Convert numpy array to PIL Image and convert to RGB
13
+ raw_image = Image.fromarray(input_image).convert('RGB')
14
+ # Process the image
15
+ inputs = processor(raw_image, return_tensors="pt")
16
+ # Generate a caption for the image
17
+ out = model.generate(**inputs, max_length=50)
18
+ # Decode the generated tokens to text
19
+ caption = processor.decode(out[0], skip_special_tokens=True)
20
+ return caption
21
+
22
+ # Gradio interface
23
+ iface = gr.Interface(
24
+ fn=caption_image,
25
+ inputs=gr.Image(),
26
+ outputs="text",
27
+ title="Image Captioning",
28
+ description="This is a web app for generating captions for images using a trained AI model."
29
+ )
30
+
31
+ # Launch the app with a public link
32
+ iface.launch(share=True)
33
+