Model Deployed
Browse files
app.py
CHANGED
@@ -1,31 +1,30 @@
|
|
1 |
import gradio as gr
|
2 |
from PIL import Image
|
3 |
import torch
|
4 |
-
from transformers import
|
5 |
|
6 |
-
# Load
|
7 |
-
processor =
|
8 |
-
model =
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
# Generate caption
|
17 |
generated_ids = model.generate(pixel_values=inputs.pixel_values, max_length=25)
|
18 |
caption = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
19 |
return caption
|
20 |
|
21 |
-
# Gradio
|
22 |
iface = gr.Interface(
|
23 |
fn=generate_caption,
|
24 |
inputs=gr.Image(type="pil"),
|
25 |
outputs="text",
|
26 |
-
title="
|
27 |
-
description="Upload an image to generate a caption
|
28 |
)
|
29 |
|
30 |
-
|
31 |
-
|
|
|
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()
|