File size: 1,410 Bytes
052f2f5
 
d907d8c
 
75fd3f8
683e91e
 
052f2f5
683e91e
052f2f5
683e91e
 
052f2f5
 
75fd3f8
052f2f5
46f4a73
d907d8c
052f2f5
d907d8c
 
052f2f5
d907d8c
052f2f5
 
 
75fd3f8
d907d8c
 
 
052f2f5
75fd3f8
46f4a73
d907d8c
46f4a73
75fd3f8
 
052f2f5
 
75fd3f8
052f2f5
 
 
 
75fd3f8
 
 
 
d907d8c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# app.py

import io
from PIL import Image
import requests
import warnings
import gradio as gr
from transformers import pipeline

# Suppress warnings
warnings.filterwarnings("ignore", message=".*Using the model-agnostic default `max_length`.*")

# Load BLIP image captioning model via Hugging Face pipeline
captioner = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")

# Helper function to download/process image and generate caption
def caption_image(image_url):
    try:
        # Load image from URL
        response = requests.get(image_url)
        response.raise_for_status()
        image = Image.open(io.BytesIO(response.content)).convert("RGB")

        # Generate caption using the pipeline
        caption = captioner(image)[0]["generated_text"]
        return caption

    except Exception as e:
        return f"Error processing image: {str(e)}"

# Gradio interface with JPEG examples
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()