Spaces:
Sleeping
Sleeping
James Bentley
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,38 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
2 |
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
from PIL import Image
|
4 |
+
import requests
|
5 |
|
6 |
+
# Initialize the pipeline
|
7 |
+
pipe = pipeline("image-to-text", model="Salesforce/blip-image-captioning-large")
|
8 |
+
|
9 |
+
def image_caption(image, text_prompt=None):
|
10 |
+
# Conditional image captioning if text prompt is provided
|
11 |
+
if text_prompt:
|
12 |
+
inputs = processor(image, text_prompt, return_tensors="pt")
|
13 |
+
out = model.generate(**inputs)
|
14 |
+
caption = processor.decode(out[0], skip_special_tokens=True)
|
15 |
+
else:
|
16 |
+
# Unconditional image captioning
|
17 |
+
inputs = processor(image, return_tensors="pt")
|
18 |
+
out = model.generate(**inputs)
|
19 |
+
caption = processor.decode(out[0], skip_special_tokens=True)
|
20 |
+
return caption
|
21 |
+
|
22 |
+
# Initialize processor and model
|
23 |
+
from transformers import BlipProcessor, BlipForConditionalGeneration
|
24 |
+
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-large")
|
25 |
+
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-large")
|
26 |
+
|
27 |
+
# Define the Gradio interface
|
28 |
+
image_input = gr.inputs.Image(type="pil", label="Upload an Image")
|
29 |
+
text_input = gr.inputs.Textbox(lines=1, placeholder="Optional: Enter text prompt", label="Text Prompt")
|
30 |
+
output = gr.outputs.Textbox(label="Generated Caption")
|
31 |
+
|
32 |
+
gr.Interface(
|
33 |
+
fn=image_caption,
|
34 |
+
inputs=[image_input, text_input],
|
35 |
+
outputs=output,
|
36 |
+
title="Image Captioning with BLIP",
|
37 |
+
description="Upload an image and get a generated caption. Optionally, provide a text prompt for conditional captioning."
|
38 |
+
).launch()
|