leave-everything commited on
Commit
9944771
·
verified ·
1 Parent(s): 1e1f199

Upload 4 files

Browse files
Files changed (4) hide show
  1. .gitignore +35 -13
  2. README.md +128 -0
  3. app.py +187 -0
  4. requirements.txt +3 -20
.gitignore CHANGED
@@ -1,19 +1,31 @@
1
- # Environment variables
2
- .env
3
- .env.local
4
-
5
- # Generated images
6
- generated_images/
7
-
8
  # Python
9
  __pycache__/
10
  *.py[cod]
11
  *$py.class
12
  *.so
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
- # Virtual environment
 
15
  venv/
16
- env/
17
  ENV/
18
 
19
  # IDE
@@ -21,14 +33,24 @@ ENV/
21
  .idea/
22
  *.swp
23
  *.swo
24
-
25
- # OS
26
  .DS_Store
27
- Thumbs.db
 
 
 
 
 
 
 
 
 
28
 
29
  # Logs
30
  *.log
 
31
 
32
  # Temporary files
33
  *.tmp
34
- temp/
 
 
 
 
 
 
 
 
 
1
  # Python
2
  __pycache__/
3
  *.py[cod]
4
  *$py.class
5
  *.so
6
+ .Python
7
+ env/
8
+ venv/
9
+ ENV/
10
+ build/
11
+ develop-eggs/
12
+ dist/
13
+ downloads/
14
+ eggs/
15
+ .eggs/
16
+ lib/
17
+ lib64/
18
+ parts/
19
+ sdist/
20
+ var/
21
+ wheels/
22
+ *.egg-info/
23
+ .installed.cfg
24
+ *.egg
25
 
26
+ # Virtual Environment
27
+ .venv/
28
  venv/
 
29
  ENV/
30
 
31
  # IDE
 
33
  .idea/
34
  *.swp
35
  *.swo
36
+ *~
 
37
  .DS_Store
38
+
39
+ # Gradio
40
+ gradio_cached_examples/
41
+ flagged/
42
+
43
+ # API Keys and Secrets
44
+ .env
45
+ .env.local
46
+ *.key
47
+ secrets.json
48
 
49
  # Logs
50
  *.log
51
+ logs/
52
 
53
  # Temporary files
54
  *.tmp
55
+ temp/
56
+ tmp/
README.md ADDED
@@ -0,0 +1,128 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Gemini Image Generation
3
+ emoji: 🎨
4
+ colorFrom: blue
5
+ colorTo: purple
6
+ sdk: gradio
7
+ sdk_version: 5.49.1
8
+ app_file: app.py
9
+ pinned: false
10
+ license: mit
11
+ ---
12
+
13
+ # 🎨 Gemini Image Generation App
14
+
15
+ A simple and intuitive Gradio application for generating images using Google's Gemini image models (Nano Banana 🍌).
16
+
17
+ ## Features
18
+
19
+ - 🔑 Secure API key input
20
+ - 🤖 **Model Selection**: Choose between Gemini 3 Pro Image (Nano Banana Pro) or 2.5 Flash Image (Nano Banana)
21
+ - 📝 Text-to-image generation with state-of-the-art quality
22
+ - 📐 Multiple aspect ratio options (1:1, 16:9, 9:16, 4:3, 3:4)
23
+ - 💡 Example prompts for inspiration
24
+ - 🎨 Clean and modern UI
25
+
26
+ ## Models Available
27
+
28
+ ### 🍌⭐ Gemini 3 Pro Image (Nano Banana Pro)
29
+ - **Release**: November 20, 2025
30
+ - **Best for**: Professional-grade image generation
31
+ - **Features**: High-resolution (1K/2K/4K), advanced text rendering, complex compositions
32
+ - **Model ID**: `gemini-3-pro-image-preview`
33
+
34
+ ### 🍌⚡ Gemini 2.5 Flash Image (Nano Banana)
35
+ - **Release**: August 26, 2025
36
+ - **Best for**: Fast, efficient image generation
37
+ - **Features**: 1024px resolution, 2-3x faster than competitors, high-volume tasks
38
+ - **Model ID**: `gemini-2.5-flash-image`
39
+
40
+ ## How to Use
41
+
42
+ 1. **Get API Key**: Visit [Google AI Studio](https://aistudio.google.com/app/apikey) to obtain your API key
43
+ 2. **Enter API Key**: Paste your API key in the secure input field
44
+ 3. **Select Model**: Choose between Gemini 3 Pro (quality) or 2.5 Flash (speed)
45
+ 4. **Write Prompt**: Describe the image you want to generate in detail
46
+ 5. **Select Aspect Ratio**: Choose your preferred aspect ratio
47
+ 6. **Generate**: Click the "Generate Image" button and wait for your image
48
+
49
+ ## Tips for Better Results
50
+
51
+ - Be specific and detailed in your prompts
52
+ - Include information about:
53
+ - Style (realistic, artistic, abstract, etc.)
54
+ - Mood and atmosphere
55
+ - Lighting conditions
56
+ - Color palette
57
+ - Composition details
58
+ - Reference specific artists or art movements if desired
59
+ - Experiment with different aspect ratios for your use case
60
+
61
+ ## Requirements
62
+
63
+ - Google AI API key (free tier available)
64
+ - Internet connection
65
+ - Modern web browser
66
+
67
+ ## Technical Details
68
+
69
+ - **Models**:
70
+ - Gemini 3 Pro Image (`gemini-3-pro-image-preview`)
71
+ - Gemini 2.5 Flash Image (`gemini-2.5-flash-image`)
72
+ - **Framework**: Gradio 5.49.1 (stable)
73
+ - **API**: Google GenAI Python SDK (google-genai 1.52.0+)
74
+ - **Deployment**: Hugging Face Spaces
75
+ - **Python**: 3.10+
76
+
77
+ ## Privacy & Security
78
+
79
+ - Your API key is not stored or logged
80
+ - API key is only used for the current generation request
81
+ - All processing happens through Google's secure API
82
+ - No image data is stored by this application
83
+
84
+ ## Local Development
85
+
86
+ To run this app locally:
87
+
88
+ ```bash
89
+ # Clone the repository
90
+ git clone <repository-url>
91
+ cd <repository-name>
92
+
93
+ # Install dependencies
94
+ pip install -r requirements.txt
95
+
96
+ # Run the app
97
+ python app.py
98
+ ```
99
+
100
+ The app will be available at `http://localhost:7860`
101
+
102
+ ## API Costs
103
+
104
+ Image generation using Gemini API may incur costs. Please refer to [Google's pricing page](https://ai.google.dev/pricing) for current rates.
105
+
106
+ ## Limitations
107
+
108
+ - Generation time: 10-30 seconds per image
109
+ - Rate limits apply based on your API tier
110
+ - Content policy restrictions apply
111
+ - Some prompts may be filtered for safety
112
+
113
+ ## Support
114
+
115
+ For issues or questions:
116
+ - Check [Google AI documentation](https://ai.google.dev/)
117
+ - Review [Gradio documentation](https://www.gradio.app/docs)
118
+ - Open an issue in this repository
119
+
120
+ ## License
121
+
122
+ MIT License - feel free to use and modify for your projects
123
+
124
+ ## Acknowledgments
125
+
126
+ - Google GenAI SDK for unified API access to Imagen models
127
+ - Gradio for the modern web interface framework
128
+ - Hugging Face for hosting platform
app.py ADDED
@@ -0,0 +1,187 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from google import genai
3
+ from google.genai import types
4
+ from PIL import Image
5
+ import io
6
+
7
+ # Model configuration
8
+ MODELS = {
9
+ "Gemini 3 Pro Image (Nano Banana Pro) 🍌⭐": "gemini-3-pro-image-preview",
10
+ "Gemini 2.5 Flash Image (Nano Banana) 🍌⚡": "gemini-2.5-flash-image",
11
+ }
12
+
13
+
14
+ def generate_image(api_key, prompt, model_name, aspect_ratio, safety_filter):
15
+ """
16
+ Generate image using Google GenAI API (Gemini image models)
17
+
18
+ Args:
19
+ api_key: Google AI API key
20
+ prompt: Text description for image generation
21
+ model_name: Selected model name
22
+ aspect_ratio: Aspect ratio for the image
23
+ safety_filter: Safety filter level
24
+
25
+ Returns:
26
+ Generated image or error message
27
+ """
28
+ if not api_key:
29
+ return None, "❌ Please enter your API key"
30
+
31
+ if not prompt:
32
+ return None, "❌ Please enter a prompt"
33
+
34
+ try:
35
+ # Create GenAI client
36
+ client = genai.Client(api_key=api_key)
37
+
38
+ # Get model ID from selection
39
+ model_id = MODELS[model_name]
40
+
41
+ # Configure image generation settings
42
+ config = types.GenerateContentConfig(
43
+ response_modalities=["IMAGE"],
44
+ image_config=types.ImageConfig(
45
+ aspect_ratio=aspect_ratio,
46
+ ),
47
+ )
48
+
49
+ # Generate image using Gemini image model
50
+ response = client.models.generate_content(
51
+ model=model_id,
52
+ contents=[prompt],
53
+ config=config
54
+ )
55
+
56
+ # Extract and return the generated image
57
+ for part in response.parts:
58
+ if part.inline_data is not None:
59
+ image = part.as_image()
60
+ return image, f"✅ Image generated successfully!\nModel: {model_name}\nPrompt: {prompt}\nAspect Ratio: {aspect_ratio}"
61
+
62
+ return None, "❌ No image was generated. Please try again with a different prompt."
63
+
64
+ except Exception as e:
65
+ error_msg = str(e)
66
+ if "API_KEY_INVALID" in error_msg or "invalid api key" in error_msg.lower():
67
+ return None, "❌ Invalid API key. Please check your API key and try again."
68
+ elif "QUOTA_EXCEEDED" in error_msg or "quota" in error_msg.lower():
69
+ return None, "❌ Quota exceeded. Please check your API usage limits."
70
+ elif "permission" in error_msg.lower():
71
+ return None, "❌ Permission denied. Please ensure your API key has access to Gemini image models."
72
+ else:
73
+ return None, f"❌ Error: {error_msg}"
74
+
75
+
76
+ # Create Gradio interface
77
+ with gr.Blocks(title="Gemini Image Generation", theme=gr.themes.Soft()) as demo:
78
+ gr.Markdown(
79
+ """
80
+ # 🎨 Gemini Image Generation App
81
+
82
+ Generate images using Google's Gemini image models (Nano Banana 🍌).
83
+
84
+ **How to use:**
85
+ 1. Get your API key from [Google AI Studio](https://aistudio.google.com/app/apikey)
86
+ 2. Enter your API key below
87
+ 3. Select your preferred model (3 Pro or 2.5 Flash)
88
+ 4. Write a detailed prompt describing the image you want
89
+ 5. Select aspect ratio
90
+ 6. Click "Generate Image"
91
+ """
92
+ )
93
+
94
+ with gr.Row():
95
+ with gr.Column(scale=1):
96
+ api_key_input = gr.Textbox(
97
+ label="🔑 API Key",
98
+ type="password",
99
+ placeholder="Enter your Google AI API key",
100
+ info="Your API key is not stored and only used for this generation"
101
+ )
102
+
103
+ model_input = gr.Dropdown(
104
+ label="🤖 Model",
105
+ choices=list(MODELS.keys()),
106
+ value="Gemini 3 Pro Image (Nano Banana Pro) 🍌⭐",
107
+ info="Gemini 3 Pro: Professional quality | 2.5 Flash: Faster generation"
108
+ )
109
+
110
+ prompt_input = gr.Textbox(
111
+ label="📝 Prompt",
112
+ placeholder="A serene landscape with mountains and a lake at sunset...",
113
+ lines=5,
114
+ info="Describe the image you want to generate"
115
+ )
116
+
117
+ aspect_ratio_input = gr.Dropdown(
118
+ label="📐 Aspect Ratio",
119
+ choices=["1:1", "16:9", "9:16", "4:3", "3:4"],
120
+ value="1:1",
121
+ info="Select the aspect ratio for your image"
122
+ )
123
+
124
+ safety_filter_input = gr.Dropdown(
125
+ label="🛡️ Safety Filter",
126
+ choices=["Default", "Low", "High"],
127
+ value="Default",
128
+ info="Content safety filter level"
129
+ )
130
+
131
+ generate_btn = gr.Button("🎨 Generate Image", variant="primary", size="lg")
132
+
133
+ with gr.Column(scale=1):
134
+ output_image = gr.Image(
135
+ label="Generated Image",
136
+ type="pil",
137
+ height=500
138
+ )
139
+ status_output = gr.Textbox(
140
+ label="Status",
141
+ lines=3,
142
+ interactive=False
143
+ )
144
+
145
+ gr.Markdown(
146
+ """
147
+ ---
148
+ ### 💡 Tips for better results:
149
+ - **Choose the right model**: Gemini 3 Pro for professional quality, 2.5 Flash for speed
150
+ - Be specific and detailed in your prompts
151
+ - Include style, mood, lighting, and composition details
152
+ - Mention specific artists or art styles if desired
153
+ - Experiment with different aspect ratios
154
+
155
+ ### 🍌 Model Comparison:
156
+ - **Gemini 3 Pro Image (Nano Banana Pro)**: Professional-grade, high-resolution (1K/2K/4K), advanced text rendering
157
+ - **Gemini 2.5 Flash Image (Nano Banana)**: Fast generation, 1024px resolution, efficient for high-volume tasks
158
+
159
+ ### ⚠️ Notes:
160
+ - API key is required for each generation
161
+ - Generation may take 10-30 seconds
162
+ - API usage may incur costs based on Google's pricing
163
+ """
164
+ )
165
+
166
+ # Connect generate button to function
167
+ generate_btn.click(
168
+ fn=generate_image,
169
+ inputs=[api_key_input, prompt_input, model_input, aspect_ratio_input, safety_filter_input],
170
+ outputs=[output_image, status_output]
171
+ )
172
+
173
+ # Example prompts
174
+ gr.Examples(
175
+ examples=[
176
+ ["A futuristic cityscape at night with neon lights and flying cars", "16:9"],
177
+ ["A cute robot playing with a kitten in a garden", "1:1"],
178
+ ["An abstract painting with vibrant colors and geometric shapes", "4:3"],
179
+ ["A serene Japanese zen garden with cherry blossoms", "16:9"],
180
+ ],
181
+ inputs=[prompt_input, aspect_ratio_input],
182
+ label="Example Prompts"
183
+ )
184
+
185
+
186
+ if __name__ == "__main__":
187
+ demo.launch()
requirements.txt CHANGED
@@ -1,20 +1,3 @@
1
- # Core dependencies
2
- gradio==4.19.2
3
- fastapi
4
- uvicorn[standard]>=0.20.0
5
-
6
- # Google Gemini API (Nano Banana)
7
- google-generativeai>=0.8.0
8
-
9
- # Image processing
10
- pillow>=10.0.0
11
- numpy>=1.24.0
12
-
13
- # Utilities
14
- python-dotenv>=1.0.0
15
- aiofiles>=23.2.1
16
- requests>=2.28.0
17
-
18
- # Hugging Face integration
19
- huggingface_hub==0.19.4
20
- datasets>=2.14.0
 
1
+ gradio==5.49.1
2
+ google-genai>=1.52.0
3
+ Pillow>=10.0.0