Spaces:
Sleeping

File size: 2,028 Bytes
0de3c15
 
 
f5a78b9
0de3c15
 
 
 
 
 
f5a78b9
0de3c15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f5a78b9
 
 
 
 
 
 
 
 
 
 
 
 
 
bb17949
f5a78b9
 
 
 
 
 
0de3c15
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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"}