Spaces:
Sleeping
Sleeping
Download app/main.py from ANL2001/Housing_Price_API: direct link, hf CLI and curl.
- Browser
- Download file 1.73 kB
-
https://huggingface.co/spaces/ANL2001/Housing_Price_API/resolve/main/app/main.py
- Command line
-
hf download hf://spaces/ANL2001/Housing_Price_API/app/main.py
-
curl -L -o main.py https://huggingface.co/spaces/ANL2001/Housing_Price_API/resolve/main/app/main.py
1.73 kB
| from contextlib import asynccontextmanager | |
| from typing import List | |
| from fastapi import FastAPI, Depends, HTTPException | |
| from sqlalchemy.orm import Session | |
| from app import model as ml | |
| from app.database import create_tables, get_db, save_prediction, get_predictions | |
| from app.schemas import HouseFeatures, PredictionResponse, PredictionRecord | |
| async def lifespan(app: FastAPI): | |
| create_tables() | |
| ml.load_artifacts() | |
| yield | |
| app = FastAPI( | |
| title="House Price Prediction API", | |
| description="Predicts house sale prices using a GBR + CatBoost + KernelRidge stacking ensemble trained on the Kaggle Housing Prices dataset (Top 16 / 4,000+ participants).", | |
| version="1.0.0", | |
| lifespan=lifespan, | |
| ) | |
| def health(): | |
| return {"status": "ok", "model": "loaded" if ml._stack is not None else "not loaded"} | |
| def predict(features: HouseFeatures, db: Session = Depends(get_db)): | |
| try: | |
| input_df = features.to_dataframe() | |
| price = ml.predict(input_df) | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=f"Prediction failed: {str(e)}") | |
| record = save_prediction( | |
| db=db, | |
| input_features=features.model_dump(by_alias=True), | |
| predicted_price=price, | |
| ) | |
| return PredictionResponse( | |
| prediction_id=record.id, | |
| predicted_price=round(price, 2), | |
| predicted_price_formatted=f"${price:,.0f}", | |
| ) | |
| def list_predictions(limit: int = 50, db: Session = Depends(get_db)): | |
| return get_predictions(db, limit=limit) | |