Spaces:
Sleeping
Sleeping
First Commit
Browse files
app.py
ADDED
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from PIL import Image
|
3 |
+
import requests
|
4 |
+
from transformers import BlipProcessor, BlipForConditionalGeneration
|
5 |
+
|
6 |
+
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
7 |
+
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base").to("cuda")
|
8 |
+
|
9 |
+
def generate_caption(image_url):
|
10 |
+
raw_image = Image.open(requests.get(image_url, stream=True).raw).convert('RGB')
|
11 |
+
|
12 |
+
# Unconditional image captioning
|
13 |
+
inputs = processor(raw_image, return_tensors="pt").to("cuda")
|
14 |
+
out = model.generate(**inputs)
|
15 |
+
caption = processor.decode(out[0], skip_special_tokens=True)
|
16 |
+
|
17 |
+
return caption
|
18 |
+
|
19 |
+
# Define the Gradio interface
|
20 |
+
iface = gr.Interface(
|
21 |
+
fn=generate_caption,
|
22 |
+
inputs="text", # URL input
|
23 |
+
outputs="text", # Caption output
|
24 |
+
title="Image Captioning with BLIP",
|
25 |
+
description="Provide an image URL, and the model will generate a caption."
|
26 |
+
)
|
27 |
+
|
28 |
+
if __name__ == "__main__":
|
29 |
+
iface.launch()
|