aicodingfun commited on
Commit
55a3974
·
verified ·
1 Parent(s): 69aa00b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +89 -0
app.py ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from google import genai
3
+ from google.genai import types
4
+ import os
5
+ from PIL import Image
6
+ import io
7
+
8
+ API_KEY = os.environ.get("GOOGLE_API_KEY")
9
+ client = genai.Client(api_key=API_KEY)
10
+
11
+ def generate_image(description: str, style: str) -> Image.Image | None:
12
+ """Generates an image using Gemini 2.0 Flash based on description and style."""
13
+
14
+ if not description:
15
+ print("Description is empty. Cannot generate image.")
16
+ return None
17
+
18
+ # Construct the prompt for the image generation model
19
+ image_prompt = f"""
20
+ Please create an illustration based on the following description and style.
21
+ - Description: {description}
22
+ - Style: {style}
23
+ - Format: High resolution, detailed image.
24
+ - Aspect Ratio: Square (1:1) is preferred, but follow the description if it implies a different ratio.
25
+ - **DO NOT** include any text in the image.
26
+ """
27
+
28
+ print(f"Generating image with prompt: {image_prompt}")
29
+
30
+ try:
31
+ # Call the Gemini API
32
+ image_response = client.models.generate_content(
33
+ model="gemini-2.0-flash-preview-image-generation",
34
+ contents=image_prompt,
35
+ config=types.GenerateContentConfig(
36
+ response_modalities=['TEXT', 'IMAGE']
37
+ )
38
+ )
39
+
40
+ # Process the response to extract the image
41
+ if image_response and image_response.candidates and image_response.candidates[0].content.parts:
42
+ for part in image_response.candidates[0].content.parts:
43
+ if part.inline_data and part.inline_data.mime_type.startswith('image/'):
44
+ image_data_base64 = part.inline_data.data
45
+ img = Image.open(io.BytesIO(image_data_base64))
46
+
47
+ img = img.resize((512, 512), Image.Resampling.LANCZOS)
48
+ print("Image generated successfully.")
49
+ return img
50
+
51
+ print("Image data not found in any response parts or not an image.")
52
+ return None
53
+ else:
54
+ print("Image generation response is empty or malformed.")
55
+ return None
56
+
57
+ except Exception as e:
58
+ print(f"Error generating image: {e}")
59
+ # Handle specific API errors if needed
60
+ return None
61
+
62
+ # Define available styles for the dropdown
63
+ styles = [
64
+ "Sketch - 草圖",
65
+ "Watercolor - 水彩",
66
+ "Oil Painting - 油畫",
67
+ "Digital Art - 數位藝術",
68
+ "Cartoon - 卡通",
69
+ "Photorealistic - 寫實",
70
+ "Abstract - 抽象派",
71
+ "Pixel Art - 像素藝術",
72
+ "Anime - 動畫",
73
+ "Impressionist - 印象派"
74
+ ]
75
+
76
+ # Create the Gradio interface
77
+ app = gr.Interface(
78
+ fn=generate_image,
79
+ inputs=[
80
+ gr.Textbox(label="✏️ Enter image description", lines=3, interactive=True),
81
+ gr.Dropdown(choices=styles, label="🎨 Select Style", value="Sketch") # Default style
82
+ ],
83
+ outputs=gr.Image(label="Generated Image"),
84
+ title="🤖 Talk to Draw! 🎨",
85
+ description="Collaborate with AI to draw images from text or voice descriptions with different styles, powered by Google Gemini 2.0 Flash.",
86
+ flagging_mode="never" # Disable flagging feature
87
+ )
88
+
89
+ app.launch()