Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -25,12 +25,14 @@ from PIL import Image
|
|
| 25 |
# Clients
|
| 26 |
# ---------------------------------------------------------------------------
|
| 27 |
|
| 28 |
-
GROQ_API_KEY
|
| 29 |
-
GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY", "")
|
| 30 |
|
| 31 |
CODE_MODEL = "llama-3.1-8b-instant" # Groq β game code
|
| 32 |
PROMPT_MODEL = "llama-3.3-70b-versatile" # Groq β creative prompts
|
| 33 |
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
def get_groq_client():
|
| 36 |
if not GROQ_API_KEY:
|
|
@@ -44,16 +46,6 @@ def get_groq_client():
|
|
| 44 |
)
|
| 45 |
|
| 46 |
|
| 47 |
-
def get_gemini_image_client():
|
| 48 |
-
"""Gemini image generation β 500 free images/day, no credit card needed."""
|
| 49 |
-
if not GOOGLE_API_KEY:
|
| 50 |
-
return None
|
| 51 |
-
return OpenAI(
|
| 52 |
-
base_url="https://generativelanguage.googleapis.com/v1beta/openai/",
|
| 53 |
-
api_key=GOOGLE_API_KEY,
|
| 54 |
-
)
|
| 55 |
-
|
| 56 |
-
|
| 57 |
# ---------------------------------------------------------------------------
|
| 58 |
# Z-Image-Engineer system prompt (from BennyDaBall/Qwen3-4b-Z-Image-Engineer-V4)
|
| 59 |
# ---------------------------------------------------------------------------
|
|
@@ -253,36 +245,33 @@ def _colored_placeholder(name: str) -> str:
|
|
| 253 |
|
| 254 |
|
| 255 |
def generate_sprites(image_prompts: dict) -> tuple:
|
| 256 |
-
"""Generate images via
|
| 257 |
Returns (sprite_map, errors_list)"""
|
| 258 |
-
|
|
|
|
|
|
|
| 259 |
sprite_map = {}
|
| 260 |
errors = []
|
| 261 |
|
| 262 |
-
|
| 263 |
-
errors.append("GOOGLE_API_KEY not set - using placeholder sprites. Add GOOGLE_API_KEY as a Space secret.")
|
| 264 |
-
for sprite_name in image_prompts:
|
| 265 |
-
sprite_map[sprite_name] = _colored_placeholder(sprite_name.replace(".png", ""))
|
| 266 |
-
return sprite_map, errors
|
| 267 |
-
|
| 268 |
-
for sprite_name, prompt in image_prompts.items():
|
| 269 |
try:
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
|
|
|
| 275 |
)
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
size = None if is_bg else (64, 64)
|
| 280 |
sprite_map[sprite_name] = _pil_to_data_uri(pil_img, size=size)
|
| 281 |
-
print(f"[
|
| 282 |
except Exception as exc:
|
| 283 |
error_msg = str(exc)
|
| 284 |
errors.append(f"{sprite_name}: {error_msg}")
|
| 285 |
-
print(f"[
|
| 286 |
sprite_map[sprite_name] = _colored_placeholder(sprite_name.replace(".png", ""))
|
| 287 |
|
| 288 |
return sprite_map, errors
|
|
@@ -364,12 +353,12 @@ def generate_game_code(game_type: str, theme: str, temperature: float, max_new_t
|
|
| 364 |
if sprite_errors:
|
| 365 |
error_detail = " | ".join(sprite_errors)
|
| 366 |
status = (
|
| 367 |
-
f"Code generated. Images: {n_real} by
|
| 368 |
f"FLUX errors: {error_detail}"
|
| 369 |
)
|
| 370 |
else:
|
| 371 |
status = (
|
| 372 |
-
f"Done! {n_real} sprite(s) generated by
|
| 373 |
"Click Launch Game to play."
|
| 374 |
)
|
| 375 |
|
|
@@ -547,7 +536,7 @@ def build_ui():
|
|
| 547 |
gr.Markdown(
|
| 548 |
"---\n"
|
| 549 |
"**Pipeline:** Theme β [Groq Llama] game code + [Groq 70B as Z-Image-Engineer] "
|
| 550 |
-
"cinematic prompts β [
|
| 551 |
"Edit the HTML and click **Launch Game** to hot-reload."
|
| 552 |
)
|
| 553 |
|
|
|
|
| 25 |
# Clients
|
| 26 |
# ---------------------------------------------------------------------------
|
| 27 |
|
| 28 |
+
GROQ_API_KEY = os.environ.get("GROQ_API_KEY", "")
|
|
|
|
| 29 |
|
| 30 |
CODE_MODEL = "llama-3.1-8b-instant" # Groq β game code
|
| 31 |
PROMPT_MODEL = "llama-3.3-70b-versatile" # Groq β creative prompts
|
| 32 |
|
| 33 |
+
# Pollinations.AI β completely free, no API key, no signup needed
|
| 34 |
+
POLLINATIONS_URL = "https://image.pollinations.ai/prompt/{prompt}?width={w}&height={h}&model=flux&nologo=true&seed={seed}"
|
| 35 |
+
|
| 36 |
|
| 37 |
def get_groq_client():
|
| 38 |
if not GROQ_API_KEY:
|
|
|
|
| 46 |
)
|
| 47 |
|
| 48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
# ---------------------------------------------------------------------------
|
| 50 |
# Z-Image-Engineer system prompt (from BennyDaBall/Qwen3-4b-Z-Image-Engineer-V4)
|
| 51 |
# ---------------------------------------------------------------------------
|
|
|
|
| 245 |
|
| 246 |
|
| 247 |
def generate_sprites(image_prompts: dict) -> tuple:
|
| 248 |
+
"""Generate images via Pollinations.AI (free, no API key, no signup).
|
| 249 |
Returns (sprite_map, errors_list)"""
|
| 250 |
+
import requests
|
| 251 |
+
from urllib.parse import quote
|
| 252 |
+
|
| 253 |
sprite_map = {}
|
| 254 |
errors = []
|
| 255 |
|
| 256 |
+
for i, (sprite_name, prompt) in enumerate(image_prompts.items()):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 257 |
try:
|
| 258 |
+
is_bg = "background" in sprite_name
|
| 259 |
+
w, h = (800, 450) if is_bg else (64, 64)
|
| 260 |
+
seed = abs(hash(sprite_name)) % 99999
|
| 261 |
+
url = POLLINATIONS_URL.format(
|
| 262 |
+
prompt=quote(prompt),
|
| 263 |
+
w=w, h=h, seed=seed
|
| 264 |
)
|
| 265 |
+
response = requests.get(url, timeout=60)
|
| 266 |
+
response.raise_for_status()
|
| 267 |
+
pil_img = Image.open(io.BytesIO(response.content))
|
| 268 |
size = None if is_bg else (64, 64)
|
| 269 |
sprite_map[sprite_name] = _pil_to_data_uri(pil_img, size=size)
|
| 270 |
+
print(f"[Pollinations] OK: {sprite_name}")
|
| 271 |
except Exception as exc:
|
| 272 |
error_msg = str(exc)
|
| 273 |
errors.append(f"{sprite_name}: {error_msg}")
|
| 274 |
+
print(f"[Pollinations] FAILED {sprite_name}: {error_msg}")
|
| 275 |
sprite_map[sprite_name] = _colored_placeholder(sprite_name.replace(".png", ""))
|
| 276 |
|
| 277 |
return sprite_map, errors
|
|
|
|
| 353 |
if sprite_errors:
|
| 354 |
error_detail = " | ".join(sprite_errors)
|
| 355 |
status = (
|
| 356 |
+
f"Code generated. Images: {n_real} by Pollinations/FLUX, {n_fallback} fallback. "
|
| 357 |
f"FLUX errors: {error_detail}"
|
| 358 |
)
|
| 359 |
else:
|
| 360 |
status = (
|
| 361 |
+
f"Done! {n_real} sprite(s) generated by Pollinations/FLUX. "
|
| 362 |
"Click Launch Game to play."
|
| 363 |
)
|
| 364 |
|
|
|
|
| 536 |
gr.Markdown(
|
| 537 |
"---\n"
|
| 538 |
"**Pipeline:** Theme β [Groq Llama] game code + [Groq 70B as Z-Image-Engineer] "
|
| 539 |
+
"cinematic prompts β [Pollinations.AI/FLUX] sprites β embedded in game. "
|
| 540 |
"Edit the HTML and click **Launch Game** to hot-reload."
|
| 541 |
)
|
| 542 |
|