File size: 1,167 Bytes
663d79a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aa3917f
 
 
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
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()