Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -269,15 +269,19 @@ def _threaded_broadcast(system_prompt: str, should_continue, **kwargs):
|
|
| 269 |
yield item
|
| 270 |
|
| 271 |
|
| 272 |
-
def _stream_model_cached(
|
|
|
|
|
|
|
| 273 |
def superseded():
|
| 274 |
return should_continue is not None and not should_continue()
|
| 275 |
|
| 276 |
# Pre-baked broadcasts are served instantly, no model call, no cold wait.
|
| 277 |
-
|
| 278 |
-
if
|
| 279 |
-
|
| 280 |
-
|
|
|
|
|
|
|
| 281 |
|
| 282 |
cached = _broadcast_cache.get(cache_key)
|
| 283 |
if cached:
|
|
@@ -321,19 +325,30 @@ def _norm_variant(variant) -> int:
|
|
| 321 |
# queue (that froze "the other stations" when retuning fast). Real generation
|
| 322 |
# stays serialized by model._gen_lock.
|
| 323 |
@app.api(name="tune", concurrency_limit=2, stream_every=0.02)
|
| 324 |
-
def tune(frequency: float, language: str = "es", variant: float = 0) -> str:
|
| 325 |
"""Tunes a frequency and streams the broadcast (SSE).
|
| 326 |
|
| 327 |
`variant` is a per-listener session nonce. For the encrypted station it
|
| 328 |
-
drives that listener's unique cipher (kept fully per-session).
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
|
|
|
|
|
|
|
|
|
| 332 |
"""
|
| 333 |
frequency = float(frequency)
|
| 334 |
lang = normalize_lang(language)
|
| 335 |
var = _norm_variant(variant)
|
| 336 |
pool = var % POOL_SIZE
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 337 |
my_epoch = _next_tune_epoch()
|
| 338 |
|
| 339 |
def still_current():
|
|
@@ -347,12 +362,13 @@ def tune(frequency: float, language: str = "es", variant: float = 0) -> str:
|
|
| 347 |
# The unsolvable station: the model broadcasts, the frontend never cleans it.
|
| 348 |
if abs(frequency - CURSED_FREQUENCY) <= STATION_TOLERANCE:
|
| 349 |
yield from _stream_model_cached(
|
| 350 |
-
f"cursed:{CURSED_FREQUENCY}:{lang}:{
|
| 351 |
localized_system_prompt(CURSED_SYSTEM_PROMPT, "es", lang),
|
| 352 |
should_continue=still_current,
|
| 353 |
user_prompt=localized_user_prompt(None, lang),
|
| 354 |
-
seed=_mix_seed(station_seed(CURSED_FREQUENCY),
|
| 355 |
max_tokens=160,
|
|
|
|
| 356 |
)
|
| 357 |
return
|
| 358 |
|
|
@@ -370,11 +386,12 @@ def tune(frequency: float, language: str = "es", variant: float = 0) -> str:
|
|
| 370 |
return
|
| 371 |
|
| 372 |
yield from _stream_model_cached(
|
| 373 |
-
f"station:{canonical}:{lang}:{
|
| 374 |
station_system_prompt(canonical, lang),
|
| 375 |
should_continue=still_current,
|
| 376 |
user_prompt=localized_user_prompt(station, lang),
|
| 377 |
-
seed=_mix_seed(station_seed(canonical),
|
|
|
|
| 378 |
)
|
| 379 |
|
| 380 |
|
|
|
|
| 269 |
yield item
|
| 270 |
|
| 271 |
|
| 272 |
+
def _stream_model_cached(
|
| 273 |
+
cache_key: str, system_prompt: str, should_continue=None, use_prebaked=True, **kwargs
|
| 274 |
+
):
|
| 275 |
def superseded():
|
| 276 |
return should_continue is not None and not should_continue()
|
| 277 |
|
| 278 |
# Pre-baked broadcasts are served instantly, no model call, no cold wait.
|
| 279 |
+
# Live mode passes use_prebaked=False to force a fresh generation instead.
|
| 280 |
+
if use_prebaked:
|
| 281 |
+
prebaked = _PREBAKED.get(cache_key)
|
| 282 |
+
if prebaked:
|
| 283 |
+
yield from _stream_fixed(prebaked, char_delay=0.018, should_continue=should_continue)
|
| 284 |
+
return
|
| 285 |
|
| 286 |
cached = _broadcast_cache.get(cache_key)
|
| 287 |
if cached:
|
|
|
|
| 325 |
# queue (that froze "the other stations" when retuning fast). Real generation
|
| 326 |
# stays serialized by model._gen_lock.
|
| 327 |
@app.api(name="tune", concurrency_limit=2, stream_every=0.02)
|
| 328 |
+
def tune(frequency: float, language: str = "es", variant: float = 0, live: float = 0) -> str:
|
| 329 |
"""Tunes a frequency and streams the broadcast (SSE).
|
| 330 |
|
| 331 |
`variant` is a per-listener session nonce. For the encrypted station it
|
| 332 |
+
drives that listener's unique cipher (kept fully per-session).
|
| 333 |
+
|
| 334 |
+
`live` is the UI mode toggle. When off (default), the spoken stations and
|
| 335 |
+
the cursed signal are served from the pre-baked pool (`pool`): instant, and
|
| 336 |
+
different visitors still get different editions. When on, the broadcast is
|
| 337 |
+
generated fresh by the model right now, using the real per-listener variant,
|
| 338 |
+
so a visitor can watch the AI write the transmission token by token.
|
| 339 |
"""
|
| 340 |
frequency = float(frequency)
|
| 341 |
lang = normalize_lang(language)
|
| 342 |
var = _norm_variant(variant)
|
| 343 |
pool = var % POOL_SIZE
|
| 344 |
+
try:
|
| 345 |
+
live_mode = bool(float(live))
|
| 346 |
+
except (TypeError, ValueError):
|
| 347 |
+
live_mode = False
|
| 348 |
+
# Live mode: generate fresh (real variant, skip the pre-baked pool).
|
| 349 |
+
# Fast mode: pooled key that hits the pre-baked cache instantly.
|
| 350 |
+
key_var = var if live_mode else pool
|
| 351 |
+
use_prebaked = not live_mode
|
| 352 |
my_epoch = _next_tune_epoch()
|
| 353 |
|
| 354 |
def still_current():
|
|
|
|
| 362 |
# The unsolvable station: the model broadcasts, the frontend never cleans it.
|
| 363 |
if abs(frequency - CURSED_FREQUENCY) <= STATION_TOLERANCE:
|
| 364 |
yield from _stream_model_cached(
|
| 365 |
+
f"cursed:{CURSED_FREQUENCY}:{lang}:{key_var}",
|
| 366 |
localized_system_prompt(CURSED_SYSTEM_PROMPT, "es", lang),
|
| 367 |
should_continue=still_current,
|
| 368 |
user_prompt=localized_user_prompt(None, lang),
|
| 369 |
+
seed=_mix_seed(station_seed(CURSED_FREQUENCY), key_var),
|
| 370 |
max_tokens=160,
|
| 371 |
+
use_prebaked=use_prebaked,
|
| 372 |
)
|
| 373 |
return
|
| 374 |
|
|
|
|
| 386 |
return
|
| 387 |
|
| 388 |
yield from _stream_model_cached(
|
| 389 |
+
f"station:{canonical}:{lang}:{key_var}",
|
| 390 |
station_system_prompt(canonical, lang),
|
| 391 |
should_continue=still_current,
|
| 392 |
user_prompt=localized_user_prompt(station, lang),
|
| 393 |
+
seed=_mix_seed(station_seed(canonical), key_var),
|
| 394 |
+
use_prebaked=use_prebaked,
|
| 395 |
)
|
| 396 |
|
| 397 |
|