Model Deployed
Browse files- app.py +30 -0
- requirements.txt +4 -0
app.py
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from PIL import Image
|
3 |
+
import torch
|
4 |
+
from transformers import BlipProcessor, BlipForConditionalGeneration
|
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 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
11 |
+
model = model.to(device)
|
12 |
+
|
13 |
+
# Define the function to generate caption
|
14 |
+
def generate_caption(image):
|
15 |
+
inputs = processor(images=image, return_tensors="pt").to(device)
|
16 |
+
generated_ids = model.generate(pixel_values=inputs.pixel_values, max_length=25)
|
17 |
+
caption = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
18 |
+
return caption
|
19 |
+
|
20 |
+
# Create Gradio interface
|
21 |
+
iface = gr.Interface(
|
22 |
+
fn=generate_caption,
|
23 |
+
inputs=gr.Image(type="pil"),
|
24 |
+
outputs="text",
|
25 |
+
title="Image Caption Generator",
|
26 |
+
description="Upload an image to generate a caption."
|
27 |
+
)
|
28 |
+
|
29 |
+
# Launch
|
30 |
+
iface.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
torch
|
2 |
+
transformers
|
3 |
+
gradio
|
4 |
+
Pillow
|