update it to fastapi
Browse files- app.py +22 -24
- requirements.txt +3 -1
app.py
CHANGED
@@ -1,35 +1,33 @@
|
|
1 |
import pickle
|
2 |
-
from
|
3 |
-
|
|
|
4 |
|
5 |
-
app=
|
6 |
-
|
|
|
|
|
7 |
model = pickle.load(open('regression_model.pkl', 'rb'))
|
8 |
scaling = pickle.load(open('scaling.pkl', 'rb'))
|
9 |
|
10 |
|
11 |
-
@app.
|
12 |
-
def home():
|
13 |
-
return
|
14 |
|
15 |
|
16 |
-
@app.
|
17 |
-
def
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
|
|
24 |
|
25 |
-
@app.route('/predict',methods=['POST'])
|
26 |
-
def predict():
|
27 |
-
data=[float(x) for x in request.form.values()]
|
28 |
-
final_input=scaling.transform(np.array(data).reshape(1,-1))
|
29 |
-
print(final_input)
|
30 |
-
output=model.predict(final_input)[0]
|
31 |
-
return render_template("home.html",prediction_text="The House price prediction is {}".format(output))
|
32 |
|
|
|
|
|
33 |
|
34 |
-
|
35 |
-
app.run(debug=True)
|
|
|
1 |
import pickle
|
2 |
+
from fastapi import FastAPI, Form, Request
|
3 |
+
from fastapi.templating import Jinja2Templates
|
4 |
+
import numpy as np
|
5 |
|
6 |
+
app = FastAPI()
|
7 |
+
templates = Jinja2Templates(directory="templates")
|
8 |
+
|
9 |
+
# Load the model and scaling
|
10 |
model = pickle.load(open('regression_model.pkl', 'rb'))
|
11 |
scaling = pickle.load(open('scaling.pkl', 'rb'))
|
12 |
|
13 |
|
14 |
+
@app.get('/')
|
15 |
+
def home(request: Request):
|
16 |
+
return templates.TemplateResponse("home.html", {"request": request, "prediction_text": ""})
|
17 |
|
18 |
|
19 |
+
@app.post('/predict')
|
20 |
+
def predict(request: Request, CRIM: float = Form(...), ZN: float = Form(...), INDUS: float = Form(...),
|
21 |
+
CHAS: float = Form(...), NOX: float = Form(...), RM: float = Form(...), Age: float = Form(...),
|
22 |
+
DIS: float = Form(...), RAD: float = Form(...), TAX: float = Form(...), PTRATIO: float = Form(...),
|
23 |
+
B: float = Form(...), LSTAT: float = Form(...)):
|
24 |
+
data = [CRIM, ZN, INDUS, CHAS, NOX, RM, Age, DIS, RAD, TAX, PTRATIO, B, LSTAT]
|
25 |
+
final_input = scaling.transform(np.array(data).reshape(1, -1))
|
26 |
+
output = model.predict(final_input)[0]
|
27 |
+
return templates.TemplateResponse("home.html", {"request": request, "prediction_text": f"The House price prediction is {output:.2f}"})
|
28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
+
if __name__ == "__main__":
|
31 |
+
import uvicorn
|
32 |
|
33 |
+
uvicorn.run(app, host="127.0.0.1", port=8000)
|
|
requirements.txt
CHANGED
@@ -1,4 +1,6 @@
|
|
1 |
-
|
|
|
|
|
2 |
scikit-learn
|
3 |
pandas
|
4 |
numpy
|
|
|
1 |
+
fastapi
|
2 |
+
uvicorn
|
3 |
+
python-multipart
|
4 |
scikit-learn
|
5 |
pandas
|
6 |
numpy
|