Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import BlipProcessor, BlipForConditionalGeneration
|
3 |
+
from PIL import Image
|
4 |
+
import torch
|
5 |
+
|
6 |
+
# Load the BLIP model and processor
|
7 |
+
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
8 |
+
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
|
9 |
+
|
10 |
+
def generate_caption(image):
|
11 |
+
image = image.convert("RGB")
|
12 |
+
inputs = processor(images=image, return_tensors="pt")
|
13 |
+
with torch.no_grad():
|
14 |
+
output = model.generate(**inputs)
|
15 |
+
caption = processor.decode(output[0], skip_special_tokens=True)
|
16 |
+
return caption
|
17 |
+
|
18 |
+
# Gradio interface
|
19 |
+
interface = gr.Interface(
|
20 |
+
fn=generate_caption,
|
21 |
+
inputs=gr.Image(type="pil", label="Upload an Image"),
|
22 |
+
outputs=gr.Textbox(label="Generated Caption"),
|
23 |
+
title="📸 BLIP Image Captioning",
|
24 |
+
description="Upload an image and get a descriptive caption using the BLIP model.",
|
25 |
+
allow_flagging="never"
|
26 |
+
)
|
27 |
+
|
28 |
+
if __name__ == "__main__":
|
29 |
+
interface.launch()
|