NickoSELI commited on
Commit
535a03e
1 Parent(s): e1203d1

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -0
app.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from PIL import Image
3
+ from transformers import BlipProcessor, BlipForConditionalGeneration
4
+ import torch
5
+
6
+ # Initialize BLIP model and processor
7
+ device = torch.device("cuda" if torch.cuda.is_available() else "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
+ def caption_image(image):
12
+ inputs = processor(images=image, return_tensors="pt").to(device)
13
+ out = model.generate(**inputs)
14
+ caption = processor.decode(out[0], skip_special_tokens=True)
15
+ return caption
16
+
17
+ def process_image(image):
18
+ # Convert the input image to PIL Image
19
+ image = Image.fromarray(image)
20
+ # Get the caption
21
+ caption = caption_image(image)
22
+ return caption
23
+
24
+ # Create Gradio Interface
25
+ interface = gr.Interface(
26
+ fn=process_image,
27
+ inputs=gr.inputs.Image(type="numpy", label="Upload Image"),
28
+ outputs=gr.outputs.Textbox(label="Caption"),
29
+ title="BLIP Image Captioning",
30
+ description="Upload an image to get a caption generated by the BLIP model."
31
+ )
32
+
33
+ # Launch the Gradio app
34
+ if __name__ == "__main__":
35
+ interface.launch()