Spaces:
Sleeping
Sleeping
Download app.py from HighFive52/ml-api: direct link, hf CLI and curl.
- Browser
- Download file 2.03 kB
-
https://huggingface.co/spaces/HighFive52/ml-api/resolve/main/app.py
- Command line
-
hf download hf://spaces/HighFive52/ml-api/app.py
-
curl -L -o app.py https://huggingface.co/spaces/HighFive52/ml-api/resolve/main/app.py
2.03 kB
| from fastapi import FastAPI | |
| from pathlib import Path | |
| from fastapi.staticfiles import StaticFiles | |
| from fastapi.responses import FileResponse, HTMLResponse | |
| from routers import spaceship, digits | |
| from ml.common.model_loader import load_spaceship_models, load_digit_model | |
| import joblib | |
| app = FastAPI() | |
| BASE_DIR = Path(__file__).resolve().parent | |
| print(f"Base directory: {BASE_DIR}") | |
| models_dir = BASE_DIR / "models" | |
| print(f"Models directory: {models_dir}") | |
| joblib.load("models/spaceship/xgb_tuned.pkl") | |
| # Load models | |
| tuned_models = load_spaceship_models(models_dir) | |
| digit_model = load_digit_model(models_dir) | |
| # Inject into routers | |
| spaceship.set_models(tuned_models) | |
| digits.set_digit_model(digit_model) | |
| # Register routers | |
| app.include_router(spaceship.router) | |
| app.include_router(digits.router) | |
| # Mount static directory for favicon | |
| app.mount("/static", StaticFiles(directory=BASE_DIR / "static"), name="static") | |
| # Serve favicon.ico at root | |
| async def favicon(): | |
| return FileResponse(BASE_DIR / "static" / "favicon.ico") | |
| # Landing page at / | |
| async def root(): | |
| return """ | |
| <html> | |
| <head> | |
| <title>HighFive52 ML API</title> | |
| </head> | |
| <body style='font-family: Arial, sans-serif; max-width: 700px; margin: 40px auto;'> | |
| <h1>Welcome to the HighFive52 ML API</h1> | |
| <p>This API provides machine learning endpoints for digit recognition and spaceship prediction.</p> | |
| <h2>How to Use</h2> | |
| <ul> | |
| <li>For interactive API documentation and testing, visit: <a href=\"https://highfive52-ml-api.hf.space/docs\">/docs</a></li> | |
| <li>To use the API programmatically, send requests to: https://highfive52-ml-api.hf.space</li> | |
| </ul> | |
| </body> | |
| </html> | |
| """ | |
| def health(): | |
| return {"status": "ok"} | |