arshadrana commited on
Commit
54509a5
·
verified ·
1 Parent(s): 17be5b0

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -0
app.py ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import BlipProcessor, BlipForConditionalGeneration
3
+ from PIL import Image
4
+
5
+ # Load the image captioning model and processor
6
+ processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
7
+ model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
8
+
9
+ def generate_caption(image: Image.Image) -> str:
10
+ # Prepare the image for the model
11
+ inputs = processor(images=image, return_tensors="pt")
12
+
13
+ # Generate caption
14
+ output = model.generate(**inputs)
15
+
16
+ # Decode the caption
17
+ caption = processor.decode(output[0], skip_special_tokens=True)
18
+ return caption
19
+
20
+ def run():
21
+ demo = gr.Interface(
22
+ fn=generate_caption,
23
+ inputs=gr.Image(type="pil"),
24
+ outputs="text",
25
+ )
26
+
27
+ demo.launch(server_name="0.0.0.0", server_port=7860)
28
+
29
+ if __name__ == "__main__":
30
+ run()