MySafeCode commited on
Commit
91ec76c
·
verified ·
1 Parent(s): d5c10df

Upload 3 files

Browse files
Files changed (1) hide show
  1. app.py +130 -136
app.py CHANGED
@@ -137,138 +137,72 @@ def call_pixazo_api(prompt, num_steps=4, seed=None, height=512, width=512,
137
  "timestamp": datetime.now().isoformat()
138
  }
139
 
140
- # Generate images with streaming updates
141
- def generate_images_stream(prompt, num_steps, seed, height, width, style_preset,
142
- num_images, guidance_scale):
143
 
144
- images = []
145
- all_results = []
146
-
147
- for i in range(num_images):
148
- try:
149
- current_seed = seed + i if seed > 0 else 0
150
-
151
- image, result = call_pixazo_api(
152
- prompt=prompt,
153
- num_steps=num_steps,
154
- seed=current_seed,
155
- height=height,
156
- width=width,
157
- style_preset=style_preset,
158
- guidance_scale=guidance_scale
159
- )
160
-
161
- if image:
162
- images.append(image)
163
-
164
- all_results.append(result)
165
-
166
- # Yield intermediate results for streaming
167
- status_text = f"Generated {i+1}/{num_images} images"
168
- if "error" in result:
169
- status_text = f"Error on image {i+1}: {result['error'][:50]}..."
170
-
171
- yield {
172
- gallery: images if images else None,
173
- status: status_text,
174
- json_output: result
175
- }
176
-
177
- # Small delay between requests
178
- if i < num_images - 1:
179
- time.sleep(1)
180
-
181
- except Exception as e:
182
- error_result = {"error": str(e), "image_index": i, "timestamp": datetime.now().isoformat()}
183
- all_results.append(error_result)
184
- yield {
185
- gallery: images if images else None,
186
- status: f"Exception on image {i+1}: {str(e)[:50]}",
187
- json_output: error_result
188
- }
189
- break
190
-
191
- # Final summary
192
- success_count = len(images)
193
-
194
- if success_count == 0:
195
- final_status = f"❌ No images generated ({success_count}/{num_images} successful)"
196
- elif success_count < num_images:
197
- final_status = f"⚠️ Partial success: {success_count}/{num_images} images"
198
- else:
199
- final_status = f"🎉 Success! All {success_count} images generated"
200
-
201
- summary = {
202
- "summary": {
203
- "total_attempts": num_images,
204
- "successful": success_count,
205
- "failed": num_images - success_count,
206
- "timestamp": datetime.now().isoformat()
207
- },
208
- "parameters": {
209
- "prompt": prompt[:100] + "..." if len(prompt) > 100 else prompt,
210
- "steps": num_steps,
211
- "seed_used": seed,
212
- "resolution": f"{width}x{height}",
213
- "style": style_preset,
214
- "num_images": num_images,
215
- "guidance_scale": guidance_scale
216
- }
217
- }
218
-
219
- yield {
220
- gallery: images if images else None,
221
- status: final_status,
222
- json_output: summary
223
- }
224
-
225
- # Create the Gradio interface
226
- with gr.Blocks(
227
- title="Pixazo Image Generator",
228
- theme=gr.themes.Soft(primary_hue="purple", secondary_hue="pink"),
229
- css="""
230
  .success { color: #10b981 !important; }
231
  .error { color: #ef4444 !important; }
232
  .warning { color: #f59e0b !important; }
233
- .container {
234
- max-width: 1200px;
235
- margin: 0 auto;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
236
  }
237
  """
238
- ) as demo:
239
 
240
  # Header
241
  gr.Markdown("""
242
- <div style="text-align: center;">
243
- <h1>🎨 Pixazo AI Image Generator</h1>
244
- <p>Create stunning images with FLUX-1 Schnell model</p>
245
- </div>
246
  """)
247
 
248
  # API Status
249
  api_key = get_api_key()
250
  if api_key:
251
- status_html = """
252
- <div style="background: #d1fae5; padding: 10px 15px; border-radius: 8px; border-left: 4px solid #10b981; margin-bottom: 20px;">
253
  <strong>✅ API Status:</strong> Connected (API key configured)
254
  </div>
255
- """
256
  else:
257
- status_html = """
258
- <div style="background: #fee2e2; padding: 15px; border-radius: 8px; border-left: 4px solid #ef4444; margin-bottom: 20px;">
259
  <strong>❌ API Status:</strong> Not Configured
260
  <p style="margin: 8px 0 0 0; font-size: 0.9em;">
261
  <strong>Setup:</strong> Go to Space Settings → Repository secrets → Add: <code>PIXAZO_API_KEY</code> = your key
262
  </p>
263
  </div>
264
- """
265
-
266
- gr.HTML(status_html)
267
 
268
  with gr.Row():
269
  # Left column - Inputs
270
  with gr.Column(scale=1, min_width=400):
271
- with gr.Group(label="Generation Settings"):
 
 
 
272
  prompt = gr.Textbox(
273
  label="Image Prompt",
274
  placeholder="Describe what you want to generate...",
@@ -289,7 +223,7 @@ with gr.Blocks(
289
  )
290
 
291
  guidance_scale = gr.Slider(
292
- label="Guidance",
293
  minimum=1.0,
294
  maximum=20.0,
295
  value=7.5,
@@ -299,7 +233,7 @@ with gr.Blocks(
299
 
300
  with gr.Row():
301
  num_steps = gr.Slider(
302
- label="Steps",
303
  minimum=1,
304
  maximum=50,
305
  value=4,
@@ -358,18 +292,19 @@ with gr.Blocks(
358
 
359
  for example_prompt, example_style in examples:
360
  with gr.Row():
361
- gr.Textbox(
362
  value=example_prompt,
363
  show_label=False,
364
  interactive=False,
365
  scale=3
366
  )
367
- gr.Button(
368
- "Use",
369
- size="sm",
370
- scale=1
371
- ).click(
372
- lambda p=example_prompt, s=example_style: (p, s),
 
373
  outputs=[prompt, style_preset]
374
  )
375
 
@@ -406,33 +341,90 @@ with gr.Blocks(
406
  """)
407
 
408
  # Event handlers
409
- @generate_btn.click(inputs=[prompt, num_steps, seed, height, width, style_preset, num_images, guidance_scale])
410
  def on_generate(prompt, num_steps, seed, height, width, style_preset, num_images, guidance_scale):
411
  if not prompt.strip():
412
- return {
413
- status: "❌ Please enter a prompt first",
414
- gallery: None,
415
- json_output: {"error": "No prompt provided"}
416
- }
417
 
418
- # Show initial status
419
- yield {
420
- status: f"🚀 Starting generation of {num_images} image(s)...",
421
- gallery: None,
422
- json_output: {"status": "starting", "num_images": num_images}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
423
  }
424
 
425
- # Stream generation results
426
- for update in generate_images_stream(prompt, num_steps, seed, height, width, style_preset, num_images, guidance_scale):
427
- yield update
428
 
429
- @clear_btn.click()
430
  def on_clear():
431
- return {
432
- status: "Ready to generate images...",
433
- gallery: None,
434
- json_output: None
435
- }
 
 
 
 
 
 
 
 
436
 
437
  # Launch the app
438
  if __name__ == "__main__":
@@ -446,5 +438,7 @@ if __name__ == "__main__":
446
  server_name="0.0.0.0",
447
  server_port=7860,
448
  share=False,
449
- show_api=False
 
 
450
  )
 
137
  "timestamp": datetime.now().isoformat()
138
  }
139
 
140
+ # Create the Gradio interface with correct Gradio 6.x API
141
+ with gr.Blocks(title="Pixazo Image Generator") as demo:
 
142
 
143
+ # Custom CSS
144
+ demo.css = """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
145
  .success { color: #10b981 !important; }
146
  .error { color: #ef4444 !important; }
147
  .warning { color: #f59e0b !important; }
148
+ .api-status {
149
+ padding: 12px 16px;
150
+ border-radius: 8px;
151
+ margin-bottom: 20px;
152
+ }
153
+ .api-status-success {
154
+ background: #d1fae5;
155
+ border-left: 4px solid #10b981;
156
+ }
157
+ .api-status-error {
158
+ background: #fee2e2;
159
+ border-left: 4px solid #ef4444;
160
+ }
161
+ .example-prompt {
162
+ cursor: pointer;
163
+ padding: 10px;
164
+ border-radius: 6px;
165
+ margin: 5px 0;
166
+ background: #f8fafc;
167
+ border: 1px solid #e2e8f0;
168
+ }
169
+ .example-prompt:hover {
170
+ background: #e2e8f0;
171
  }
172
  """
 
173
 
174
  # Header
175
  gr.Markdown("""
176
+ # 🎨 Pixazo AI Image Generator
177
+
178
+ Create stunning images with FLUX-1 Schnell model
 
179
  """)
180
 
181
  # API Status
182
  api_key = get_api_key()
183
  if api_key:
184
+ gr.Markdown("""
185
+ <div class="api-status api-status-success">
186
  <strong>✅ API Status:</strong> Connected (API key configured)
187
  </div>
188
+ """)
189
  else:
190
+ gr.Markdown("""
191
+ <div class="api-status api-status-error">
192
  <strong>❌ API Status:</strong> Not Configured
193
  <p style="margin: 8px 0 0 0; font-size: 0.9em;">
194
  <strong>Setup:</strong> Go to Space Settings → Repository secrets → Add: <code>PIXAZO_API_KEY</code> = your key
195
  </p>
196
  </div>
197
+ """)
 
 
198
 
199
  with gr.Row():
200
  # Left column - Inputs
201
  with gr.Column(scale=1, min_width=400):
202
+ # Generation Settings group
203
+ with gr.Group():
204
+ gr.Markdown("### 🎨 Generation Settings")
205
+
206
  prompt = gr.Textbox(
207
  label="Image Prompt",
208
  placeholder="Describe what you want to generate...",
 
223
  )
224
 
225
  guidance_scale = gr.Slider(
226
+ label="Guidance Scale",
227
  minimum=1.0,
228
  maximum=20.0,
229
  value=7.5,
 
233
 
234
  with gr.Row():
235
  num_steps = gr.Slider(
236
+ label="Number of Steps",
237
  minimum=1,
238
  maximum=50,
239
  value=4,
 
292
 
293
  for example_prompt, example_style in examples:
294
  with gr.Row():
295
+ prompt_text = gr.Textbox(
296
  value=example_prompt,
297
  show_label=False,
298
  interactive=False,
299
  scale=3
300
  )
301
+ use_btn = gr.Button("Use", size="sm", scale=1)
302
+
303
+ def use_example(p=example_prompt, s=example_style):
304
+ return p, s
305
+
306
+ use_btn.click(
307
+ use_example,
308
  outputs=[prompt, style_preset]
309
  )
310
 
 
341
  """)
342
 
343
  # Event handlers
 
344
  def on_generate(prompt, num_steps, seed, height, width, style_preset, num_images, guidance_scale):
345
  if not prompt.strip():
346
+ yield "❌ Please enter a prompt first", None, {"error": "No prompt provided"}
347
+ return
348
+
349
+ images = []
350
+ all_results = []
351
 
352
+ yield "🚀 Starting generation...", None, {"status": "starting"}
353
+
354
+ for i in range(num_images):
355
+ try:
356
+ current_seed = seed + i if seed > 0 else 0
357
+
358
+ image, result = call_pixazo_api(
359
+ prompt=prompt,
360
+ num_steps=num_steps,
361
+ seed=current_seed,
362
+ height=height,
363
+ width=width,
364
+ style_preset=style_preset,
365
+ guidance_scale=guidance_scale
366
+ )
367
+
368
+ if image:
369
+ images.append(image)
370
+
371
+ all_results.append(result)
372
+
373
+ status_text = f"Generated {i+1}/{num_images} images"
374
+ if "error" in result:
375
+ status_text = f"Image {i+1} error: {result['error'][:50]}..."
376
+
377
+ yield status_text, images if images else None, result
378
+
379
+ if i < num_images - 1:
380
+ time.sleep(1)
381
+
382
+ except Exception as e:
383
+ error_result = {"error": str(e), "image_index": i}
384
+ all_results.append(error_result)
385
+ yield f"Exception on image {i+1}: {str(e)[:50]}", images if images else None, error_result
386
+ break
387
+
388
+ # Final summary
389
+ success_count = len(images)
390
+
391
+ if success_count == 0:
392
+ final_status = f"❌ No images generated ({success_count}/{num_images} successful)"
393
+ elif success_count < num_images:
394
+ final_status = f"⚠️ Partial success: {success_count}/{num_images} images"
395
+ else:
396
+ final_status = f"🎉 Success! All {success_count} images generated"
397
+
398
+ summary = {
399
+ "summary": {
400
+ "total_attempts": num_images,
401
+ "successful": success_count,
402
+ "failed": num_images - success_count
403
+ },
404
+ "parameters": {
405
+ "prompt_length": len(prompt),
406
+ "steps": num_steps,
407
+ "resolution": f"{width}x{height}",
408
+ "style": style_preset
409
+ }
410
  }
411
 
412
+ yield final_status, images if images else None, summary
 
 
413
 
 
414
  def on_clear():
415
+ return "Ready to generate images...", None, None
416
+
417
+ # Connect buttons
418
+ generate_btn.click(
419
+ fn=on_generate,
420
+ inputs=[prompt, num_steps, seed, height, width, style_preset, num_images, guidance_scale],
421
+ outputs=[status, gallery, json_output]
422
+ )
423
+
424
+ clear_btn.click(
425
+ fn=on_clear,
426
+ outputs=[status, gallery, json_output]
427
+ )
428
 
429
  # Launch the app
430
  if __name__ == "__main__":
 
438
  server_name="0.0.0.0",
439
  server_port=7860,
440
  share=False,
441
+ show_api=False,
442
+ theme=gr.themes.Soft(primary_hue="purple", secondary_hue="pink"),
443
+ css=demo.css if hasattr(demo, 'css') else ""
444
  )