import os import sys import shutil import logging from pathlib import Path # Set up logging logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) # Add the backend directory to the Python path sys.path.append(os.path.abspath("backend")) # Create necessary directories if they don't exist os.makedirs("./temp_audio", exist_ok=True) os.makedirs("./temp", exist_ok=True) # Set environment variables for MongoDB connection timeout os.environ["MONGODB_CONNECT_TIMEOUT_MS"] = "5000" # 5 seconds timeout os.environ["MONGODB_SERVER_SELECTION_TIMEOUT_MS"] = "5000" # 5 seconds timeout try: # Set up CORS headers to allow frontend to access backend from backend.app.main import app from fastapi.middleware.cors import CORSMiddleware app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) # Serve static files from the frontend build directory from fastapi.staticfiles import StaticFiles # Check if static directory exists with index.html static_path = Path("./static") if static_path.exists() and (static_path / "index.html").exists(): app.mount("/", StaticFiles(directory="./static", html=True), name="static") # For Hugging Face Spaces - expose the app if __name__ == "__main__": import uvicorn port = int(os.environ.get("PORT", 7860)) host = os.environ.get("HOST", "0.0.0.0") logger.info(f"Starting server on {host}:{port}") uvicorn.run(app, host=host, port=port) except Exception as e: # If there's an error, create a minimal FastAPI app to show the error from fastapi import FastAPI from fastapi.responses import HTMLResponse from fastapi.staticfiles import StaticFiles error_app = FastAPI() @error_app.get("/", response_class=HTMLResponse) async def root(): return f"""
An error occurred while starting the application:
{str(e)}
Please check your environment variables and configuration.
MongoDB URL format should be: mongodb+srv://username:password@cluster.mongodb.net/...
API keys should be properly formatted and valid.