studius / server /main.py
2Btainment's picture
P9.2: Forge-Proxy mit Space-Token + 429-Retry
b831af6 verified
Raw History Blame Contribute Delete
35 kB
"""STUDIUS backend: serves the 3D frontend, HF OAuth sign-in, workspace (user's HF Storage Bucket) API.
Tokens live only in server memory for the session (no disk, no logs) and are dropped on logout/expiry."""
import asyncio, shutil, os, io, json, time, secrets, base64, tempfile, mimetypes, urllib.parse, re
from pathlib import Path
import httpx
from fastapi import FastAPI, Request, UploadFile, File, Form, HTTPException
from fastapi.responses import RedirectResponse, JSONResponse, FileResponse, Response, StreamingResponse
from fastapi.staticfiles import StaticFiles
from huggingface_hub import HfApi
from .infer import make_router, tmp_path, client as gr_client
from .agents import make_agents_router
ROOT = Path(__file__).resolve().parent.parent
HF = os.getenv("OPENID_PROVIDER_URL", "https://huggingface.co").rstrip("/")
CID, CSECRET = os.getenv("OAUTH_CLIENT_ID"), os.getenv("OAUTH_CLIENT_SECRET")
SCOPES = os.getenv("OAUTH_SCOPES", "openid profile")
HOST = os.getenv("SPACE_HOST")
SESS: dict[str, dict] = {} # sid -> {token, user, exp}
STATES: dict[str, float] = {}
TTL = 8 * 3600
MAX_UP = 200 * 1024 * 1024
ALLOW_FETCH = re.compile(r"^https://[a-z0-9-]+\.hf\.space/")
app = FastAPI(title="STUDIUS", docs_url=None, redoc_url=None)
def cookie(resp, sid):
resp.set_cookie("studius_sid", sid, max_age=TTL, httponly=True, secure=True, samesite="none")
def session(req: Request):
now = time.time()
for k in [k for k, v in SESS.items() if v["exp"] < now]: SESS.pop(k, None)
s = SESS.get(req.cookies.get("studius_sid", ""))
if s: return s
auth = req.headers.get("authorization", "") # API clients/tests may pass their own HF token per request (never stored)
if auth.lower().startswith("bearer hf_"):
tok = auth[7:]
try: who = HfApi(token=tok).whoami()
except Exception: raise HTTPException(401, "invalid token")
return {"token": tok, "user": {"name": who["name"], "avatar": who.get("avatarUrl")}, "exp": now + 60}
return None
def need(req):
s = session(req)
if not s: raise HTTPException(401, "not signed in")
return s
@app.get("/healthz")
def healthz(): return {"ok": True, "oauth": bool(CID)}
@app.get("/login")
def login(req: Request):
if not CID: return JSONResponse({"error": "OAuth not configured (hf_oauth: true missing)"}, 503)
st = secrets.token_urlsafe(24); STATES[st] = time.time() + 600
redirect = f"https://{HOST}/login/callback" if HOST else str(req.url_for("callback"))
q = urllib.parse.urlencode({"client_id": CID, "redirect_uri": redirect, "scope": SCOPES, "state": st, "response_type": "code"})
return RedirectResponse(f"{HF}/oauth/authorize?{q}")
@app.get("/login/callback", name="callback")
async def callback(code: str = "", state: str = ""):
if STATES.pop(state, 0) < time.time(): raise HTTPException(400, "invalid state")
redirect = f"https://{HOST}/login/callback"
basic = base64.b64encode(f"{CID}:{CSECRET}".encode()).decode()
async with httpx.AsyncClient(timeout=20) as c:
r = await c.post(f"{HF}/oauth/token", data={"grant_type": "authorization_code", "code": code, "redirect_uri": redirect, "client_id": CID}, headers={"Authorization": f"Basic {basic}"})
if r.status_code != 200: raise HTTPException(400, "token exchange failed")
tok = r.json()["access_token"]; exp = min(TTL, int(r.json().get("expires_in", TTL)))
u = (await c.get(f"{HF}/oauth/userinfo", headers={"Authorization": f"Bearer {tok}"})).json()
sid = secrets.token_urlsafe(32)
SESS[sid] = {"token": tok, "user": {"name": u.get("preferred_username"), "avatar": u.get("picture")}, "exp": time.time() + exp}
resp = RedirectResponse("/?signedin=1"); cookie(resp, sid); return resp
@app.post("/logout")
def logout(req: Request):
SESS.pop(req.cookies.get("studius_sid", ""), None); r = JSONResponse({"ok": True}); r.delete_cookie("studius_sid"); return r
@app.get("/api/me")
def me(req: Request):
s = session(req)
return {"signedIn": bool(s), "user": s and s["user"], "oauth": bool(CID), "scopes": SCOPES}
# ---------- workspace (HF Storage Bucket) ----------
def bid(bucket: str, s):
if not re.fullmatch(r"[A-Za-z0-9][\w.-]*/[A-Za-z0-9][\w.-]{0,95}", bucket or ""): raise HTTPException(400, "bucket must be 'namespace/name'")
return bucket
def safe(path: str):
p = (path or "").strip("/")
if not p or ".." in p.split("/") or len(p) > 400: raise HTTPException(400, "bad path")
return p
def herr(e):
msg = str(e).split("\n")[0][:300]; code = getattr(getattr(e, "response", None), "status_code", 500) or 500
raise HTTPException(code if code in (401, 403, 404, 409) else 502, msg)
@app.get("/api/ws/buckets")
def ws_buckets(req: Request):
"""All buckets the user can see: own namespace + every org (per-owner errors reported, not fatal)."""
s = need(req); api = HfApi(token=s["token"])
owners = [s["user"]["name"]]
try: owners += [o["name"] for o in (api.whoami().get("orgs") or []) if o.get("name")]
except Exception: pass
out, errs = [], {}
from concurrent.futures import ThreadPoolExecutor
def one(o):
try: return o, [{"id": b.id, "owner": o, "private": b.private, "updated": (getattr(b, "updated_at", None) or getattr(b, "last_modified", None) or "") and str(getattr(b, "updated_at", None) or getattr(b, "last_modified", None))} for b in api.list_buckets(o)], None
except Exception as e: return o, [], str(e).split("\n")[0][:160]
with ThreadPoolExecutor(6) as ex:
for o, bs, e in ex.map(one, owners):
out += bs
if e: errs[o] = e
return {"user": s["user"]["name"], "owners": owners, "buckets": out, "errors": errs}
@app.post("/api/ws/connect")
def ws_connect(req: Request, bucket: str = Form(...), create: bool = Form(False)):
s = need(req); api = HfApi(token=s["token"]); bucket = bid(bucket, s)
try:
if create: api.create_bucket(bucket, private=True, exist_ok=True)
info = api.bucket_info(bucket)
if create: # scaffold layout
api.batch_bucket_files(bucket, add=[(json.dumps({"app": "STUDIUS", "layout": 1, "created": int(time.time())}).encode(), ".studius/workspace.json")])
return {"bucket": info.id, "private": info.private}
except Exception as e: herr(e)
# ---- per-file sidecar metadata (.studius/meta/<path>.json) + versions (.studius/versions/<path>/v<N>__<name>) ----
HUB = os.environ.get("HF_ENDPOINT", "https://huggingface.co")
MD = ".studius/meta/"; VD = ".studius/versions/"
def mpath(p): return f"{MD}{p}.json"
def legacy(api, bucket): # read-only fallback for Phase-1 single index (never written anymore)
try:
with tempfile.TemporaryDirectory() as d:
api.download_bucket_files(bucket, [(".studius/assets.json", f"{d}/a.json")]); return json.loads(Path(f"{d}/a.json").read_text())
except Exception: return {}
def metas(api, bucket, paths):
"""Batch-download sidecars for paths -> {path: meta}."""
out = {}; paths = list(paths)
if not paths: return out
with tempfile.TemporaryDirectory() as d:
pairs = [(mpath(p), f"{d}/{i}.json") for i, p in enumerate(paths)]
try: api.download_bucket_files(bucket, pairs, raise_on_missing_files=False)
except TypeError: api.download_bucket_files(bucket, pairs)
except Exception: pass
for i, p in enumerate(paths):
f = Path(f"{d}/{i}.json")
if f.exists():
try: out[p] = json.loads(f.read_text())
except Exception: pass
return out
def getmeta(api, bucket, p):
m = metas(api, bucket, [p]).get(p)
return m if m is not None else legacy(api, bucket).get(p, {})
def info(api, bucket, p):
r = list(api.get_bucket_paths_info(bucket, [p])); return r[0] if r else None
def snapshot(api, bucket, p, m):
"""Server-side copy (by xet hash) of the current file into versions/ before overwrite/delete."""
cur = info(api, bucket, p)
if not cur: return None
v = int(m.get("v", 1) or 1); dst = f"{VD}{p}/v{v}__{Path(p).name}"
api.batch_bucket_files(bucket, copy=[("bucket", bucket, cur.xet_hash, dst)],
add=[(json.dumps({**m, "path": p, "archived": int(time.time())}, indent=1).encode(), dst + ".json")])
return dst
@app.get("/api/ws/list")
def ws_list(req: Request, bucket: str, prefix: str = ""):
s = need(req); api = HfApi(token=s["token"]); bucket = bid(bucket, s)
try:
files = [f for f in api.list_bucket_tree(bucket, prefix=prefix or None, recursive=True) if getattr(f, "type", "") == "file" and not f.path.startswith(".studius/")]
side = {f.path for f in api.list_bucket_tree(bucket, prefix=MD.rstrip("/"), recursive=True) if getattr(f, "type", "") == "file"}
want = [f.path for f in files if mpath(f.path) in side]
meta = metas(api, bucket, want); old = legacy(api, bucket) if len(want) < len(files) else {}
out = []
for f in files:
m = meta.get(f.path) or old.get(f.path)
out.append({"path": f.path, "size": f.size, "mtime": f.mtime.isoformat() if f.mtime else None, "mime": mimetypes.guess_type(f.path)[0] or "application/octet-stream", **({"meta": m} if m else {})})
return {"bucket": bucket, "files": sorted(out, key=lambda x: x["mtime"] or "", reverse=True)}
except Exception as e: herr(e)
def put(api, bucket, data: bytes, path: str, meta: dict):
old = getmeta(api, bucket, path); exists = info(api, bucket, path) is not None
if exists: snapshot(api, bucket, path, {**old, "v": old.get("v", 1)})
m = {**{k: v for k, v in old.items() if k in ("pins",)}, **meta, "saved": int(time.time()), "v": (old.get("v", 1) + 1) if exists else 1}
api.batch_bucket_files(bucket, add=[(data, path), (json.dumps(m, indent=1).encode(), mpath(path))])
return {"path": path, "size": len(data), "meta": m}
@app.post("/api/ws/upload")
async def ws_upload(req: Request, bucket: str = Form(...), path: str = Form(""), studio: str = Form("upload"), file: UploadFile = File(...)):
s = need(req); api = HfApi(token=s["token"]); bucket = bid(bucket, s)
data = await file.read(MAX_UP + 1)
if len(data) > MAX_UP: raise HTTPException(413, "max 200 MB")
p = safe(path or f"uploads/{file.filename}")
try: return put(api, bucket, data, p, {"studio": studio, "mime": file.content_type, "source": "upload", "filename": file.filename})
except Exception as e: herr(e)
@app.post("/api/ws/save-url")
async def ws_save_url(req: Request, bucket: str = Form(...), url: str = Form(...), path: str = Form(...), studio: str = Form(""), prompt: str = Form(""), model: str = Form("")):
"""Save a studio result (server tmp result or public *.hf.space file URL) into the workspace server-side."""
s = need(req); api = HfApi(token=s["token"]); bucket = bid(bucket, s)
if url.startswith("/api/tmp/"):
lp = tmp_path(url)
if not lp: raise HTTPException(410, "result expired")
content = Path(lp).read_bytes(); src = "hf:" + (model or "user-quota")
else:
if not ALLOW_FETCH.match(url): raise HTTPException(400, "only *.hf.space result URLs")
async with httpx.AsyncClient(timeout=60, follow_redirects=True) as c:
r = await c.get(url)
if r.status_code != 200 or len(r.content) > MAX_UP: raise HTTPException(502, "fetch failed")
content = r.content; src = url.split("/gradio_api")[0].split("/file=")[0]
try: return put(api, bucket, content, safe(path), {"studio": studio, "prompt": prompt[:500], "source": src, **({"model": model} if model else {})})
except Exception as e: herr(e)
@app.get("/api/ws/meta")
def ws_meta(req: Request, bucket: str, path: str):
s = need(req); api = HfApi(token=s["token"]); bucket = bid(bucket, s); p = safe(path)
try: return {"path": p, "meta": getmeta(api, bucket, p)}
except Exception as e: herr(e)
@app.get("/api/ws/versions")
def ws_versions(req: Request, bucket: str, path: str):
s = need(req); api = HfApi(token=s["token"]); bucket = bid(bucket, s); p = safe(path)
try:
vs = []; nm = re.escape(Path(p).name)
for f in api.list_bucket_tree(bucket, prefix=f"{VD}{p}", recursive=True):
mm = re.fullmatch(rf"v(\d+)__{nm}", f.path.rsplit("/", 1)[-1])
if getattr(f, "type", "") == "file" and mm and f.path.startswith(f"{VD}{p}/"):
vs.append({"v": int(mm.group(1)), "vpath": f.path, "size": f.size, "mtime": f.mtime.isoformat() if f.mtime else None})
return {"path": p, "current": getmeta(api, bucket, p).get("v"), "versions": sorted(vs, key=lambda x: -x["v"])}
except Exception as e: herr(e)
@app.post("/api/ws/restore")
def ws_restore(req: Request, bucket: str = Form(...), path: str = Form(...), v: int = Form(...)):
"""Restore version v as a NEW current version (current one is archived first; nothing is lost)."""
s = need(req); api = HfApi(token=s["token"]); bucket = bid(bucket, s); p = safe(path)
try:
src = f"{VD}{p}/v{v}__{Path(p).name}"; old = info(api, bucket, src)
if not old: raise HTTPException(404, "version not found")
m = getmeta(api, bucket, p); exists = info(api, bucket, p) is not None
if exists: snapshot(api, bucket, p, m)
nm = {**m, "v": (m.get("v", 1) + 1) if exists else v + 1, "saved": int(time.time()), "restored_from": v}
api.batch_bucket_files(bucket, copy=[("bucket", bucket, old.xet_hash, p)], add=[(json.dumps(nm, indent=1).encode(), mpath(p))])
return {"path": p, "meta": nm}
except HTTPException: raise
except Exception as e: herr(e)
@app.get("/api/ws/file")
async def ws_file(req: Request, bucket: str, path: str, dl: int = 0):
"""Streamed proxy from the bucket resolve URL (supports HTTP Range for audio/video seeking)."""
s = need(req); bucket = bid(bucket, s); p = safe(path)
from urllib.parse import quote
h = {"Authorization": f"Bearer {s['token']}"}
if req.headers.get("range"): h["Range"] = req.headers["range"]
c = httpx.AsyncClient(timeout=httpx.Timeout(60, read=300), follow_redirects=True)
r = await c.send(c.build_request("GET", f"{HUB}/buckets/{bucket}/resolve/{quote(p, safe='')}", headers=h), stream=True)
if r.status_code not in (200, 206):
await r.aclose(); await c.aclose(); raise HTTPException(404 if r.status_code == 404 else 502, f"bucket file: HTTP {r.status_code}")
oh = {"Cache-Control": "private, no-store", "Accept-Ranges": "bytes"}
for k in ("content-length", "content-range"):
if r.headers.get(k): oh[k] = r.headers[k]
if dl: oh["Content-Disposition"] = f'attachment; filename="{Path(p).name}"'
async def body():
try:
async for chunk in r.aiter_bytes(256 * 1024): yield chunk
finally: await r.aclose(); await c.aclose()
return StreamingResponse(body(), status_code=r.status_code, media_type=mimetypes.guess_type(p)[0] or "application/octet-stream", headers=oh)
@app.post("/api/ws/delete")
def ws_delete(req: Request, bucket: str = Form(...), path: str = Form(...), confirm: str = Form("")):
"""Delete from the workspace view; the last version is archived in .studius/versions/ first."""
s = need(req); api = HfApi(token=s["token"]); bucket = bid(bucket, s); p = safe(path)
if confirm != p: raise HTTPException(400, "confirm must equal path")
try:
m = getmeta(api, bucket, p); arch = snapshot(api, bucket, p, {**m, "v": m.get("v", 1)})
dels = [p] + ([mpath(p)] if info(api, bucket, mpath(p)) else [])
api.batch_bucket_files(bucket, delete=dels); return {"deleted": p, "archived": arch}
except Exception as e: herr(e)
@app.post("/api/ws/pin")
def ws_pin(req: Request, bucket: str = Form(...), path: str = Form(...), studio: str = Form(...), on: bool = Form(True)):
s = need(req); api = HfApi(token=s["token"]); bucket = bid(bucket, s); p = safe(path)
try:
e = getmeta(api, bucket, p); pins = set(e.get("pins", []))
(pins.add if on else pins.discard)(studio); e["pins"] = sorted(pins)
api.batch_bucket_files(bucket, add=[(json.dumps(e, indent=1).encode(), mpath(p))]); return {"path": p, "pins": e["pins"]}
except Exception as e: herr(e)
@app.post("/api/transcode")
async def transcode(req: Request, file: UploadFile = File(...), name: str = Form("video"), bucket: str = Form(""), path: str = Form(""), studio: str = Form("videoedit"), prompt: str = Form("")):
"""WebM/any video -> H.264 (High, yuv420p) + AAC 48k MP4 with faststart (iPhone/Instagram/TikTok-safe).
Returns a tmp URL; optionally saves straight into the workspace bucket."""
import asyncio, shutil, subprocess
from .infer import tmp_put
s = need(req)
if not shutil.which("ffmpeg"): raise HTTPException(503, "ffmpeg not installed")
data = await file.read(MAX_UP + 1)
if len(data) > MAX_UP: raise HTTPException(413, "max 200 MB")
d = tempfile.mkdtemp(); src = f"{d}/in.bin"; stem = re.sub(r"[^\w.-]+", "-", Path(name).stem)[:80] or "video"; out = f"{d}/{stem}.mp4"
Path(src).write_bytes(data)
cmd = ["ffmpeg", "-hide_banner", "-loglevel", "error", "-y", "-i", src, "-map", "0:v:0", "-map", "0:a:0?",
"-vf", "scale=trunc(iw/2)*2:trunc(ih/2)*2:out_range=tv,fps=30,format=yuv420p", "-pix_fmt", "yuv420p", "-color_range", "tv", "-c:v", "libx264", "-profile:v", "high", "-level", "4.1", "-preset", "veryfast", "-crf", "20",
"-c:a", "aac", "-b:a", "160k", "-ar", "48000", "-ac", "2", "-shortest", "-movflags", "+faststart", out]
p = await asyncio.to_thread(subprocess.run, cmd, capture_output=True, text=True, timeout=600)
if p.returncode != 0 or not os.path.exists(out): shutil.rmtree(d, ignore_errors=True); raise HTTPException(422, "ffmpeg: " + (p.stderr or "failed")[-300:])
pr = await asyncio.to_thread(subprocess.run, ["ffprobe", "-v", "error", "-show_entries", "stream=codec_name,codec_type,width,height,profile,pix_fmt:format=duration", "-of", "json", out], capture_output=True, text=True)
info = json.loads(pr.stdout or "{}"); u = tmp_put(out); size = os.path.getsize(out); res = {"url": u, "size": size, "probe": info}
if bucket and path:
api = HfApi(token=s["token"])
try: res["saved"] = put(api, bid(bucket, s), Path(out).read_bytes(), safe(path), {"studio": studio, "prompt": prompt[:500], "source": "transcode:h264", "mime": "video/mp4"})
except Exception as e: res["save_error"] = str(e)[:200]
shutil.rmtree(d, ignore_errors=True)
return res
@app.post("/api/stillvideo")
async def stillvideo(req: Request, image: UploadFile = File(...), audio: UploadFile = File(...), name: str = Form("talk")):
"""Talking-head fallback without GPU: still avatar image + voice -> gentle zoom H.264/AAC MP4 (faststart)."""
import asyncio, shutil, subprocess
from .infer import tmp_put
need(req)
if not shutil.which("ffmpeg"): raise HTTPException(503, "ffmpeg not installed")
d = tempfile.mkdtemp(); im, au = f"{d}/in.img", f"{d}/in.aud"; stem = re.sub(r"[^\w.-]+", "-", Path(name).stem)[:80] or "talk"; out = f"{d}/{stem}.mp4"
for f, pth in ((image, im), (audio, au)):
b = await f.read(50 * 2**20 + 1)
if len(b) > 50 * 2**20: shutil.rmtree(d, ignore_errors=True); raise HTTPException(413, "max 50 MB")
Path(pth).write_bytes(b)
vf = "scale=1024:1024:force_original_aspect_ratio=increase,crop=1024:1024,zoompan=z='min(1.0+0.0008*on,1.15)':x='iw/2-(iw/zoom/2)':y='ih/2-(ih/zoom/2)':d=1:s=720x720:fps=30,format=yuv420p"
cmd = ["ffmpeg", "-hide_banner", "-loglevel", "error", "-y", "-loop", "1", "-i", im, "-i", au, "-vf", vf, "-c:v", "libx264", "-profile:v", "high", "-level", "4.1", "-preset", "veryfast", "-crf", "21", "-pix_fmt", "yuv420p",
"-c:a", "aac", "-b:a", "160k", "-ar", "48000", "-ac", "2", "-shortest", "-t", "120", "-movflags", "+faststart", out]
p = await asyncio.to_thread(subprocess.run, cmd, capture_output=True, text=True, timeout=300)
if p.returncode != 0 or not os.path.exists(out): shutil.rmtree(d, ignore_errors=True); raise HTTPException(422, "ffmpeg: " + (p.stderr or "failed")[-300:])
u = tmp_put(out); shutil.rmtree(d, ignore_errors=True)
return {"url": u}
@app.post("/api/reframe")
async def reframe(req: Request, file: UploadFile | None = File(None), src: str = Form(""), aspect: str = Form("9:16"), mode: str = Form("blur"), name: str = Form("video")):
"""Reframe a video for platforms: 9:16 / 16:9 / 1:1 / 4:5, either blurred-background fill or centre crop. H.264/AAC faststart."""
import asyncio, shutil, subprocess
from .infer import tmp_put, tmp_path
need(req)
if not shutil.which("ffmpeg"): raise HTTPException(503, "ffmpeg not installed")
dims = {"9:16": (1080, 1920), "16:9": (1920, 1080), "1:1": (1080, 1080), "4:5": (1080, 1350)}
if aspect not in dims or mode not in ("blur", "crop"): raise HTTPException(400, "aspect/mode")
W, H = dims[aspect]; d = tempfile.mkdtemp(); stem = re.sub(r"[^\w.-]+", "-", Path(name).stem)[:70] or "video"; out = f"{d}/{stem}-{aspect.replace(':', 'x')}.mp4"
if src:
inp = tmp_path(src)
if not inp: shutil.rmtree(d, ignore_errors=True); raise HTTPException(400, "tmp file expired")
elif file is not None:
b = await file.read(MAX_UP + 1)
if len(b) > MAX_UP: shutil.rmtree(d, ignore_errors=True); raise HTTPException(413, "max 200 MB")
inp = f"{d}/in.bin"; Path(inp).write_bytes(b)
else: raise HTTPException(400, "file or src required")
if mode == "crop": fc = f"[0:v]scale={W}:{H}:force_original_aspect_ratio=increase,crop={W}:{H},setsar=1,format=yuv420p[v]"
else: fc = (f"[0:v]split[a][b];[a]scale={W}:{H}:force_original_aspect_ratio=increase,crop={W}:{H},boxblur=30:2,eq=brightness=-0.08[bg];"
f"[b]scale={W}:{H}:force_original_aspect_ratio=decrease[fg];[bg][fg]overlay=(W-w)/2:(H-h)/2,setsar=1,format=yuv420p[v]")
cmd = ["ffmpeg", "-hide_banner", "-loglevel", "error", "-y", "-i", inp, "-filter_complex", fc, "-map", "[v]", "-map", "0:a:0?", "-r", "30", "-c:v", "libx264", "-profile:v", "high", "-level", "4.1",
"-preset", "veryfast", "-crf", "21", "-pix_fmt", "yuv420p", "-c:a", "aac", "-b:a", "160k", "-ar", "48000", "-ac", "2", "-movflags", "+faststart", out]
p = await asyncio.to_thread(subprocess.run, cmd, capture_output=True, text=True, timeout=600)
if p.returncode != 0 or not os.path.exists(out): shutil.rmtree(d, ignore_errors=True); raise HTTPException(422, "ffmpeg: " + (p.stderr or "failed")[-300:])
u = tmp_put(out); shutil.rmtree(d, ignore_errors=True)
return {"url": u, "width": W, "height": H, "aspect": aspect, "mode": mode}
@app.post("/api/doc/export")
async def doc_export(req: Request, md: str = Form(...), title: str = Form("Dokument"), fmt: str = Form("docx"), accent: str = Form("#c8941e"), brand: str = Form("")):
"""Markdown (subset: #/##/###, lists, **bold**, paragraphs) -> DOCX (python-docx) or PDF (fpdf2). Returns a tmp URL."""
from .infer import tmp_put
need(req)
if fmt not in ("docx", "pdf"): raise HTTPException(400, "fmt")
md = md[:200000]; d = tempfile.mkdtemp(); stem = re.sub(r"[^\w.-]+", "-", title)[:60] or "dokument"; out = f"{d}/{stem}.{fmt}"
rgb = tuple(int(accent.lstrip("#")[i:i + 2], 16) for i in (0, 2, 4)) if re.fullmatch(r"#[0-9a-fA-F]{6}", accent) else (200, 148, 30)
lines = md.replace("\r", "").split("\n")
def runs(t): # split **bold**
parts = re.split(r"(\*\*[^*]+\*\*)", t); return [(x[2:-2], True) if x.startswith("**") and x.endswith("**") else (x, False) for x in parts if x]
if fmt == "docx":
from docx import Document
from docx.shared import RGBColor
doc = Document(); doc.core_properties.title = title
if brand: doc.sections[0].header.paragraphs[0].text = brand
for ln in lines:
m = re.match(r"^(#{1,3})\s+(.*)", ln)
if m:
h = doc.add_heading(m.group(2).strip(), level=len(m.group(1)))
for r in h.runs: r.font.color.rgb = RGBColor(*rgb)
continue
m = re.match(r"^\s*([-*]|\d+\.)\s+(.*)", ln)
if m: p = doc.add_paragraph(style="List Number" if m.group(1)[0].isdigit() else "List Bullet"); t = m.group(2)
elif not ln.strip(): continue
else: p = doc.add_paragraph(); t = ln
for x, b in runs(t): p.add_run(x).bold = b
doc.save(out)
else:
from fpdf import FPDF
L = lambda t: t.encode("latin-1", "replace").decode("latin-1")
pdf = FPDF(); pdf.set_auto_page_break(True, 18); pdf.add_page(); pdf.set_title(L(title))
if brand: pdf.set_font("Helvetica", "B", 9); pdf.set_text_color(*rgb); pdf.cell(0, 6, L(brand), new_x="LMARGIN", new_y="NEXT"); pdf.set_draw_color(*rgb); pdf.line(10, pdf.get_y(), 200, pdf.get_y()); pdf.ln(4)
for ln in lines:
m = re.match(r"^(#{1,3})\s+(.*)", ln)
if m: sz = {1: 20, 2: 15, 3: 12}[len(m.group(1))]; pdf.ln(2); pdf.set_font("Helvetica", "B", sz); pdf.set_text_color(*rgb); pdf.multi_cell(0, sz * .5, L(m.group(2).strip())); pdf.ln(1); continue
if not ln.strip(): pdf.ln(2); continue
m = re.match(r"^\s*([-*]|\d+\.)\s+(.*)", ln); pdf.set_text_color(20, 20, 20); pdf.set_font("Helvetica", "", 11)
t = ("- " if m and not m.group(1)[0].isdigit() else (m.group(1) + " " if m else "")) + (m.group(2) if m else ln)
pdf.multi_cell(0, 5.5, L(t), markdown=True); pdf.ln(.5)
pdf.output(out)
u = tmp_put(out); shutil.rmtree(d, ignore_errors=True)
return {"url": u, "fmt": fmt, "name": f"{stem}.{fmt}"}
@app.get("/api/quota")
async def quota(req: Request):
"""User's own ZeroGPU quota (GPU-seconds) from the Hub. Token stays server-side."""
s = session(req)
if not s: return {"ok": False, "reason": "anon", "note": "Anonym: ca. 2 min GPU/Tag pro IP"}
async with httpx.AsyncClient(timeout=15) as c:
r = await c.get(f"{HUB}/api/spaces/zero-gpu/quota", headers={"Authorization": f"Bearer {s['token']}"})
if r.status_code != 200: return {"ok": False, "reason": f"HTTP {r.status_code}", "note": "Quota für dieses Login nicht abrufbar"}
q = r.json(); return {"ok": True, "base": q.get("base"), "remaining": q.get("current"), "resetsAt": q.get("resetsAt"), "overquotaUsed": q.get("overquotaUsed", 0), "runs": q.get("runs")}
# ---------- P7: Admin config (owner-only write) + RepoSpace bridge ----------
OWNER = os.getenv("STUDIUS_OWNER", "2Btainment")
CFG: dict = {"cfg": None, "bucket": os.getenv("STUDIUS_CONFIG_BUCKET", "")}
CFG_PATH = ".studius/config.json"
def is_owner(s): return bool(s) and s["user"]["name"] == OWNER
def cfg_boot():
tok = os.getenv("HF_TOKEN"); b = CFG["bucket"]
if CFG["cfg"] is not None or not (tok and b): return
try:
api = HfApi(token=tok); d = tempfile.mkdtemp(); api.download_bucket_files(b, [(CFG_PATH, f"{d}/c.json")])
CFG["cfg"] = json.loads(Path(f"{d}/c.json").read_text())
except Exception: CFG["cfg"] = {}
@app.get("/api/config")
def get_config(req: Request):
cfg_boot(); s = session(req)
return {"config": CFG["cfg"] or {}, "owner": OWNER, "isOwner": is_owner(s), "signedIn": bool(s), "bucket": CFG["bucket"]}
@app.post("/api/config")
async def set_config(req: Request, config: str = Form(...), bucket: str = Form("")):
s = need(req)
if not is_owner(s): raise HTTPException(403, "nur Owner")
try: c = json.loads(config); assert isinstance(c, dict) and len(config) < 200_000
except Exception: raise HTTPException(400, "bad config")
CFG["cfg"] = c; saved = None
if bucket:
try:
api = HfApi(token=s["token"]); bid(bucket, s); put(api, bucket, json.dumps(c, indent=1).encode(), CFG_PATH, {"studio": "admin"})
CFG["bucket"] = bucket; saved = f"{bucket}/{CFG_PATH}"
except HTTPException: raise
except Exception as e: herr(e)
return {"ok": True, "saved": saved}
WORKER = os.getenv("STUDIUS_WORKER", "2Btainment/studius-worker"); WORKER_MODEL = "studius-worker/Qwen3-Coder-30B-A3B-Instruct"
LLM_MODELS = ["Qwen/Qwen2.5-Coder-32B-Instruct", "meta-llama/Llama-3.3-70B-Instruct", "Qwen/Qwen2.5-72B-Instruct"]
@app.post("/api/llm")
async def llm_ep(req: Request, prompt: str = Form(...), system: str = Form(""), max_tokens: int = Form(1200)):
"""Shared LLM for Dev-Baukasten + Admin: user's own HF token via providers, anonymous fallback = Llama-3.2-3B Space."""
import asyncio
s = session(req); prompt = prompt[:24000]; mt = max(64, min(max_tokens, 4000)); err = ""
msgs = ([{"role": "system", "content": system}] if system else []) + [{"role": "user", "content": prompt}]
wtok = (s or {}).get("token") or os.getenv("HF_TOKEN")
if wtok and WORKER:
try:
from gradio_client import Client
def w():
return Client(WORKER, hf_token=wtok, verbose=False).predict(msgs, min(mt, 4000), 0.3, api_name="/chat")
out = await asyncio.wait_for(asyncio.to_thread(w), 240)
if isinstance(out, str) and out.strip(): return {"text": out, "model": WORKER_MODEL}
except Exception as e: err = "worker: " + str(e)[:160]
if s:
from huggingface_hub import InferenceClient
msgs = ([{"role": "system", "content": system}] if system else []) + [{"role": "user", "content": prompt}]
for m in LLM_MODELS:
try:
out = await asyncio.to_thread(lambda: InferenceClient(token=s["token"], timeout=120).chat_completion(model=m, messages=msgs, max_tokens=mt, temperature=0.3))
return {"text": out.choices[0].message.content, "model": m}
except Exception as e: err += " | " + str(e)[:160]
try:
from gradio_client import Client
def g():
c = Client("huggingface-projects/llama-3.2-3B-Instruct", hf_token=(s or {}).get("token"), verbose=False)
return c.predict((system + "\n\n" if system else "") + prompt, min(mt, 2048), 0.3, 0.9, 50, 1.1, api_name="/generate")
out = await asyncio.to_thread(g)
return {"text": out if isinstance(out, str) else json.dumps(out), "model": "huggingface-projects/llama-3.2-3B-Instruct", "note": err}
except Exception as e: raise HTTPException(502, f"LLM nicht erreichbar: {str(e)[:200]} {err}")
REPOSPACE = os.getenv("REPOSPACE_URL", "https://2btainment-repospace.hf.space").rstrip("/")
@app.post("/api/repo/session")
async def repo_session(repo_url: str = Form(...)):
if not re.fullmatch(r"https://github\.com/[\w.-]+/[\w.-]+/?", repo_url.strip()): raise HTTPException(400, "nur https://github.com/owner/repo")
async with httpx.AsyncClient(timeout=60) as c:
r = await c.post(f"{REPOSPACE}/api/sessions", json={"repo_url": repo_url.strip()})
if r.status_code >= 400: raise HTTPException(502, f"RepoSpace HTTP {r.status_code}: {r.text[:200]}")
d = r.json(); return {**d, "base": REPOSPACE}
@app.get("/api/repo/session/{sid}")
async def repo_status(sid: str):
if not re.fullmatch(r"[\w-]{1,80}", sid): raise HTTPException(400, "bad id")
async with httpx.AsyncClient(timeout=30) as c:
r = await c.get(f"{REPOSPACE}/api/sessions/{sid}"); lg = await c.get(f"{REPOSPACE}/api/sessions/{sid}/logs")
if r.status_code >= 400: raise HTTPException(502, f"RepoSpace HTTP {r.status_code}")
return {**r.json(), "logs": lg.text[-3000:] if lg.status_code < 400 else "", "base": REPOSPACE}
SPACEFORGE = os.getenv("SPACEFORGE_URL", "https://2btainment-spaceforge.hf.space").rstrip("/")
def _fh(): # Space-Token (falls gesetzt) gegen anonymes HF-Edge-Rate-Limit (429) auf *.hf.space
t = os.getenv("HF_TOKEN"); return {"Authorization": f"Bearer {t}"} if t else {}
async def _freq(c, m, url, **kw):
for i in range(3):
r = await c.request(m, url, headers=_fh(), **kw)
if r.status_code != 429: return r
await asyncio.sleep(2 + 3 * i)
return r
FORGE_T = {"html_widget", "pwa", "android", "linux_appimage", "arch_pkgbuild", "mcp"}
@app.get("/api/forge/spaces")
async def forge_spaces():
try: ids = await asyncio.to_thread(lambda: sorted(s.id for s in HfApi().list_spaces(author="2Btainment", limit=300)))
except Exception as e: raise HTTPException(502, f"HF API: {str(e)[:120]}")
return {"spaces": ids}
@app.post("/api/forge/build")
async def forge_build(space: str = Form(...), targets: str = Form(...)):
sp = space.strip(); m = re.fullmatch(r"(?:https://huggingface\.co/spaces/)?([\w.-]+)/([\w.-]+)/?", sp) or re.fullmatch(r"https://([\w-]+)-([\w-]+)\.hf\.space/?", sp)
if not m: raise HTTPException(400, "Space als owner/name oder https://huggingface.co/spaces/owner/name")
tg = [t for t in targets.split(",") if t in FORGE_T]
if not tg: raise HTTPException(400, "kein gültiges Ziel")
async with httpx.AsyncClient(timeout=60) as c:
r = await _freq(c, "POST", f"{SPACEFORGE}/api/build", json={"url": f"https://huggingface.co/spaces/{m.group(1)}/{m.group(2)}", "targets": tg, "options": {}})
if r.status_code >= 400: raise HTTPException(502, "SpaceForge ist gerade ausgelastet (HF-Limit) – bitte gleich nochmal" if r.status_code == 429 else f"SpaceForge HTTP {r.status_code}: {r.text[:200]}")
return r.json()
@app.get("/api/forge/build/{bid}")
async def forge_status(bid: str):
if not re.fullmatch(r"[\w-]{1,64}", bid): raise HTTPException(400, "bad id")
async with httpx.AsyncClient(timeout=30) as c:
r = await _freq(c, "GET", f"{SPACEFORGE}/api/build/{bid}"); lg = await _freq(c, "GET", f"{SPACEFORGE}/api/build/{bid}/logs")
if r.status_code >= 400: raise HTTPException(r.status_code if r.status_code == 404 else 502, f"SpaceForge HTTP {r.status_code}")
return {**r.json(), "logs": lg.text[-3000:] if lg.status_code < 400 else ""}
@app.get("/api/forge/build/{bid}/download")
async def forge_download(bid: str, target: str):
if not re.fullmatch(r"[\w-]{1,64}", bid) or target not in FORGE_T: raise HTTPException(400, "bad request")
async with httpx.AsyncClient(timeout=300, follow_redirects=True) as c:
r = await _freq(c, "GET", f"{SPACEFORGE}/api/build/{bid}/download", params={"target": target})
if r.status_code >= 400: raise HTTPException(r.status_code if r.status_code == 404 else 502, f"SpaceForge HTTP {r.status_code}: {r.text[:200]}")
return Response(r.content, media_type=r.headers.get("content-type", "application/octet-stream"), headers={"Content-Disposition": r.headers.get("content-disposition", f'attachment; filename="{target}.zip"')})
@app.middleware("http")
async def nocache(req, call_next):
r = await call_next(req)
if req.url.path in ("/", "/index.html"): r.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
return r
app.include_router(make_router(session))
app.include_router(make_agents_router(session, need, bid, gr_client))
app.mount("/", StaticFiles(directory=ROOT, html=True), name="static")