Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import google.generativeai as genai
|
| 3 |
+
from google.generativeai import types
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import io
|
| 6 |
+
import os # Import os for environment variable check (useful for local testing)
|
| 7 |
+
|
| 8 |
+
# --- Configuration ---
|
| 9 |
+
|
| 10 |
+
# Configure the Gemini API key.
|
| 11 |
+
# Streamlit Secrets is the recommended way for Hugging Face Spaces.
|
| 12 |
+
# Add your API key to `.streamlit/secrets.toml` like this:
|
| 13 |
+
# GEMINI_API_KEY = "YOUR_API_KEY_HERE"
|
| 14 |
+
# For local testing, you might also set it as an environment variable.
|
| 15 |
+
try:
|
| 16 |
+
# Prefer Streamlit secrets if available
|
| 17 |
+
if "GEMINI_API_KEY" in st.secrets:
|
| 18 |
+
genai.configure(api_key=st.secrets["GEMINI_API_KEY"])
|
| 19 |
+
st.success("Gemini API key loaded from secrets.")
|
| 20 |
+
elif os.getenv("GEMINI_API_KEY"):
|
| 21 |
+
# Fallback to environment variable (useful for local dev without secrets)
|
| 22 |
+
genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
|
| 23 |
+
st.warning("Gemini API key loaded from environment variable. Use Streamlit Secrets for deployment.")
|
| 24 |
+
else:
|
| 25 |
+
st.error("GEMINI_API_KEY not found in Streamlit secrets or environment variables.")
|
| 26 |
+
st.stop() # Stop execution if API key is missing
|
| 27 |
+
|
| 28 |
+
except Exception as e:
|
| 29 |
+
st.error(f"Failed to configure Gemini API: {e}")
|
| 30 |
+
st.stop()
|
| 31 |
+
|
| 32 |
+
# Define the model name
|
| 33 |
+
MODEL_NAME = "gemini-2.0-flash-exp" # Using the model from your code
|
| 34 |
+
|
| 35 |
+
# --- Streamlit UI ---
|
| 36 |
+
|
| 37 |
+
st.title("🎨 Gemini Native Image Generation")
|
| 38 |
+
st.markdown("Enter a detailed prompt below to generate an image using the Gemini API.")
|
| 39 |
+
|
| 40 |
+
# Input area for the prompt
|
| 41 |
+
prompt = st.text_area(
|
| 42 |
+
"Enter your image prompt:",
|
| 43 |
+
height=150,
|
| 44 |
+
placeholder="e.g., A spaceship landing on a alien planet, digital art"
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
# Button to trigger generation
|
| 48 |
+
generate_button = st.button("Generate Image")
|
| 49 |
+
|
| 50 |
+
# --- Generation Logic ---
|
| 51 |
+
|
| 52 |
+
if generate_button and prompt:
|
| 53 |
+
# Show a spinner while generating
|
| 54 |
+
with st.spinner("Generating image... Please wait."):
|
| 55 |
+
try:
|
| 56 |
+
# Call the Gemini API
|
| 57 |
+
response = genai.generate_content(
|
| 58 |
+
model=MODEL_NAME,
|
| 59 |
+
# Pass the prompt as a list within contents
|
| 60 |
+
contents=[prompt],
|
| 61 |
+
config=types.GenerateContentConfig(
|
| 62 |
+
response_modalities=["Image"] # Only request Image modality
|
| 63 |
+
),
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
# Check if the response has candidates and content parts
|
| 67 |
+
if response.candidates and response.candidates[0].content.parts:
|
| 68 |
+
# Find the image part (inline_data)
|
| 69 |
+
img_part = None
|
| 70 |
+
for part in response.candidates[0].content.parts:
|
| 71 |
+
if part.inline_data:
|
| 72 |
+
img_part = part.inline_data
|
| 73 |
+
break # Found the image part
|
| 74 |
+
|
| 75 |
+
if img_part:
|
| 76 |
+
img_data = img_part.data # This is the bytes data
|
| 77 |
+
mime_type = img_part.mime_type # This is the mime type (e.g., 'image/png')
|
| 78 |
+
|
| 79 |
+
# Display the image using Streamlit's st.image
|
| 80 |
+
try:
|
| 81 |
+
# Use PIL to open the image from bytes data
|
| 82 |
+
image = Image.open(io.BytesIO(img_data))
|
| 83 |
+
st.image(image, caption="Generated Image", use_column_width=True)
|
| 84 |
+
st.success("Image generated successfully!")
|
| 85 |
+
|
| 86 |
+
# Optional: Add a download button
|
| 87 |
+
st.download_button(
|
| 88 |
+
label="Download Image",
|
| 89 |
+
data=img_data,
|
| 90 |
+
file_name="generated_image.png", # You might want a more dynamic name
|
| 91 |
+
mime=mime_type
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
except Exception as e:
|
| 95 |
+
st.error(f"Error processing or displaying image data: {e}")
|
| 96 |
+
# You could print raw data info for debugging:
|
| 97 |
+
# st.write(f"Raw data size: {len(img_data)} bytes, MIME: {mime_type}")
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
else:
|
| 101 |
+
st.warning("Generation successful, but no image data was returned for this prompt.")
|
| 102 |
+
# Check for text response if image wasn't found (though we requested only Image)
|
| 103 |
+
text_parts = [p.text for p in response.candidates[0].content.parts if p.text]
|
| 104 |
+
if text_parts:
|
| 105 |
+
st.write("Text response (no image found):")
|
| 106 |
+
for text in text_parts:
|
| 107 |
+
st.write(text)
|
| 108 |
+
|
| 109 |
+
else:
|
| 110 |
+
st.error("No valid response received from the API.")
|
| 111 |
+
# Print response for debugging if needed
|
| 112 |
+
# st.write(response)
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
except Exception as e:
|
| 116 |
+
st.error(f"An API error occurred during generation: {e}")
|
| 117 |
+
# Often API errors include details in the exception object
|
| 118 |
+
|
| 119 |
+
elif generate_button and not prompt:
|
| 120 |
+
st.warning("Please enter a prompt to generate an image.")
|