studius / server /infer.py
2Btainment's picture
P4.4: talking-head avatar (MoDA lip-sync, Wav2Lip/ffmpeg fallbacks), upload filenames
61657d1 verified
Raw History Blame Contribute Delete
7.05 kB
"""Run allow-listed public Gradio Spaces server-side with the signed-in user's OAuth token
(user's own ZeroGPU / Space quota). Streams status + data as SSE. Result files are kept in a
short-lived tmp store (/api/tmp/<id>/<name>) – never the token. Anonymous users get 401 → frontend
falls back to the anonymous browser client."""
import asyncio, json, os, secrets, shutil, tempfile, time, threading, mimetypes
from pathlib import Path
from fastapi import APIRouter, Request, HTTPException, UploadFile
from fastapi.responses import StreamingResponse, FileResponse
from gradio_client import Client, handle_file
ALLOW = { # space -> allowed endpoints (Module API backends)
"huggingface-projects/llama-3.2-3B-Instruct": {"/generate"},
"black-forest-labs/FLUX.1-schnell": {"/infer"},
"black-forest-labs/FLUX.1-Kontext-Dev": {"/infer"},
"ACE-Step/ACE-Step": {"/__call__"},
"declare-lab/TangoFlux": {"/predict"},
"abidlabs/music-separation": {"/inference"},
"Lightricks/ltx-video-distilled": {"/text_to_video"},
"multimodalart/MoDA-fast-talking-head": {"/generate_motion"},
"pragnakalp/Wav2lip-ZeroGPU": {"/run_infrence"},
"Qwen/Qwen3-TTS": {"/generate_voice_design", "/generate_voice_clone"},
}
PATH_ARGS = {("Lightricks/ltx-video-distilled", "/text_to_video"): {"input_image_filepath", "input_video_filepath"}} # str-typed filepath args: upload to the Space, pass its server path
TMP: dict[str, tuple[str, float]] = {}
TMPDIR = Path(tempfile.gettempdir()) / "studius-tmp"; TMPDIR.mkdir(exist_ok=True)
CLIENTS: dict[tuple, tuple[Client, float]] = {}
router = APIRouter()
def tmp_put(src: str) -> str:
now = time.time()
for k, (p, e) in list(TMP.items()):
if e < now: TMP.pop(k, None); shutil.rmtree(Path(p).parent, ignore_errors=True)
i = secrets.token_urlsafe(18); d = TMPDIR / i; d.mkdir()
dst = d / Path(src).name; shutil.copy(src, dst); TMP[i] = (str(dst), now + 3600)
return f"/api/tmp/{i}/{dst.name}"
def tmp_path(url: str):
try: _, _, _, i, _ = url.split("/", 4)
except ValueError: return None
v = TMP.get(i)
if not v and i and "/" not in i and ".." not in i: # survive process restarts: re-index from disk (< 1 h old)
d = TMPDIR / i
if d.is_dir():
fs = [f for f in d.iterdir() if f.is_file()]
if fs and fs[0].stat().st_mtime + 3600 > time.time(): TMP[i] = v = (str(fs[0]), fs[0].stat().st_mtime + 3600)
return v[0] if v and v[1] > time.time() else None
def client(space, token):
k = (space, token); c = CLIENTS.get(k)
if c and c[1] > time.time(): return c[0]
for kk, (_, e) in list(CLIENTS.items()):
if e < time.time(): CLIENTS.pop(kk, None)
cl = Client(space, hf_token=token, verbose=False, download_files=str(TMPDIR / "dl")); CLIENTS[k] = (cl, time.time() + 900); return cl
def conv(x): # gradio outputs → JSON (files → tmp URLs)
if isinstance(x, (list, tuple)): return [conv(v) for v in x]
if isinstance(x, dict):
if x.get("path") and os.path.exists(str(x["path"])): return {"url": tmp_put(x["path"])}
return {k: conv(v) for k, v in x.items()}
if isinstance(x, str) and x.startswith(str(Path(tempfile.gettempdir()))) and os.path.isfile(x): return {"url": tmp_put(x)}
return x
def make_router(session):
@router.post("/api/run")
async def run(req: Request):
s = session(req)
if not s: raise HTTPException(401, "not signed in")
form = await req.form(); space = form.get("space"); ep = form.get("ep")
if ep not in ALLOW.get(space, ()): raise HTTPException(400, "space/endpoint not allowed")
args = json.loads(form.get("args") or "{}"); tmpd = tempfile.mkdtemp(dir=TMPDIR)
for k, v in list(args.items()): # {"__file__": "f0"} placeholders → uploaded parts
if isinstance(v, dict) and "__file__" in v:
up: UploadFile = form.get(v["__file__"]); p = Path(tmpd) / Path(up.filename or "in.bin").name
p.write_bytes(await up.read()); args[k] = handle_file(str(p))
elif isinstance(v, dict) and "__tmp__" in v:
p = tmp_path(v["__tmp__"]);
if not p: raise HTTPException(400, "tmp file expired")
args[k] = handle_file(p)
timeout = min(float(form.get("timeout") or 240), 600); tok = s["token"]
for k in PATH_ARGS.get((space, ep), ()):
v = args.get(k)
if isinstance(v, dict) and v.get("path"): # handle_file() result -> upload to the Space's /gradio_api/upload
c = client(space, tok); base = c.src.rstrip("/")
import httpx as _hx
with open(v["path"], "rb") as fh:
r = _hx.post(f"{base}/gradio_api/upload", files={"files": (Path(v["path"]).name, fh)}, headers={"Authorization": f"Bearer {tok}"}, timeout=120)
if r.status_code != 200: raise HTTPException(502, f"upload to space failed: HTTP {r.status_code}")
args[k] = r.json()[0]
q: asyncio.Queue = asyncio.Queue(); loop = asyncio.get_running_loop()
def worker():
emit = lambda e: loop.call_soon_threadsafe(q.put_nowait, e)
try:
job = client(space, tok).submit(**args, api_name=ep); t0 = time.time(); seen = 0; last = None
while not job.done():
if time.time() - t0 > timeout: job.cancel(); raise TimeoutError(f"Zeitüberschreitung nach {int(timeout)} s")
st = job.status(); outs = job.outputs()
for o in outs[seen:]: emit({"type": "data", "data": conv(o)})
seen = len(outs)
sd = {"type": "status", "stage": {"PROCESSING": "generating", "ITERATING": "generating"}.get(st.code.name, "pending"), "position": st.rank, "queue_size": st.queue_size, "eta": st.eta}
if sd != last: emit(sd); last = sd
time.sleep(0.25)
res = job.result(); emit({"type": "data", "data": conv(res)}); emit({"type": "done"})
except Exception as e:
emit({"type": "error", "message": str(e).split("\n")[0][:400]})
finally:
shutil.rmtree(tmpd, ignore_errors=True); emit(None)
threading.Thread(target=worker, daemon=True).start()
async def gen():
while True:
e = await q.get()
if e is None: break
yield f"data: {json.dumps(e)}\n\n"
if await req.is_disconnected(): break
return StreamingResponse(gen(), media_type="text/event-stream", headers={"Cache-Control": "no-store", "X-Accel-Buffering": "no"})
@router.get("/api/tmp/{i}/{name}")
def tmp_file(i: str, name: str):
p = tmp_path(f"/api/tmp/{i}/{name}")
if not p or Path(p).name != name: raise HTTPException(404)
return FileResponse(p, media_type=mimetypes.guess_type(name)[0] or "application/octet-stream", headers={"Cache-Control": "private, max-age=3600"})
return router