saivarun04's picture
Update app.py
aa3917f verified
import os
import gradio as gr
from PIL import Image
import google.generativeai as genai
# Load API key from Hugging Face Spaces secret
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
if not GOOGLE_API_KEY:
raise ValueError("GOOGLE_API_KEY environment variable not found. Please set it in Hugging Face Spaces.")
# Configure Gemini
genai.configure(api_key=GOOGLE_API_KEY)
# Initialize the Gemini 1.5 Flash model
model = genai.GenerativeModel(model_name="models/gemini-1.5-flash")
def generate_caption(image):
if image is None:
return "Please upload an image."
try:
# Send image + prompt to Gemini
response = model.generate_content([
"Describe this image in one concise, natural sentence.",
image
])
return response.text.strip()
except Exception as e:
return f"Error: {str(e)}"
# Gradio interface
interface = gr.Interface(
fn=generate_caption,
inputs=gr.Image(type="pil"),
outputs="text",
title="🧠 Image Caption Generator",
description="Upload an image to get a caption generated"
)
# Corrected this line
if __name__ == "__main__":
interface.launch()