Devarsh24 commited on
Commit
6860ee1
·
verified ·
1 Parent(s): 285daeb

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -0
app.py ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import BlipProcessor, BlipForConditionalGeneration
3
+ import torch
4
+ from PIL import Image
5
+ from io import BytesIO
6
+ import requests
7
+
8
+ # Load model and processor
9
+ model_name = "Salesforce/blip-image-captioning-large"
10
+ processor = BlipProcessor.from_pretrained(model_name)
11
+ model = BlipForConditionalGeneration.from_pretrained(model_name)
12
+
13
+ def generate_caption(image):
14
+ # Preprocess the image
15
+ inputs = processor(image, return_tensors="pt")
16
+ # Generate caption
17
+ with torch.no_grad():
18
+ outputs = model.generate(**inputs)
19
+ # Decode and return caption
20
+ caption = processor.decode(outputs[0], skip_special_tokens=True)
21
+ return caption
22
+
23
+ # Create a Gradio interface
24
+ iface = gr.Interface(fn=generate_caption, inputs="image", outputs="text")
25
+ iface.launch(share=True) # `share=True` to get a public link