| |
| """ |
| FastAPI server entry point for Medical Diagnosis AI |
| |
| Local Development: |
| python server.py |
| Server will start on http://localhost:8000 |
| API docs: http://localhost:8000/docs |
| |
| Hugging Face Spaces / Production: |
| python server.py |
| Server will start on http://0.0.0.0:7860 |
| Serves both API and static React frontend |
| """ |
|
|
| import uvicorn |
| import logging |
| import os |
| from pathlib import Path |
| from fastapi.staticfiles import StaticFiles |
|
|
| |
| logging.basicConfig( |
| level=logging.INFO, |
| format='%(asctime)s - %(name)s - %(levelname)s - %(message)s' |
| ) |
| logger = logging.getLogger(__name__) |
|
|
|
|
| def get_server_config(): |
| """Determine server configuration based on environment""" |
| |
| in_space = os.getenv("SPACE_ID") is not None |
|
|
| host = "0.0.0.0" |
| port = 7860 if in_space else 8000 |
| reload = not in_space |
|
|
| return host, port, reload, in_space |
|
|
|
|
| if __name__ == "__main__": |
| host, port, reload, in_space = get_server_config() |
|
|
| |
| from app.api import app |
|
|
| |
| frontend_dist = Path(__file__).parent / "frontend" / "dist" |
|
|
| if frontend_dist.exists(): |
| logger.info(f"π Mounting static files from: {frontend_dist}") |
| |
| |
| app.mount("/", StaticFiles(directory=frontend_dist, html=True), name="static") |
| else: |
| logger.warning(f"β οΈ Frontend dist directory not found: {frontend_dist}") |
| logger.warning(" Run 'cd frontend && npm run build' to build the frontend") |
|
|
| |
| environment = "π Hugging Face Spaces" if in_space else "π» Local Development" |
| logger.info(f"Starting Medical Diagnosis AI Server ({environment})") |
| logger.info(f"π Server: http://{host}:{port}") |
| logger.info(f"π Frontend: http://{host}:{port}") |
| logger.info(f"π API Base: http://{host}:{port}/api") |
| logger.info(f"π API Docs: http://{host}:{port}/docs") |
|
|
| uvicorn.run( |
| "app.api:app", |
| host=host, |
| port=port, |
| reload=reload, |
| log_level="info" |
| ) |
|
|