Spaces:
Sleeping
Sleeping
Download server/studio_proxy.py from budijuarto/kriyatrack: direct link, hf CLI and curl.
- Browser
- Download file 1.67 kB
-
https://huggingface.co/spaces/budijuarto/kriyatrack/resolve/main/server/studio_proxy.py
- Command line
-
hf download hf://spaces/budijuarto/kriyatrack/server/studio_proxy.py
-
curl -L -o studio_proxy.py https://huggingface.co/spaces/budijuarto/kriyatrack/resolve/main/server/studio_proxy.py
1.67 kB
| """Explicit custom routes for Gradio Server mode. Node owns application paths.""" | |
| import httpx | |
| from fastapi import Request | |
| from starlette.responses import JSONResponse, Response | |
| def attach_studio(server, port): | |
| async def forward(request: Request): | |
| try: | |
| headers = {k: v for k, v in request.headers.items() if k not in {"connection", "transfer-encoding", "accept-encoding"}} | |
| url = f"http://127.0.0.1:{port}{request.url.path}" | |
| if request.url.query: | |
| url += "?" + request.url.query | |
| async with httpx.AsyncClient(timeout=25, trust_env=False) as client: | |
| upstream = await client.request(request.method, url, content=await request.body(), headers=headers) | |
| response = Response(content=upstream.content, status_code=upstream.status_code) | |
| for k, value in upstream.headers.multi_items(): | |
| if k.lower() not in {"transfer-encoding", "content-encoding", "content-length", "connection", "date", "server", "keep-alive"}: | |
| response.headers.append(k, value) | |
| return response | |
| except httpx.RequestError: | |
| return JSONResponse({"detail":"Studio belum siap. Coba lagi sebentar."}, status_code=503) | |
| for path in ["/", "/index.html", "/health", "/images/{path:path}", "/assets/{path:path}", "/models/{path:path}", "/data/{path:path}", "/benchmark/{path:path}", "/logos/{path:path}", "/licenses/{path:path}"]: | |
| server.add_api_route(path, forward, methods=["GET", "HEAD"], include_in_schema=False) | |
| server.add_api_route("/api/{path:path}", forward, methods=["POST", "GET", "HEAD"], include_in_schema=False) | |