polats commited on
Commit
bfdbea8
·
1 Parent(s): f9dd2fe

Stream Tiny Aya deltas through text route

Browse files
Files changed (1) hide show
  1. app.py +28 -3
app.py CHANGED
@@ -467,6 +467,30 @@ def _tiny_aya_generate(system, user, max_tokens, temperature):
467
  return str(result or "")
468
 
469
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
470
  @fastapi_app.post("/voxcpm-tts")
471
  async def voxcpm_tts(request: Request):
472
  body = await request.json()
@@ -762,9 +786,10 @@ async def text_generate_stream(request: Request):
762
  if model == "tiny-aya-global-zerogpu":
763
  if not TINY_AYA_SPACE:
764
  raise llm.LlmUnavailable("TINY_AYA_SPACE not set")
765
- text = _tiny_aya_generate(system, user, max_tokens, temperature)
766
- if text:
767
- loop.call_soon_threadsafe(q.put_nowait, ("delta", text))
 
768
  else:
769
  for chunk in llm.stream_chat(
770
  system,
 
467
  return str(result or "")
468
 
469
 
470
+ def _tiny_aya_stream(system, user, max_tokens, temperature):
471
+ from gradio_client import Client
472
+ client = Client(TINY_AYA_SPACE, token=HF_TOKEN or None)
473
+ try:
474
+ job = client.submit(
475
+ system or "",
476
+ user or "",
477
+ int(max_tokens or 400),
478
+ float(temperature if temperature is not None else 0.8),
479
+ api_name="/generate_stream",
480
+ )
481
+ prev = ""
482
+ for update in job:
483
+ text = update[0] if isinstance(update, (tuple, list)) else update
484
+ text = str(text or "")
485
+ if len(text) > len(prev):
486
+ yield text[len(prev):]
487
+ prev = text
488
+ except Exception:
489
+ text = _tiny_aya_generate(system, user, max_tokens, temperature)
490
+ if text:
491
+ yield text
492
+
493
+
494
  @fastapi_app.post("/voxcpm-tts")
495
  async def voxcpm_tts(request: Request):
496
  body = await request.json()
 
786
  if model == "tiny-aya-global-zerogpu":
787
  if not TINY_AYA_SPACE:
788
  raise llm.LlmUnavailable("TINY_AYA_SPACE not set")
789
+ for chunk in _tiny_aya_stream(system, user, max_tokens, temperature):
790
+ if stop.is_set():
791
+ break
792
+ loop.call_soon_threadsafe(q.put_nowait, ("delta", chunk))
793
  else:
794
  for chunk in llm.stream_chat(
795
  system,