image2caption / app.py
chatbotdeveloper2025genai's picture
Upload folder using huggingface_hub
8cf4ded verified
Raw History Blame Contribute Delete
2.51 kB
# 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)))