Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,43 +2,25 @@ import gradio as gr
|
|
| 2 |
import requests
|
| 3 |
import json
|
| 4 |
import os
|
| 5 |
-
import time
|
| 6 |
from PIL import Image
|
| 7 |
import io
|
| 8 |
-
import
|
| 9 |
-
import numpy as np
|
| 10 |
|
| 11 |
# Get API key from Hugging Face secrets
|
| 12 |
def get_api_key():
|
| 13 |
-
|
| 14 |
-
api_key = os.environ.get("PIXAZO_API_KEY") or \
|
| 15 |
-
os.environ.get("PIXAZO_KEY") or \
|
| 16 |
-
os.environ.get("API_KEY")
|
| 17 |
-
|
| 18 |
-
if not api_key:
|
| 19 |
-
# For local testing, you can set it in .env file
|
| 20 |
-
# In production on HF Spaces, it should be in secrets
|
| 21 |
-
print("Warning: API key not found in environment variables")
|
| 22 |
-
print("Please set PIXAZO_API_KEY in Hugging Face Space secrets")
|
| 23 |
-
|
| 24 |
-
# Try reading from a local file for development (never commit this!)
|
| 25 |
-
try:
|
| 26 |
-
with open("api_key.txt", "r") as f:
|
| 27 |
-
api_key = f.read().strip()
|
| 28 |
-
print("Using API key from local file (development only)")
|
| 29 |
-
except:
|
| 30 |
-
pass
|
| 31 |
-
|
| 32 |
-
return api_key
|
| 33 |
|
| 34 |
-
# Call Pixazo API
|
| 35 |
def call_pixazo_api(prompt, num_steps=4, seed=None, height=512, width=512,
|
| 36 |
style_preset=None, guidance_scale=None):
|
| 37 |
|
| 38 |
api_key = get_api_key()
|
| 39 |
|
| 40 |
if not api_key:
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
url = "https://gateway.pixazo.ai/flux-1-schnell/v1/getData"
|
| 44 |
|
|
@@ -64,65 +46,109 @@ def call_pixazo_api(prompt, num_steps=4, seed=None, height=512, width=512,
|
|
| 64 |
body["guidance_scale"] = guidance_scale
|
| 65 |
|
| 66 |
try:
|
| 67 |
-
print(f"Sending request to Pixazo API
|
|
|
|
|
|
|
| 68 |
|
| 69 |
response = requests.post(
|
| 70 |
url,
|
| 71 |
headers=headers,
|
| 72 |
json=body,
|
| 73 |
-
timeout=120
|
| 74 |
)
|
| 75 |
|
| 76 |
-
print(f"Response status
|
| 77 |
|
| 78 |
-
if response.status_code
|
| 79 |
-
|
| 80 |
-
print(
|
| 81 |
-
raise gr.Error(f"API returned error: {response.status_code}")
|
| 82 |
-
|
| 83 |
-
result = response.json()
|
| 84 |
-
print("API call successful")
|
| 85 |
-
|
| 86 |
-
# Handle different response formats
|
| 87 |
-
# Check for base64 image data
|
| 88 |
-
if "images" in result and isinstance(result["images"], list) and len(result["images"]) > 0:
|
| 89 |
-
image_data = result["images"][0]
|
| 90 |
|
| 91 |
-
#
|
| 92 |
-
if
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
try:
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
|
|
|
| 107 |
except requests.exceptions.Timeout:
|
| 108 |
-
|
|
|
|
|
|
|
|
|
|
| 109 |
except requests.exceptions.RequestException as e:
|
| 110 |
-
|
|
|
|
|
|
|
| 111 |
except Exception as e:
|
| 112 |
-
|
|
|
|
|
|
|
| 113 |
|
| 114 |
# Batch generate images
|
| 115 |
def generate_images(prompt, num_steps, seed, height, width, style_preset,
|
| 116 |
num_images, guidance_scale):
|
| 117 |
|
| 118 |
images = []
|
| 119 |
-
|
| 120 |
|
| 121 |
for i in range(num_images):
|
| 122 |
try:
|
| 123 |
# Use different seed for each image if seed is provided
|
| 124 |
current_seed = seed + i if seed > 0 else 0
|
| 125 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
image, result = call_pixazo_api(
|
| 127 |
prompt=prompt,
|
| 128 |
num_steps=num_steps,
|
|
@@ -135,58 +161,91 @@ def generate_images(prompt, num_steps, seed, height, width, style_preset,
|
|
| 135 |
|
| 136 |
if image:
|
| 137 |
images.append(image)
|
| 138 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
|
| 140 |
# Update progress
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
|
| 145 |
# Small delay between requests
|
| 146 |
if i < num_images - 1:
|
| 147 |
-
time.sleep(
|
| 148 |
|
| 149 |
except Exception as e:
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
|
|
|
|
|
|
| 153 |
break
|
| 154 |
|
| 155 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
yield [gr.Gallery(visible=False),
|
| 157 |
-
"No images
|
| 158 |
-
json.dumps(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
else:
|
| 160 |
yield [gr.Gallery(value=images, visible=True),
|
| 161 |
-
f"
|
| 162 |
-
json.dumps(
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
["A photorealistic portrait of an astronaut on Mars with detailed spacesuit and red planet landscape", 4, 789, 512, 512, "photographic", 1, 7.5],
|
| 170 |
-
]
|
| 171 |
|
| 172 |
# Create Gradio interface
|
| 173 |
def create_interface():
|
| 174 |
with gr.Blocks(theme=gr.themes.Soft(), title="Pixazo Image Generator") as demo:
|
| 175 |
gr.Markdown("""
|
| 176 |
# 🎨 Pixazo FLUX-1 Schnell Image Generator
|
| 177 |
-
|
|
|
|
|
|
|
| 178 |
""")
|
| 179 |
|
| 180 |
# API Status Check
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
|
|
|
| 185 |
gr.Markdown("""
|
| 186 |
-
|
| 187 |
-
1. Go to
|
| 188 |
-
2. Add
|
| 189 |
-
3.
|
| 190 |
""")
|
| 191 |
|
| 192 |
with gr.Row():
|
|
@@ -197,14 +256,14 @@ def create_interface():
|
|
| 197 |
label="Prompt",
|
| 198 |
placeholder="Describe the image you want to generate...",
|
| 199 |
lines=3,
|
| 200 |
-
|
| 201 |
)
|
| 202 |
|
| 203 |
with gr.Row():
|
| 204 |
style_preset = gr.Dropdown(
|
| 205 |
label="Style Preset",
|
| 206 |
choices=["none", "cyberpunk", "fantasy", "anime", "photographic",
|
| 207 |
-
"digital-art", "comic", "
|
| 208 |
value="none"
|
| 209 |
)
|
| 210 |
|
|
@@ -222,14 +281,14 @@ def create_interface():
|
|
| 222 |
minimum=1,
|
| 223 |
maximum=50,
|
| 224 |
value=4,
|
| 225 |
-
step=1
|
|
|
|
| 226 |
)
|
| 227 |
|
| 228 |
seed = gr.Number(
|
| 229 |
-
label="Seed
|
| 230 |
-
value=
|
| 231 |
-
|
| 232 |
-
maximum=999999
|
| 233 |
)
|
| 234 |
|
| 235 |
with gr.Row():
|
|
@@ -254,17 +313,22 @@ def create_interface():
|
|
| 254 |
minimum=1,
|
| 255 |
maximum=4,
|
| 256 |
value=1,
|
| 257 |
-
step=1
|
|
|
|
| 258 |
)
|
| 259 |
|
| 260 |
generate_btn = gr.Button("✨ Generate Images", variant="primary", size="lg")
|
| 261 |
|
| 262 |
-
#
|
| 263 |
gr.Examples(
|
| 264 |
-
examples=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 265 |
inputs=[prompt, num_steps, seed, height, width, style_preset, num_images, guidance_scale],
|
| 266 |
-
label="
|
| 267 |
-
examples_per_page=4
|
| 268 |
)
|
| 269 |
|
| 270 |
with gr.Column(scale=3):
|
|
@@ -280,31 +344,33 @@ def create_interface():
|
|
| 280 |
show_label=True,
|
| 281 |
columns=2,
|
| 282 |
height="auto",
|
| 283 |
-
visible=False
|
|
|
|
| 284 |
)
|
| 285 |
|
| 286 |
-
|
|
|
|
| 287 |
json_output = gr.JSON(
|
| 288 |
-
label="
|
| 289 |
-
|
| 290 |
)
|
| 291 |
|
| 292 |
-
#
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
|
| 309 |
# Generation function
|
| 310 |
generate_btn.click(
|
|
@@ -313,35 +379,54 @@ def create_interface():
|
|
| 313 |
outputs=[gallery, status, json_output]
|
| 314 |
)
|
| 315 |
|
| 316 |
-
# Quick
|
| 317 |
with gr.Row():
|
| 318 |
-
gr.Button("
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 322 |
)
|
| 323 |
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 329 |
|
| 330 |
return demo
|
| 331 |
|
| 332 |
-
#
|
| 333 |
def create_simple_interface():
|
| 334 |
-
with gr.Blocks(title="Pixazo
|
| 335 |
-
gr.Markdown("# 🖼️
|
| 336 |
|
| 337 |
with gr.Row():
|
| 338 |
with gr.Column():
|
| 339 |
-
prompt = gr.Textbox(label="Prompt",
|
| 340 |
btn = gr.Button("Generate", variant="primary")
|
| 341 |
-
|
| 342 |
with gr.Column():
|
| 343 |
output = gr.Image(label="Generated Image")
|
| 344 |
-
|
| 345 |
|
| 346 |
def simple_generate(prompt_text):
|
| 347 |
if not prompt_text.strip():
|
|
@@ -349,54 +434,39 @@ def create_simple_interface():
|
|
| 349 |
|
| 350 |
api_key = get_api_key()
|
| 351 |
if not api_key:
|
| 352 |
-
return None, "API key not
|
| 353 |
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
return None, f"API Response: {json.dumps(result, indent=2)[:200]}..."
|
| 366 |
-
|
| 367 |
-
except Exception as e:
|
| 368 |
-
return None, f"Error: {str(e)}"
|
| 369 |
|
| 370 |
-
btn.click(
|
| 371 |
-
fn=simple_generate,
|
| 372 |
-
inputs=[prompt],
|
| 373 |
-
outputs=[output, status]
|
| 374 |
-
)
|
| 375 |
|
| 376 |
return demo
|
| 377 |
|
| 378 |
# Main function
|
| 379 |
def main():
|
| 380 |
-
#
|
| 381 |
-
|
|
|
|
|
|
|
| 382 |
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
else:
|
| 386 |
-
demo = create_simple_interface()
|
| 387 |
|
| 388 |
-
# Launch with appropriate settings for Hugging Face Spaces
|
| 389 |
demo.launch(
|
| 390 |
server_name="0.0.0.0",
|
| 391 |
server_port=7860,
|
| 392 |
-
share=False
|
| 393 |
-
debug=False
|
| 394 |
)
|
| 395 |
|
| 396 |
if __name__ == "__main__":
|
| 397 |
-
# Check if running on Hugging Face Spaces
|
| 398 |
-
if os.environ.get("SPACE_ID"):
|
| 399 |
-
print("Running on Hugging Face Spaces")
|
| 400 |
-
print(f"Space ID: {os.environ.get('SPACE_ID')}")
|
| 401 |
-
|
| 402 |
main()
|
|
|
|
| 2 |
import requests
|
| 3 |
import json
|
| 4 |
import os
|
|
|
|
| 5 |
from PIL import Image
|
| 6 |
import io
|
| 7 |
+
import time
|
|
|
|
| 8 |
|
| 9 |
# Get API key from Hugging Face secrets
|
| 10 |
def get_api_key():
|
| 11 |
+
return os.environ.get("PIXAZO_API_KEY")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
# Call Pixazo API - Now we know the exact response format!
|
| 14 |
def call_pixazo_api(prompt, num_steps=4, seed=None, height=512, width=512,
|
| 15 |
style_preset=None, guidance_scale=None):
|
| 16 |
|
| 17 |
api_key = get_api_key()
|
| 18 |
|
| 19 |
if not api_key:
|
| 20 |
+
return None, {
|
| 21 |
+
"error": "API key not configured",
|
| 22 |
+
"instructions": "Please set PIXAZO_API_KEY in Hugging Face Space secrets"
|
| 23 |
+
}
|
| 24 |
|
| 25 |
url = "https://gateway.pixazo.ai/flux-1-schnell/v1/getData"
|
| 26 |
|
|
|
|
| 46 |
body["guidance_scale"] = guidance_scale
|
| 47 |
|
| 48 |
try:
|
| 49 |
+
print(f"🌐 Sending request to Pixazo API...")
|
| 50 |
+
print(f" Prompt: {prompt[:50]}...")
|
| 51 |
+
print(f" Steps: {num_steps}, Size: {width}x{height}")
|
| 52 |
|
| 53 |
response = requests.post(
|
| 54 |
url,
|
| 55 |
headers=headers,
|
| 56 |
json=body,
|
| 57 |
+
timeout=120
|
| 58 |
)
|
| 59 |
|
| 60 |
+
print(f"✅ Response status: {response.status_code}")
|
| 61 |
|
| 62 |
+
if response.status_code == 200:
|
| 63 |
+
result = response.json()
|
| 64 |
+
print(f"📦 Response format: {list(result.keys())}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
+
# ✅ Now we know the exact format: {"output": "<IMAGE_URL>"}
|
| 67 |
+
if "output" in result:
|
| 68 |
+
image_url = result["output"]
|
| 69 |
+
print(f"🖼️ Image URL received: {image_url[:80]}...")
|
| 70 |
+
|
| 71 |
+
# Download the image from the URL
|
| 72 |
+
try:
|
| 73 |
+
print(f"⬇️ Downloading image from URL...")
|
| 74 |
+
img_response = requests.get(image_url, timeout=30)
|
| 75 |
+
|
| 76 |
+
if img_response.status_code == 200:
|
| 77 |
+
image = Image.open(io.BytesIO(img_response.content))
|
| 78 |
+
print(f"✅ Image downloaded successfully: {image.size[0]}x{image.size[1]}")
|
| 79 |
+
|
| 80 |
+
return image, {
|
| 81 |
+
"success": True,
|
| 82 |
+
"status_code": 200,
|
| 83 |
+
"image_url": image_url,
|
| 84 |
+
"response_summary": {
|
| 85 |
+
"keys": list(result.keys()),
|
| 86 |
+
"url_length": len(image_url)
|
| 87 |
+
}
|
| 88 |
+
}
|
| 89 |
+
else:
|
| 90 |
+
return None, {
|
| 91 |
+
"error": f"Failed to download image: HTTP {img_response.status_code}",
|
| 92 |
+
"image_url": image_url,
|
| 93 |
+
"download_status": img_response.status_code
|
| 94 |
+
}
|
| 95 |
+
|
| 96 |
+
except Exception as e:
|
| 97 |
+
return None, {
|
| 98 |
+
"error": f"Error downloading image: {str(e)}",
|
| 99 |
+
"image_url": image_url,
|
| 100 |
+
"response": result
|
| 101 |
+
}
|
| 102 |
+
else:
|
| 103 |
+
return None, {
|
| 104 |
+
"error": "Response missing 'output' field",
|
| 105 |
+
"response": result,
|
| 106 |
+
"expected_format": '{"output": "IMAGE_URL"}'
|
| 107 |
+
}
|
| 108 |
+
else:
|
| 109 |
+
# Handle non-200 responses
|
| 110 |
try:
|
| 111 |
+
error_data = response.json()
|
| 112 |
+
except:
|
| 113 |
+
error_data = {"text": response.text[:500]}
|
| 114 |
+
|
| 115 |
+
return None, {
|
| 116 |
+
"error": f"API returned status {response.status_code}",
|
| 117 |
+
"status_code": response.status_code,
|
| 118 |
+
"response": error_data
|
| 119 |
+
}
|
| 120 |
+
|
| 121 |
except requests.exceptions.Timeout:
|
| 122 |
+
return None, {
|
| 123 |
+
"error": "Request timed out after 120 seconds",
|
| 124 |
+
"suggestion": "Try reducing image size or number of steps"
|
| 125 |
+
}
|
| 126 |
except requests.exceptions.RequestException as e:
|
| 127 |
+
return None, {
|
| 128 |
+
"error": f"Network error: {str(e)}"
|
| 129 |
+
}
|
| 130 |
except Exception as e:
|
| 131 |
+
return None, {
|
| 132 |
+
"error": f"Unexpected error: {str(e)}"
|
| 133 |
+
}
|
| 134 |
|
| 135 |
# Batch generate images
|
| 136 |
def generate_images(prompt, num_steps, seed, height, width, style_preset,
|
| 137 |
num_images, guidance_scale):
|
| 138 |
|
| 139 |
images = []
|
| 140 |
+
all_results = []
|
| 141 |
|
| 142 |
for i in range(num_images):
|
| 143 |
try:
|
| 144 |
# Use different seed for each image if seed is provided
|
| 145 |
current_seed = seed + i if seed > 0 else 0
|
| 146 |
|
| 147 |
+
print(f"\n{'='*50}")
|
| 148 |
+
print(f"Generating image {i+1}/{num_images}")
|
| 149 |
+
print(f"Seed: {current_seed}")
|
| 150 |
+
print(f"{'='*50}")
|
| 151 |
+
|
| 152 |
image, result = call_pixazo_api(
|
| 153 |
prompt=prompt,
|
| 154 |
num_steps=num_steps,
|
|
|
|
| 161 |
|
| 162 |
if image:
|
| 163 |
images.append(image)
|
| 164 |
+
print(f"✅ Image {i+1} generated successfully!")
|
| 165 |
+
else:
|
| 166 |
+
print(f"❌ Image {i+1} failed: {result.get('error', 'Unknown error')}")
|
| 167 |
+
|
| 168 |
+
all_results.append(result)
|
| 169 |
|
| 170 |
# Update progress
|
| 171 |
+
if image:
|
| 172 |
+
status_msg = f"✅ Generated {i+1}/{num_images} images"
|
| 173 |
+
else:
|
| 174 |
+
error_msg = result.get('error', 'Unknown error')
|
| 175 |
+
status_msg = f"❌ Image {i+1} failed: {error_msg[:50]}..."
|
| 176 |
+
|
| 177 |
+
yield [gr.Gallery(value=images, visible=bool(images)),
|
| 178 |
+
status_msg,
|
| 179 |
+
json.dumps(result, indent=2)]
|
| 180 |
|
| 181 |
# Small delay between requests
|
| 182 |
if i < num_images - 1:
|
| 183 |
+
time.sleep(2)
|
| 184 |
|
| 185 |
except Exception as e:
|
| 186 |
+
error_result = {"error": str(e), "image_index": i}
|
| 187 |
+
all_results.append(error_result)
|
| 188 |
+
yield [gr.Gallery(value=images, visible=bool(images)),
|
| 189 |
+
f"⚠️ Exception on image {i+1}: {str(e)[:50]}",
|
| 190 |
+
json.dumps(error_result, indent=2)]
|
| 191 |
break
|
| 192 |
|
| 193 |
+
# Final summary
|
| 194 |
+
success_count = len(images)
|
| 195 |
+
total_count = num_images
|
| 196 |
+
|
| 197 |
+
if success_count == 0:
|
| 198 |
yield [gr.Gallery(visible=False),
|
| 199 |
+
f"❌ No images generated ({success_count}/{total_count} successful)",
|
| 200 |
+
json.dumps({
|
| 201 |
+
"summary": "All generations failed",
|
| 202 |
+
"total_attempts": total_count,
|
| 203 |
+
"successful": success_count,
|
| 204 |
+
"failed": total_count,
|
| 205 |
+
"detailed_results": all_results
|
| 206 |
+
}, indent=2)]
|
| 207 |
+
elif success_count < total_count:
|
| 208 |
+
yield [gr.Gallery(value=images, visible=True),
|
| 209 |
+
f"⚠️ Partial success: {success_count}/{total_count} images",
|
| 210 |
+
json.dumps({
|
| 211 |
+
"summary": "Partial success",
|
| 212 |
+
"total_attempts": total_count,
|
| 213 |
+
"successful": success_count,
|
| 214 |
+
"failed": total_count - success_count,
|
| 215 |
+
"detailed_results": all_results
|
| 216 |
+
}, indent=2)]
|
| 217 |
else:
|
| 218 |
yield [gr.Gallery(value=images, visible=True),
|
| 219 |
+
f"🎉 Success! All {success_count} images generated",
|
| 220 |
+
json.dumps({
|
| 221 |
+
"summary": "Complete success",
|
| 222 |
+
"total_attempts": total_count,
|
| 223 |
+
"successful": success_count,
|
| 224 |
+
"failed": 0,
|
| 225 |
+
"sample_image_url": all_results[0].get("image_url", "N/A") if all_results else "N/A"
|
| 226 |
+
}, indent=2)]
|
|
|
|
|
|
|
| 227 |
|
| 228 |
# Create Gradio interface
|
| 229 |
def create_interface():
|
| 230 |
with gr.Blocks(theme=gr.themes.Soft(), title="Pixazo Image Generator") as demo:
|
| 231 |
gr.Markdown("""
|
| 232 |
# 🎨 Pixazo FLUX-1 Schnell Image Generator
|
| 233 |
+
*Now with correct response format handling!*
|
| 234 |
+
|
| 235 |
+
**Response Format Confirmed:** `{"output": "IMAGE_URL"}`
|
| 236 |
""")
|
| 237 |
|
| 238 |
# API Status Check
|
| 239 |
+
api_key = get_api_key()
|
| 240 |
+
if api_key:
|
| 241 |
+
gr.Markdown(f"### ✅ API Key: Configured ({len(api_key)} characters)")
|
| 242 |
+
else:
|
| 243 |
+
gr.Markdown("### ❌ API Key: Not configured")
|
| 244 |
gr.Markdown("""
|
| 245 |
+
**Setup Required:**
|
| 246 |
+
1. Go to Space Settings → Repository secrets
|
| 247 |
+
2. Add secret: **Key**: `PIXAZO_API_KEY`, **Value**: your API key
|
| 248 |
+
3. The app will automatically reload
|
| 249 |
""")
|
| 250 |
|
| 251 |
with gr.Row():
|
|
|
|
| 256 |
label="Prompt",
|
| 257 |
placeholder="Describe the image you want to generate...",
|
| 258 |
lines=3,
|
| 259 |
+
value="A sleek futuristic car racing through a neon-lit cyberpunk city at night"
|
| 260 |
)
|
| 261 |
|
| 262 |
with gr.Row():
|
| 263 |
style_preset = gr.Dropdown(
|
| 264 |
label="Style Preset",
|
| 265 |
choices=["none", "cyberpunk", "fantasy", "anime", "photographic",
|
| 266 |
+
"digital-art", "comic", "3d-model", "pixel-art", "isometric"],
|
| 267 |
value="none"
|
| 268 |
)
|
| 269 |
|
|
|
|
| 281 |
minimum=1,
|
| 282 |
maximum=50,
|
| 283 |
value=4,
|
| 284 |
+
step=1,
|
| 285 |
+
info="Higher = better quality but slower"
|
| 286 |
)
|
| 287 |
|
| 288 |
seed = gr.Number(
|
| 289 |
+
label="Seed",
|
| 290 |
+
value=42,
|
| 291 |
+
info="0 = random, >0 = reproducible"
|
|
|
|
| 292 |
)
|
| 293 |
|
| 294 |
with gr.Row():
|
|
|
|
| 313 |
minimum=1,
|
| 314 |
maximum=4,
|
| 315 |
value=1,
|
| 316 |
+
step=1,
|
| 317 |
+
info="Generate multiple variations"
|
| 318 |
)
|
| 319 |
|
| 320 |
generate_btn = gr.Button("✨ Generate Images", variant="primary", size="lg")
|
| 321 |
|
| 322 |
+
# Quick examples
|
| 323 |
gr.Examples(
|
| 324 |
+
examples=[
|
| 325 |
+
["A cyberpunk cityscape with flying cars and neon signs, night time, rain", 4, 123, 512, 512, "cyberpunk", 1, 7.5],
|
| 326 |
+
["A magical forest with glowing mushrooms and fairies, soft lighting", 4, 456, 512, 512, "fantasy", 1, 7.5],
|
| 327 |
+
["A cute anime cat wearing a spacesuit on the moon", 4, 789, 512, 512, "anime", 1, 7.5],
|
| 328 |
+
["Photorealistic portrait of an elderly wise man with detailed wrinkles", 6, 999, 768, 512, "photographic", 1, 8.0],
|
| 329 |
+
],
|
| 330 |
inputs=[prompt, num_steps, seed, height, width, style_preset, num_images, guidance_scale],
|
| 331 |
+
label="Quick Examples"
|
|
|
|
| 332 |
)
|
| 333 |
|
| 334 |
with gr.Column(scale=3):
|
|
|
|
| 344 |
show_label=True,
|
| 345 |
columns=2,
|
| 346 |
height="auto",
|
| 347 |
+
visible=False,
|
| 348 |
+
object_fit="contain"
|
| 349 |
)
|
| 350 |
|
| 351 |
+
# Response viewer
|
| 352 |
+
with gr.Accordion("📊 API Response Details", open=False):
|
| 353 |
json_output = gr.JSON(
|
| 354 |
+
label="API Response",
|
| 355 |
+
container=True
|
| 356 |
)
|
| 357 |
|
| 358 |
+
# Debug info
|
| 359 |
+
with gr.Accordion("🔍 Debug Information", open=False):
|
| 360 |
+
gr.Markdown("""
|
| 361 |
+
**Known Response Format:** `{"output": "IMAGE_URL"}`
|
| 362 |
+
|
| 363 |
+
**Flow:**
|
| 364 |
+
1. Send request to Pixazo API with prompt and parameters
|
| 365 |
+
2. Receive JSON with image URL in `output` field
|
| 366 |
+
3. Download image from that URL
|
| 367 |
+
4. Display image in gallery
|
| 368 |
+
|
| 369 |
+
**Troubleshooting:**
|
| 370 |
+
- Check the JSON response for errors
|
| 371 |
+
- If image fails to download, check the URL in the response
|
| 372 |
+
- Make sure your API key is valid
|
| 373 |
+
""")
|
| 374 |
|
| 375 |
# Generation function
|
| 376 |
generate_btn.click(
|
|
|
|
| 379 |
outputs=[gallery, status, json_output]
|
| 380 |
)
|
| 381 |
|
| 382 |
+
# Quick test button
|
| 383 |
with gr.Row():
|
| 384 |
+
test_btn = gr.Button("🧪 Test Single Image", variant="secondary")
|
| 385 |
+
clear_btn = gr.Button("🗑️ Clear Results", variant="secondary")
|
| 386 |
+
|
| 387 |
+
def test_single_image():
|
| 388 |
+
test_prompt = "A simple test image of a red apple on a table"
|
| 389 |
+
image, result = call_pixazo_api(
|
| 390 |
+
prompt=test_prompt,
|
| 391 |
+
num_steps=2,
|
| 392 |
+
height=256,
|
| 393 |
+
width=256
|
| 394 |
)
|
| 395 |
|
| 396 |
+
if image:
|
| 397 |
+
return [gr.Gallery(value=[image], visible=True),
|
| 398 |
+
"✅ Test successful!",
|
| 399 |
+
json.dumps(result, indent=2)]
|
| 400 |
+
else:
|
| 401 |
+
return [gr.Gallery(visible=False),
|
| 402 |
+
f"❌ Test failed: {result.get('error', 'Unknown')}",
|
| 403 |
+
json.dumps(result, indent=2)]
|
| 404 |
+
|
| 405 |
+
test_btn.click(
|
| 406 |
+
fn=test_single_image,
|
| 407 |
+
outputs=[gallery, status, json_output]
|
| 408 |
+
)
|
| 409 |
+
|
| 410 |
+
clear_btn.click(
|
| 411 |
+
fn=lambda: [None, "Ready to generate...", None],
|
| 412 |
+
outputs=[gallery, status, json_output]
|
| 413 |
+
)
|
| 414 |
|
| 415 |
return demo
|
| 416 |
|
| 417 |
+
# Simple interface for quick testing
|
| 418 |
def create_simple_interface():
|
| 419 |
+
with gr.Blocks(title="Pixazo Simple") as demo:
|
| 420 |
+
gr.Markdown("# 🖼️ Pixazo Image Generator")
|
| 421 |
|
| 422 |
with gr.Row():
|
| 423 |
with gr.Column():
|
| 424 |
+
prompt = gr.Textbox(label="Prompt", value="A beautiful sunset")
|
| 425 |
btn = gr.Button("Generate", variant="primary")
|
| 426 |
+
|
| 427 |
with gr.Column():
|
| 428 |
output = gr.Image(label="Generated Image")
|
| 429 |
+
info = gr.Textbox(label="Info")
|
| 430 |
|
| 431 |
def simple_generate(prompt_text):
|
| 432 |
if not prompt_text.strip():
|
|
|
|
| 434 |
|
| 435 |
api_key = get_api_key()
|
| 436 |
if not api_key:
|
| 437 |
+
return None, "API key not set"
|
| 438 |
|
| 439 |
+
image, result = call_pixazo_api(
|
| 440 |
+
prompt=prompt_text,
|
| 441 |
+
num_steps=4,
|
| 442 |
+
height=512,
|
| 443 |
+
width=512
|
| 444 |
+
)
|
| 445 |
+
|
| 446 |
+
if image:
|
| 447 |
+
return image, f"✅ Generated {image.size[0]}x{image.size[1]} image"
|
| 448 |
+
else:
|
| 449 |
+
return None, f"❌ Error: {result.get('error', 'Unknown')}"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 450 |
|
| 451 |
+
btn.click(simple_generate, inputs=[prompt], outputs=[output, info])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 452 |
|
| 453 |
return demo
|
| 454 |
|
| 455 |
# Main function
|
| 456 |
def main():
|
| 457 |
+
# Print startup info
|
| 458 |
+
print("🚀 Starting Pixazo Image Generator")
|
| 459 |
+
print(f"📌 API Key present: {'Yes' if get_api_key() else 'No'}")
|
| 460 |
+
print(f"📌 Expected response format: {{'output': 'IMAGE_URL'}}")
|
| 461 |
|
| 462 |
+
# Create and launch interface
|
| 463 |
+
demo = create_interface()
|
|
|
|
|
|
|
| 464 |
|
|
|
|
| 465 |
demo.launch(
|
| 466 |
server_name="0.0.0.0",
|
| 467 |
server_port=7860,
|
| 468 |
+
share=False
|
|
|
|
| 469 |
)
|
| 470 |
|
| 471 |
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 472 |
main()
|