Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import BlipProcessor, BlipForConditionalGeneration
|
3 |
+
from PIL import Image
|
4 |
+
import torch
|
5 |
+
|
6 |
+
# Load 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 |
+
# Function to generate captions
|
11 |
+
def generate_caption(image):
|
12 |
+
image = Image.open(image).convert("RGB")
|
13 |
+
inputs = processor(image, return_tensors="pt")
|
14 |
+
outputs = model.generate(**inputs)
|
15 |
+
caption = processor.decode(outputs[0], skip_special_tokens=True)
|
16 |
+
return caption
|
17 |
+
|
18 |
+
# Create Gradio Interface
|
19 |
+
iface = gr.Interface(
|
20 |
+
fn=generate_caption,
|
21 |
+
inputs=gr.Image(type="pil"),
|
22 |
+
outputs="text",
|
23 |
+
title="AI Image Caption Generator",
|
24 |
+
description="Upload an image and get an AI-generated caption.",
|
25 |
+
)
|
26 |
+
|
27 |
+
# Run app
|
28 |
+
iface.launch()
|