Team Ai
Apppublic

Samra09/Physical_AI_Humanoid_Robotics

sourceHugging Faceupdated 10mo agoView on Hugging Face
0likes
deploy.py62 linesDownload Raw Back to root
1import os2import sys3import uvicorn4from fastapi.staticfiles import StaticFiles5from fastapi.responses import FileResponse6from fastapi import Request7 8# Add the current directory to python path to ensure imports work9# We need to add the 'backend' directory to sys.path so that 'src' imports in main.py work10backend_dir = os.path.dirname(os.path.abspath(__file__))11sys.path.append(backend_dir)12# Also add root for good measure if needed, but backend_dir is critical for 'src'13sys.path.append(os.path.join(backend_dir, '..'))14 15# Import the main app from backend.main16# We assume backend/main.py initializes 'app'17from backend.main import app18from src.utils.logger import app_logger19 20# Define paths21BASE_DIR = os.path.dirname(os.path.abspath(__file__))22# The static folder is at the root level in the Docker container (./static)23# But since we are running from backend/, and BASE_DIR is .../backend24# We need to go up one level to find 'static' which we copied to the root in Dockerfile25STATIC_DIR = os.path.join(BASE_DIR, '..', 'static')26 27app_logger.info(f"Setting up static file serving from: {STATIC_DIR}")28 29# Check if static directory exists30if os.path.exists(STATIC_DIR):31    # Mount static files32    # We mount it to root "/" to serve index.html and assets33    app.mount("/", StaticFiles(directory=STATIC_DIR, html=True), name="static")34 35    # Catch-all route for SPA client-side routing (redirect 404s to index.html)36    # Note: Because we mounted StaticFiles at root, it handles valid files.37    # We need an exception handler or a catch-all for non-existant paths to return index.html38    39    # However, FastAPI's StaticFiles takes precedence. 40    # To support clean URLs for Docusaurus, we usually rely on the build structure.41    # Docusaurus builds 'path/index.html' for '/path'.42    # So StaticFiles with html=True usually handles it.43    44    # But for truly dynamic routes or deep links that might not map to a file:45    @app.exception_handler(404)46    async def not_found_exception_handler(request: Request, exc):47        # Only return index.html for non-API routes48        if not request.url.path.startswith("/api"):49            index_path = os.path.join(STATIC_DIR, "index.html")50            if os.path.exists(index_path):51                return FileResponse(index_path)52        return {"detail": "Not Found"}, 40453        54else:55    app_logger.warning(f"Static directory {STATIC_DIR} not found. Frontend will not be served.")56 57if __name__ == "__main__":58    # Hugging Face Spaces sets PORT env var59    port = int(os.environ.get("PORT", 7860))60    app_logger.info(f"Starting Deployment Server on port {port}")61    uvicorn.run(app, host="0.0.0.0", port=port)62