This repository implements a small FastAPI web application that serves a single-page client and a /predict endpoint backed by a serialized scikit-learn pipeline. The app demonstrates a complete prediction flow: user inputs → model prediction → percentile against dataset.
flowchart LR
A[User Browser] -->|GET /| B(FastAPI)
B --> C[templates/index.html]
B -->|POST /predict| D{Prediction API}
D --> E[Load pipeline: joblib]
D --> F[Compute percentile using Housing.csv]
E --> G[scikit-learn pipeline]
style A fill:#f9f,stroke:#333,stroke-width:1px
style B fill:#cff,stroke:#333,stroke-width:1px
style D fill:#cfc,stroke:#333,stroke-width:1px
This simple diagram shows the request flow: the browser serves a static client, which posts JSON to /predict. The API loads a serialized pipeline and the CSV dataset to compute a percentile for context.
Use the API directly from curl or any HTTP client.
curl example:
curl -s -X POST http://127.0.0.1:8000/predict \
-H "Content-Type: application/json" \
-d '{"area":2300,"bedrooms":3,"bathrooms":2,"stories":1,"mainroad":1,"guestroom":0,"basement":0,"hotwaterheating":0,"airconditioning":1,"parking":2,"prefarea":1,"furnishingstatus":0}'Python example using requests:
import requests
payload = {
"area":2300,
"bedrooms":3,
"bathrooms":2,
"stories":1,
"mainroad":1,
"guestroom":0,
"basement":0,
"hotwaterheating":0,
"airconditioning":1,
"parking":2,
"prefarea":1,
"furnishingstatus":0
}
resp = requests.post('http://127.0.0.1:8000/predict', json=payload)
print(resp.json())Housing.csvmust contain thepricecolumn used to compute percentiles.main.pyexpectshouse_price_pipeline.pklat the repo root. The object should support.predict(X).- The Pydantic
Houseschema defines the input fields and types.
Model training checklist (recommended):
- Split dataset (train / val / test).
- Build a scikit-learn pipeline with necessary preprocessing (scaling, one-hot or ordinal encoding for categorical fields).
- Train, evaluate (RMSE, R²), and persist the pipeline with
joblib.dump(pipeline, 'house_price_pipeline.pkl').
- Create and activate a venv, then install deps:
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt-
Ensure
house_price_pipeline.pklis present. -
Start the app:
uvicorn main:app --reload --port 8000Open http://127.0.0.1:8000/ to use the UI.