Generate fresh images on every run
Browse files
app.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import base64
|
| 2 |
import io
|
|
|
|
| 3 |
import re
|
| 4 |
import tempfile
|
| 5 |
import time
|
|
@@ -14,7 +15,7 @@ from PIL import Image, ImageDraw, ImageFont
|
|
| 14 |
|
| 15 |
POLLINATIONS_URL = (
|
| 16 |
"https://image.pollinations.ai/prompt/{prompt}"
|
| 17 |
-
"?width={width}&height={height}&model=flux&nologo=true&seed={seed}"
|
| 18 |
)
|
| 19 |
|
| 20 |
|
|
@@ -157,13 +158,14 @@ def placeholder_png_bytes(role: str, width: int, height: int) -> bytes:
|
|
| 157 |
return out.getvalue()
|
| 158 |
|
| 159 |
|
| 160 |
-
def generate_asset(spec: AssetSpec, index: int) -> tuple[str, str, str | None]:
|
| 161 |
-
seed =
|
| 162 |
url = POLLINATIONS_URL.format(
|
| 163 |
prompt=quote(spec.prompt),
|
| 164 |
width=spec.width,
|
| 165 |
height=spec.height,
|
| 166 |
seed=seed,
|
|
|
|
| 167 |
)
|
| 168 |
|
| 169 |
for attempt in range(3):
|
|
@@ -269,16 +271,17 @@ def generate_images_and_game(html_code: str, roles: str, style_hint: str):
|
|
| 269 |
assets: dict[str, str] = {}
|
| 270 |
gallery = []
|
| 271 |
errors = []
|
|
|
|
| 272 |
|
| 273 |
for index, spec in enumerate(specs):
|
| 274 |
-
data_uri, gallery_path, error = generate_asset(spec, index)
|
| 275 |
assets[spec.role] = data_uri
|
| 276 |
gallery.append((gallery_path, f"{spec.role} -> {spec.filename}"))
|
| 277 |
if error:
|
| 278 |
errors.append(f"{spec.role}: fallback used ({error})")
|
| 279 |
|
| 280 |
rewritten = embed_assets(html_code, assets, specs)
|
| 281 |
-
status = f"Generated and embedded {len(specs)} asset(s)."
|
| 282 |
if errors:
|
| 283 |
status += "\n\n" + "\n".join(errors)
|
| 284 |
return rewritten, status, gallery, build_preview(rewritten)
|
|
|
|
| 1 |
import base64
|
| 2 |
import io
|
| 3 |
+
import random
|
| 4 |
import re
|
| 5 |
import tempfile
|
| 6 |
import time
|
|
|
|
| 15 |
|
| 16 |
POLLINATIONS_URL = (
|
| 17 |
"https://image.pollinations.ai/prompt/{prompt}"
|
| 18 |
+
"?width={width}&height={height}&model=flux&nologo=true&seed={seed}&run={run_id}"
|
| 19 |
)
|
| 20 |
|
| 21 |
|
|
|
|
| 158 |
return out.getvalue()
|
| 159 |
|
| 160 |
|
| 161 |
+
def generate_asset(spec: AssetSpec, index: int, run_id: int) -> tuple[str, str, str | None]:
|
| 162 |
+
seed = random.Random(f"{spec.role}|{spec.prompt}|{index}|{run_id}").randint(1, 999999)
|
| 163 |
url = POLLINATIONS_URL.format(
|
| 164 |
prompt=quote(spec.prompt),
|
| 165 |
width=spec.width,
|
| 166 |
height=spec.height,
|
| 167 |
seed=seed,
|
| 168 |
+
run_id=run_id,
|
| 169 |
)
|
| 170 |
|
| 171 |
for attempt in range(3):
|
|
|
|
| 271 |
assets: dict[str, str] = {}
|
| 272 |
gallery = []
|
| 273 |
errors = []
|
| 274 |
+
run_id = time.time_ns()
|
| 275 |
|
| 276 |
for index, spec in enumerate(specs):
|
| 277 |
+
data_uri, gallery_path, error = generate_asset(spec, index, run_id)
|
| 278 |
assets[spec.role] = data_uri
|
| 279 |
gallery.append((gallery_path, f"{spec.role} -> {spec.filename}"))
|
| 280 |
if error:
|
| 281 |
errors.append(f"{spec.role}: fallback used ({error})")
|
| 282 |
|
| 283 |
rewritten = embed_assets(html_code, assets, specs)
|
| 284 |
+
status = f"Generated and embedded {len(specs)} asset(s). Run {str(run_id)[-6:]}."
|
| 285 |
if errors:
|
| 286 |
status += "\n\n" + "\n".join(errors)
|
| 287 |
return rewritten, status, gallery, build_preview(rewritten)
|