Download src/app/main.py from servisgas/pricing: direct link, hf CLI and curl.
- Browser
- Download file 1.5 kB
-
https://huggingface.co/spaces/servisgas/pricing/resolve/main/src/app/main.py
- Command line
-
hf download hf://spaces/servisgas/pricing/src/app/main.py
-
curl -L -o main.py https://huggingface.co/spaces/servisgas/pricing/resolve/main/src/app/main.py
1.5 kB
| from fastapi import FastAPI, HTTPException | |
| from pydantic import BaseModel, Field | |
| from handler import FastApiHandler | |
| app = FastAPI(title="TrueNest Rent Prediction API") | |
| handler = None | |
| # ---------- Request schema with example ---------- | |
| class PredictRequest(BaseModel): | |
| model_params: dict = Field( | |
| ..., | |
| json_schema_extra={ | |
| "example": { | |
| "bathrooms": 1, | |
| "bedrooms": 2, | |
| "propertyType": "Flat", | |
| "deposit": False, | |
| "letType": "Long term", | |
| "furnishType": "Furnished", | |
| "latitude": 51.49199, | |
| "longitude": -0.17134 | |
| } | |
| }, | |
| ) | |
| # ---------- Startup: load model once ---------- | |
| def load_model_once(): | |
| global handler | |
| handler = FastApiHandler() | |
| print("✅ MLflow model loaded at startup") | |
| # ---------- Routes ---------- | |
| def root(): | |
| return {"message": "🏡 Rent Prediction API is running", "run_id": handler.run_id} | |
| def predict(req: PredictRequest): | |
| result = handler.handle(req.dict()) | |
| if "error" in result: | |
| raise HTTPException(status_code=400, detail=result["error"]) | |
| return result | |
| def explain(req: PredictRequest): | |
| try: | |
| explanation = handler.explain_prediction(req.model_params) | |
| return explanation | |
| except Exception as e: | |
| raise HTTPException(status_code=400, detail=str(e)) | |