crabbly commited on
Commit
53bccd2
·
1 Parent(s): 664c9a8

Update model syncing

Browse files
Files changed (1) hide show
  1. main.py +60 -0
main.py CHANGED
@@ -514,5 +514,65 @@ async def proxy_adjustments(rest: str, request: Request):
514
  return JSONResponse(status_code=502, content=proxy_error_payload("Proxy Error (Hugging Face)", exc))
515
 
516
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
517
  if __name__ == "__main__":
518
  uvicorn.run(app, host="0.0.0.0", port=int(os.getenv("PORT", 7860)))
 
514
  return JSONResponse(status_code=502, content=proxy_error_payload("Proxy Error (Hugging Face)", exc))
515
 
516
 
517
+ SYNC_PROXY_TIMEOUT_SECONDS = 300
518
+
519
+
520
+ @app.api_route("/proxy_sync{rest:path}", methods=["GET", "POST"])
521
+ async def proxy_sync(rest: str, request: Request):
522
+ """Forward admin sync requests to the dev backend only.
523
+
524
+ Dev remains the controller for dev<->prod sync. The browser never receives
525
+ production sync tokens, and this proxy never routes sync calls to prod based
526
+ on username.
527
+ """
528
+ target_url = f"{DEV_URL}/sync{rest}"
529
+ if request.url.query:
530
+ target_url += f"?{request.url.query}"
531
+ headers = {"Authorization": f"Bearer {HF_TOKEN}"}
532
+ content_type = request.headers.get("content-type", "")
533
+ files = []
534
+ data = {}
535
+ json_body = None
536
+ if request.method == "POST":
537
+ if "multipart/form-data" in content_type:
538
+ form = await request.form()
539
+ for key, value in form.multi_items():
540
+ if hasattr(value, "filename"):
541
+ files.append((key, (value.filename, await value.read(), value.content_type)))
542
+ else:
543
+ data[key] = str(value)
544
+ elif "application/json" in content_type:
545
+ try:
546
+ json_body = await request.json()
547
+ except Exception:
548
+ json_body = None
549
+
550
+ def make_request():
551
+ return request_with_hf_backoff(
552
+ getattr(requests, request.method.lower()),
553
+ target_url,
554
+ max_retries=0,
555
+ headers=headers,
556
+ files=files or None,
557
+ data=data or None,
558
+ json=json_body,
559
+ timeout=SYNC_PROXY_TIMEOUT_SECONDS,
560
+ )
561
+
562
+ try:
563
+ response = await run_in_threadpool(make_request)
564
+ except requests.exceptions.RequestException as exc:
565
+ return JSONResponse(status_code=502, content=proxy_error_payload("Proxy Error (Hugging Face)", exc))
566
+ except Exception as exc:
567
+ return JSONResponse(status_code=500, content={"success": False, "message": f"Proxy Error (Internal): {str(exc)}"})
568
+
569
+ media_type = response.headers.get("content-type", "application/json")
570
+ passthrough_headers = {}
571
+ disposition = response.headers.get("content-disposition")
572
+ if disposition:
573
+ passthrough_headers["content-disposition"] = disposition
574
+ return Response(content=response.content, status_code=response.status_code, media_type=media_type, headers=passthrough_headers)
575
+
576
+
577
  if __name__ == "__main__":
578
  uvicorn.run(app, host="0.0.0.0", port=int(os.getenv("PORT", 7860)))