Spaces:
Runtime error
Runtime error
Download app.py from hello-yaaash25/zenith: direct link, hf CLI and curl.
- Browser
- Download file 2.65 kB
-
https://huggingface.co/spaces/hello-yaaash25/zenith/resolve/main/app.py
- Command line
-
hf download hf://spaces/hello-yaaash25/zenith/app.py
-
curl -L -o app.py https://huggingface.co/spaces/hello-yaaash25/zenith/resolve/main/app.py
2.65 kB
| import os | |
| import subprocess | |
| import threading | |
| import httpx | |
| from fastapi import FastAPI, Request | |
| from fastapi.responses import JSONResponse, HTMLResponse, StreamingResponse | |
| from starlette.middleware.cors import CORSMiddleware | |
| import uvicorn | |
| from fastapi.staticfiles import StaticFiles | |
| import socketio | |
| import asyncio | |
| # Create a Socket.IO server | |
| sio = socketio.AsyncServer(async_mode='asgi', cors_allowed_origins='*') | |
| socket_app = socketio.ASGIApp(sio) | |
| # Create FastAPI app | |
| app = FastAPI() | |
| # Mount Socket.IO at /ws path | |
| app.mount("/ws", socket_app) | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| # Try to mount static files | |
| try: | |
| app.mount("/.chainlit/static", StaticFiles(directory="/app/.chainlit/static"), name="static") | |
| except: | |
| pass | |
| async def health(): | |
| return {"status": "ok"} | |
| # Proxy all HTTP requests except /ws | |
| async def proxy_chainlit(request: Request, path: str = ""): | |
| # Skip WebSocket routes as they're handled by Socket.IO | |
| if path.startswith("ws"): | |
| return {"error": "WebSocket requests should use the Socket.IO implementation"} | |
| # The target Chainlit URL | |
| target_url = f"http://localhost:8000/{path}" | |
| # Include query parameters | |
| if request.query_params: | |
| target_url += f"?{request.query_params}" | |
| try: | |
| # Forward the request to Chainlit | |
| async with httpx.AsyncClient() as client: | |
| response = await client.request( | |
| method=request.method, | |
| url=target_url, | |
| headers={key: value for key, value in request.headers.items() if key.lower() not in ["host"]}, | |
| content=await request.body(), | |
| follow_redirects=True, | |
| timeout=60.0 | |
| ) | |
| # Return the Chainlit response | |
| return StreamingResponse( | |
| response.aiter_bytes(), | |
| status_code=response.status_code, | |
| headers=dict(response.headers), | |
| ) | |
| except Exception as e: | |
| return HTMLResponse( | |
| f"<h1>Loading Zenith...</h1><p>Error: {str(e)}</p>", | |
| status_code=200 | |
| ) | |
| # Run Chainlit in a separate thread | |
| def run_chainlit(): | |
| subprocess.run(["chainlit", "run", "main.py", "--host", "0.0.0.0", "--port", "8000"]) | |
| # Start Chainlit | |
| threading.Thread(target=run_chainlit, daemon=True).start() | |
| if __name__ == "__main__": | |
| uvicorn.run(app, host="0.0.0.0", port=7860) |