File size: 1,778 Bytes
741667f
 
38abc6a
 
 
 
 
741667f
38abc6a
741667f
1b7c0b6
38abc6a
741667f
 
38abc6a
741667f
 
659bfbb
741667f
c88e865
741667f
 
 
659bfbb
 
38abc6a
 
 
 
 
 
659bfbb
 
 
 
 
 
 
 
 
 
741667f
 
38abc6a
 
 
 
1d0a556
b609efe
38abc6a
 
 
 
741667f
38abc6a
741667f
38abc6a
 
 
c88e865
659bfbb
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
51
52
53
54
55
56
57
58
59
60
61
62
# app.py

import os
import io
from PIL import Image
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 pipeline
captioner = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")

# Function to generate caption using the pipeline
def generate_caption(image: Image.Image):
    try:
        # Convert image to RGB just in case
        image = image.convert("RGB")
        # Generate caption
        caption = captioner(image)[0]["generated_text"]
        return caption
    except Exception as e:
        return f"An error occurred: {str(e)}"

# Predefined sample images
def get_sample_images():
    """
    Returns a list of predefined sample images in the assets directory.
    """
    sample_dir = "CreatureCaptures"  # Ensure this directory exists and contains sample images
    try:
        return [
            os.path.join(sample_dir, file)
            for file in os.listdir(sample_dir)
            if file.lower().endswith((".png", ".jpg", ".jpeg"))
        ]
    except FileNotFoundError:
        return []

# Load sample images
sample_images = get_sample_images()

# Gradio interface
demo = gr.Interface(
    fn=generate_caption,
    inputs=gr.Image(type="pil", label="Upload Image"),
    outputs=gr.Textbox(label="Generated Caption"),
    examples=sample_images,
    title="Image Captioning App",
    description=(
        "Upload an image or use one of the predefined samples to generate a caption. "
        "This app uses `Salesforce/blip-image-captioning-base` locally via Hugging Face Transformers."
    ),
    flagging_mode="never"
)

if __name__ == "__main__":
    demo.launch()