Spaces:
Sleeping

ml-api / app.py
Eric Dyke
Removed unnecessary links
bb17949
Raw History Blame Contribute Delete
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
@app.get("/favicon.ico", include_in_schema=False)
async def favicon():
return FileResponse(BASE_DIR / "static" / "favicon.ico")
# Landing page at /
@app.get("/", response_class=HTMLResponse, include_in_schema=False)
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>
"""
@app.get("/health")
def health():
return {"status": "ok"}