2Btainment commited on
Commit
b831af6
·
verified ·
1 Parent(s): ea04223

P9.2: Forge-Proxy mit Space-Token + 429-Retry

Browse files
Files changed (1) hide show
  1. server/main.py +12 -4
server/main.py CHANGED
@@ -499,6 +499,14 @@ async def repo_status(sid: str):
499
  return {**r.json(), "logs": lg.text[-3000:] if lg.status_code < 400 else "", "base": REPOSPACE}
500
 
501
  SPACEFORGE = os.getenv("SPACEFORGE_URL", "https://2btainment-spaceforge.hf.space").rstrip("/")
 
 
 
 
 
 
 
 
502
  FORGE_T = {"html_widget", "pwa", "android", "linux_appimage", "arch_pkgbuild", "mcp"}
503
  @app.get("/api/forge/spaces")
504
  async def forge_spaces():
@@ -513,15 +521,15 @@ async def forge_build(space: str = Form(...), targets: str = Form(...)):
513
  tg = [t for t in targets.split(",") if t in FORGE_T]
514
  if not tg: raise HTTPException(400, "kein gültiges Ziel")
515
  async with httpx.AsyncClient(timeout=60) as c:
516
- r = await c.post(f"{SPACEFORGE}/api/build", json={"url": f"https://huggingface.co/spaces/{m.group(1)}/{m.group(2)}", "targets": tg, "options": {}})
517
- if r.status_code >= 400: raise HTTPException(502, f"SpaceForge HTTP {r.status_code}: {r.text[:200]}")
518
  return r.json()
519
 
520
  @app.get("/api/forge/build/{bid}")
521
  async def forge_status(bid: str):
522
  if not re.fullmatch(r"[\w-]{1,64}", bid): raise HTTPException(400, "bad id")
523
  async with httpx.AsyncClient(timeout=30) as c:
524
- r = await c.get(f"{SPACEFORGE}/api/build/{bid}"); lg = await c.get(f"{SPACEFORGE}/api/build/{bid}/logs")
525
  if r.status_code >= 400: raise HTTPException(r.status_code if r.status_code == 404 else 502, f"SpaceForge HTTP {r.status_code}")
526
  return {**r.json(), "logs": lg.text[-3000:] if lg.status_code < 400 else ""}
527
 
@@ -529,7 +537,7 @@ async def forge_status(bid: str):
529
  async def forge_download(bid: str, target: str):
530
  if not re.fullmatch(r"[\w-]{1,64}", bid) or target not in FORGE_T: raise HTTPException(400, "bad request")
531
  async with httpx.AsyncClient(timeout=300, follow_redirects=True) as c:
532
- r = await c.get(f"{SPACEFORGE}/api/build/{bid}/download", params={"target": target})
533
  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]}")
534
  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"')})
535
 
 
499
  return {**r.json(), "logs": lg.text[-3000:] if lg.status_code < 400 else "", "base": REPOSPACE}
500
 
501
  SPACEFORGE = os.getenv("SPACEFORGE_URL", "https://2btainment-spaceforge.hf.space").rstrip("/")
502
+ def _fh(): # Space-Token (falls gesetzt) gegen anonymes HF-Edge-Rate-Limit (429) auf *.hf.space
503
+ t = os.getenv("HF_TOKEN"); return {"Authorization": f"Bearer {t}"} if t else {}
504
+ async def _freq(c, m, url, **kw):
505
+ for i in range(3):
506
+ r = await c.request(m, url, headers=_fh(), **kw)
507
+ if r.status_code != 429: return r
508
+ await asyncio.sleep(2 + 3 * i)
509
+ return r
510
  FORGE_T = {"html_widget", "pwa", "android", "linux_appimage", "arch_pkgbuild", "mcp"}
511
  @app.get("/api/forge/spaces")
512
  async def forge_spaces():
 
521
  tg = [t for t in targets.split(",") if t in FORGE_T]
522
  if not tg: raise HTTPException(400, "kein gültiges Ziel")
523
  async with httpx.AsyncClient(timeout=60) as c:
524
+ 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": {}})
525
+ 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]}")
526
  return r.json()
527
 
528
  @app.get("/api/forge/build/{bid}")
529
  async def forge_status(bid: str):
530
  if not re.fullmatch(r"[\w-]{1,64}", bid): raise HTTPException(400, "bad id")
531
  async with httpx.AsyncClient(timeout=30) as c:
532
+ r = await _freq(c, "GET", f"{SPACEFORGE}/api/build/{bid}"); lg = await _freq(c, "GET", f"{SPACEFORGE}/api/build/{bid}/logs")
533
  if r.status_code >= 400: raise HTTPException(r.status_code if r.status_code == 404 else 502, f"SpaceForge HTTP {r.status_code}")
534
  return {**r.json(), "logs": lg.text[-3000:] if lg.status_code < 400 else ""}
535
 
 
537
  async def forge_download(bid: str, target: str):
538
  if not re.fullmatch(r"[\w-]{1,64}", bid) or target not in FORGE_T: raise HTTPException(400, "bad request")
539
  async with httpx.AsyncClient(timeout=300, follow_redirects=True) as c:
540
+ r = await _freq(c, "GET", f"{SPACEFORGE}/api/build/{bid}/download", params={"target": target})
541
  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]}")
542
  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"')})
543