File size: 2,509 Bytes
722950b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
63
64
65
66
67
68
69
70
71
72
73
74
75
# app.py
import os
import io
import base64
from PIL import Image as PImage
from transformers import pipeline
import gradio as gr
from dotenv import load_dotenv  # pip install python-dotenv

# -------------------------------
# 1️⃣ Load environment variables from .env
# -------------------------------
load_dotenv()
hf_api_key = os.getenv("HF_API_KEY")
if not hf_api_key:
    raise ValueError("HF_API_KEY not found in .env file")

os.environ["HF_API_KEY"] = hf_api_key

# -------------------------------
# 2️⃣ Initialize BLIP pipeline
# -------------------------------
get_completion = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")

# -------------------------------
# 3️⃣ Helper function: PIL -> base64
# -------------------------------
def image_to_base64_str(pil_image):
    byte_arr = io.BytesIO()
    pil_image.save(byte_arr, format='PNG')
    byte_arr = byte_arr.getvalue()
    return base64.b64encode(byte_arr).decode('utf-8')

# -------------------------------
# 4️⃣ Generate caption from base64 image
# -------------------------------
def generate_caption(base64_image):
    image_data = base64.b64decode(base64_image)
    image = PImage.open(io.BytesIO(image_data))
    caption_result = get_completion(image)
    if isinstance(caption_result, list) and len(caption_result) > 0:
        return caption_result[0].get('generated_text', "No caption generated")
    else:
        return "No caption generated"

# -------------------------------
# 5️⃣ Captioner function for Gradio
# -------------------------------
def captioner(image):
    base64_image = image_to_base64_str(image)
    caption_text = generate_caption(base64_image)
    print("Caption:", caption_text)
    return caption_text

# -------------------------------
# 6️⃣ Example images (must be in project folder)
# -------------------------------
examples = ["christmas_dog.jpeg", "bird_flight.jpeg", "cow.jpeg", "dog_animal_greyhound.jpeg"]

# -------------------------------
# 7️⃣ Gradio Interface
# -------------------------------
demo = gr.Interface(
    fn=captioner,
    inputs=[gr.Image(label="Upload Image", type="pil")],
    outputs=[gr.Textbox(label="Caption")],
    title="Image Captioning with BLIP",
    description="Caption any image using the BLIP model",
    allow_flagging="never",
    examples=examples
)

# Launch the app
demo.launch(share=True, server_port=int(os.environ.get('PORT', 7860)))