Spaces:
Sleeping
Sleeping
Gruhit Patel commited on
Commit ·
7749774
1
Parent(s): f1968d8
Initial Commit
Browse files- .github/workflows/test.yml +25 -0
- .gitignore +2 -0
- models.py +11 -0
- requirement.txt +0 -0
- server.py +38 -0
- test_server.py +28 -0
- xgb_model.pkl +0 -0
.github/workflows/test.yml
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
name: TestBackend
|
| 2 |
+
|
| 3 |
+
on:
|
| 4 |
+
push:
|
| 5 |
+
branches:
|
| 6 |
+
- main
|
| 7 |
+
|
| 8 |
+
jobs:
|
| 9 |
+
test:
|
| 10 |
+
name: Run pytest
|
| 11 |
+
runs-on: ubuntu-latest
|
| 12 |
+
|
| 13 |
+
steps:
|
| 14 |
+
- uses: action/checkout@v2
|
| 15 |
+
|
| 16 |
+
- name: Install python to perform testing
|
| 17 |
+
- uses: actions/setup-python@v2
|
| 18 |
+
with:
|
| 19 |
+
python-version: '3.9'
|
| 20 |
+
|
| 21 |
+
- name: Install Dependencies
|
| 22 |
+
- run: python install -r requirement.txt
|
| 23 |
+
|
| 24 |
+
- name: Run Pytest
|
| 25 |
+
- run: pytest
|
.gitignore
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
bin
|
| 2 |
+
__pycache__
|
models.py
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pydantic import BaseModel
|
| 2 |
+
|
| 3 |
+
class PredictReq(BaseModel):
|
| 4 |
+
medInc: float
|
| 5 |
+
houseAge: float
|
| 6 |
+
avgRooms: float
|
| 7 |
+
avgBdrms: float
|
| 8 |
+
population: float
|
| 9 |
+
avgOccup: float
|
| 10 |
+
latitude: float
|
| 11 |
+
longitude: float
|
requirement.txt
ADDED
|
File without changes
|
server.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from models import PredictReq
|
| 3 |
+
import pickle
|
| 4 |
+
import json
|
| 5 |
+
import numpy as np
|
| 6 |
+
|
| 7 |
+
app = FastAPI()
|
| 8 |
+
|
| 9 |
+
ml_model = None
|
| 10 |
+
# Load the XGBoost model
|
| 11 |
+
with open('xgb_model.pkl', 'rb') as model_file:
|
| 12 |
+
ml_model = pickle.load(model_file)
|
| 13 |
+
|
| 14 |
+
# This is just the normal getter api to check if working of the entire backend
|
| 15 |
+
@app.get("/")
|
| 16 |
+
def foo():
|
| 17 |
+
return {
|
| 18 |
+
"status": "House Price Prediction"
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
@app.post("/predict_house_price")
|
| 22 |
+
def predice_house_price(req: PredictReq):
|
| 23 |
+
inp = np.array([[
|
| 24 |
+
req.medInc,
|
| 25 |
+
req.houseAge,
|
| 26 |
+
req.avgRooms,
|
| 27 |
+
req.avgBdrms,
|
| 28 |
+
req.population,
|
| 29 |
+
req.avgOccup,
|
| 30 |
+
req.latitude,
|
| 31 |
+
req.longitude,
|
| 32 |
+
]])
|
| 33 |
+
|
| 34 |
+
prediction = ml_model.predict(inp)[0]
|
| 35 |
+
|
| 36 |
+
return {
|
| 37 |
+
'price': str(prediction)
|
| 38 |
+
}
|
test_server.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi.testclient import TestClient
|
| 2 |
+
import json
|
| 3 |
+
|
| 4 |
+
from server import app
|
| 5 |
+
|
| 6 |
+
client = TestClient(app)
|
| 7 |
+
|
| 8 |
+
def test_read_app():
|
| 9 |
+
response = client.get("/")
|
| 10 |
+
assert response.status_code == 200
|
| 11 |
+
assert response.json() == {"status": "House Price Prediction"}
|
| 12 |
+
|
| 13 |
+
def test_predict_house_price():
|
| 14 |
+
response = client.post(
|
| 15 |
+
"/predict_house_price",
|
| 16 |
+
json={
|
| 17 |
+
'medInc': 8.3252,
|
| 18 |
+
'houseAge': 41,
|
| 19 |
+
'avgRooms': 6.0,
|
| 20 |
+
'avgBdrms': 1.0,
|
| 21 |
+
'population': 1.0,
|
| 22 |
+
'avgOccup': 2.5,
|
| 23 |
+
'latitude': 37.88,
|
| 24 |
+
'longitude': -122.23,
|
| 25 |
+
}
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
assert response.status_code == 200
|
xgb_model.pkl
ADDED
|
Binary file (455 kB). View file
|
|
|