Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -250,28 +250,33 @@ def _colored_placeholder(name: str) -> str:
|
|
| 250 |
return "data:image/svg+xml;base64," + base64.b64encode(svg.encode()).decode()
|
| 251 |
|
| 252 |
|
| 253 |
-
def generate_sprites(image_prompts: dict) ->
|
| 254 |
-
"""Generate actual images from enhanced prompts via FLUX.1-schnell.
|
|
|
|
| 255 |
image_client = get_image_client()
|
| 256 |
sprite_map = {}
|
|
|
|
| 257 |
|
| 258 |
-
|
| 259 |
-
|
|
|
|
| 260 |
sprite_map[sprite_name] = _colored_placeholder(sprite_name.replace(".png", ""))
|
| 261 |
-
|
|
|
|
|
|
|
| 262 |
try:
|
| 263 |
-
# Background gets full canvas size, sprites get 64x64
|
| 264 |
is_bg = "background" in sprite_name
|
| 265 |
pil_img = image_client.text_to_image(prompt, model=IMAGE_MODEL)
|
| 266 |
size = None if is_bg else (64, 64)
|
| 267 |
sprite_map[sprite_name] = _pil_to_data_uri(pil_img, size=size)
|
| 268 |
-
print(f"[FLUX]
|
| 269 |
except Exception as exc:
|
| 270 |
error_msg = str(exc)
|
|
|
|
| 271 |
print(f"[FLUX] FAILED {sprite_name}: {error_msg}")
|
| 272 |
-
sprite_map[sprite_name] =
|
| 273 |
|
| 274 |
-
return sprite_map
|
| 275 |
|
| 276 |
# ---------------------------------------------------------------------------
|
| 277 |
# Step 3: Inject sprites into HTML
|
|
@@ -339,17 +344,25 @@ def generate_game_code(game_type: str, theme: str, temperature: float, max_new_t
|
|
| 339 |
image_prompts = generate_image_prompts(theme.strip(), game_type)
|
| 340 |
|
| 341 |
# -- Sprite generation (FLUX.1-schnell) --
|
| 342 |
-
sprite_map = generate_sprites(image_prompts)
|
| 343 |
|
| 344 |
# -- Inject sprites --
|
| 345 |
final_code = _inject_sprites(code, sprite_map)
|
| 346 |
|
| 347 |
n_real = sum(1 for v in sprite_map.values() if "image/png" in v)
|
| 348 |
n_fallback = len(sprite_map) - n_real
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 353 |
|
| 354 |
# Show the enhanced prompts that were used
|
| 355 |
prompt_summary = "\n\n".join(
|
|
|
|
| 250 |
return "data:image/svg+xml;base64," + base64.b64encode(svg.encode()).decode()
|
| 251 |
|
| 252 |
|
| 253 |
+
def generate_sprites(image_prompts: dict) -> tuple:
|
| 254 |
+
"""Generate actual images from enhanced prompts via FLUX.1-schnell.
|
| 255 |
+
Returns (sprite_map, errors_list)"""
|
| 256 |
image_client = get_image_client()
|
| 257 |
sprite_map = {}
|
| 258 |
+
errors = []
|
| 259 |
|
| 260 |
+
if not image_client:
|
| 261 |
+
errors.append("HF_TOKEN not set - using placeholder sprites. Add HF_TOKEN as a Space secret.")
|
| 262 |
+
for sprite_name in image_prompts:
|
| 263 |
sprite_map[sprite_name] = _colored_placeholder(sprite_name.replace(".png", ""))
|
| 264 |
+
return sprite_map, errors
|
| 265 |
+
|
| 266 |
+
for sprite_name, prompt in image_prompts.items():
|
| 267 |
try:
|
|
|
|
| 268 |
is_bg = "background" in sprite_name
|
| 269 |
pil_img = image_client.text_to_image(prompt, model=IMAGE_MODEL)
|
| 270 |
size = None if is_bg else (64, 64)
|
| 271 |
sprite_map[sprite_name] = _pil_to_data_uri(pil_img, size=size)
|
| 272 |
+
print(f"[FLUX] OK: {sprite_name}")
|
| 273 |
except Exception as exc:
|
| 274 |
error_msg = str(exc)
|
| 275 |
+
errors.append(f"{sprite_name}: {error_msg}")
|
| 276 |
print(f"[FLUX] FAILED {sprite_name}: {error_msg}")
|
| 277 |
+
sprite_map[sprite_name] = _colored_placeholder(sprite_name.replace(".png", ""))
|
| 278 |
|
| 279 |
+
return sprite_map, errors
|
| 280 |
|
| 281 |
# ---------------------------------------------------------------------------
|
| 282 |
# Step 3: Inject sprites into HTML
|
|
|
|
| 344 |
image_prompts = generate_image_prompts(theme.strip(), game_type)
|
| 345 |
|
| 346 |
# -- Sprite generation (FLUX.1-schnell) --
|
| 347 |
+
sprite_map, sprite_errors = generate_sprites(image_prompts)
|
| 348 |
|
| 349 |
# -- Inject sprites --
|
| 350 |
final_code = _inject_sprites(code, sprite_map)
|
| 351 |
|
| 352 |
n_real = sum(1 for v in sprite_map.values() if "image/png" in v)
|
| 353 |
n_fallback = len(sprite_map) - n_real
|
| 354 |
+
|
| 355 |
+
if sprite_errors:
|
| 356 |
+
error_detail = " | ".join(sprite_errors)
|
| 357 |
+
status = (
|
| 358 |
+
f"Code generated. Images: {n_real} FLUX, {n_fallback} fallback. "
|
| 359 |
+
f"FLUX errors: {error_detail}"
|
| 360 |
+
)
|
| 361 |
+
else:
|
| 362 |
+
status = (
|
| 363 |
+
f"Done! {n_real} sprite(s) generated by FLUX. "
|
| 364 |
+
"Click Launch Game to play."
|
| 365 |
+
)
|
| 366 |
|
| 367 |
# Show the enhanced prompts that were used
|
| 368 |
prompt_summary = "\n\n".join(
|