|
|
|
|
|
|
|
|
import io |
|
|
from PIL import Image |
|
|
import requests |
|
|
import warnings |
|
|
import gradio as gr |
|
|
from transformers import pipeline |
|
|
|
|
|
|
|
|
warnings.filterwarnings("ignore", message=".*Using the model-agnostic default `max_length`.*") |
|
|
|
|
|
|
|
|
captioner = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base") |
|
|
|
|
|
|
|
|
def caption_image(image_url): |
|
|
try: |
|
|
|
|
|
response = requests.get(image_url) |
|
|
response.raise_for_status() |
|
|
image = Image.open(io.BytesIO(response.content)).convert("RGB") |
|
|
|
|
|
|
|
|
caption = captioner(image)[0]["generated_text"] |
|
|
return caption |
|
|
|
|
|
except Exception as e: |
|
|
return f"Error processing image: {str(e)}" |
|
|
|
|
|
|
|
|
demo = gr.Interface( |
|
|
fn=caption_image, |
|
|
inputs=gr.Textbox(label="Image URL"), |
|
|
outputs="text", |
|
|
title="Image Captioning App", |
|
|
description=( |
|
|
"Upload an image or use one of the predefined examples to generate a caption. " |
|
|
"This app uses `Salesforce/blip-image-captioning-base`." |
|
|
), |
|
|
examples=[ |
|
|
['https://free-images.com/lg/9e46/white_bengal_tiger_tiger_0.jpg'] |
|
|
], |
|
|
flagging_mode="never" |
|
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
|
demo.launch() |
|
|
|
|
|
|