Spaces:
Sleeping
Sleeping
File size: 3,161 Bytes
55a3974 d96b3d4 55a3974 |
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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
import gradio as gr
from google import genai
from google.genai import types
import os
from PIL import Image
import io
API_KEY = os.environ.get("GOOGLE_API_KEY")
client = genai.Client(api_key=API_KEY)
def generate_image(description: str, style: str) -> Image.Image | None:
"""Generates an image using Gemini 2.0 Flash based on description and style."""
if not description:
print("Description is empty. Cannot generate image.")
return None
# Construct the prompt for the image generation model
image_prompt = f"""
Please create an illustration based on the following description and style.
- Description: {description}
- Style: {style}
- Format: High resolution, detailed image.
- Aspect Ratio: Square (1:1) is preferred, but follow the description if it implies a different ratio.
- **DO NOT** include any text in the image.
"""
print(f"Generating image with prompt: {image_prompt}")
try:
# Call the Gemini API
image_response = client.models.generate_content(
model="gemini-2.0-flash-preview-image-generation",
contents=image_prompt,
config=types.GenerateContentConfig(
response_modalities=['TEXT', 'IMAGE']
)
)
# Process the response to extract the image
if image_response and image_response.candidates and image_response.candidates[0].content.parts:
for part in image_response.candidates[0].content.parts:
if part.inline_data and part.inline_data.mime_type.startswith('image/'):
image_data_base64 = part.inline_data.data
img = Image.open(io.BytesIO(image_data_base64))
img = img.resize((512, 512), Image.Resampling.LANCZOS)
print("Image generated successfully.")
return img
print("Image data not found in any response parts or not an image.")
return None
else:
print("Image generation response is empty or malformed.")
return None
except Exception as e:
print(f"Error generating image: {e}")
# Handle specific API errors if needed
return None
# Define available styles for the dropdown
styles = [
"Sketch - 草圖",
"Watercolor - 水彩",
"Oil Painting - 油畫",
"Digital Art - 數位藝術",
"Cartoon - 卡通",
"Photorealistic - 寫實",
"Abstract - 抽象派",
"Pixel Art - 像素藝術",
"Anime - 動畫",
"Impressionist - 印象派"
]
# Create the Gradio interface
app = gr.Interface(
fn=generate_image,
inputs=[
gr.Textbox(label="✏️ Enter image description", lines=3, interactive=True),
gr.Dropdown(choices=styles, label="🎨 Select Style", value="Sketch - 草圖") # Default style
],
outputs=gr.Image(label="Generated Image"),
title="🤖 Talk to Draw! 🎨",
description="Collaborate with AI to draw images from text or voice descriptions with different styles, powered by Google Gemini 2.0 Flash.",
flagging_mode="never" # Disable flagging feature
)
app.launch() |