mahedi420 commited on
Commit
6001492
1 Parent(s): 832ad55

update it to fastapi

Browse files
Files changed (2) hide show
  1. app.py +22 -24
  2. requirements.txt +3 -1
app.py CHANGED
@@ -1,35 +1,33 @@
1
  import pickle
2
- from flask import Flask , request, jsonify, redirect, render_template
3
- import pandas as pd , numpy as np
 
4
 
5
- app=Flask(__name__)
6
- ## load the model
 
 
7
  model = pickle.load(open('regression_model.pkl', 'rb'))
8
  scaling = pickle.load(open('scaling.pkl', 'rb'))
9
 
10
 
11
- @app.route('/')
12
- def home():
13
- return render_template('home.html')
14
 
15
 
16
- @app.route('/predict_api', methods = ['POST'])
17
- def predict_api():
18
- data = request.json['data']
19
- data = np.array(list(data.values())).reshape(1,-1)
20
- # data = list(np.array(data.values()))
21
- new_data = scaling.transform(data)
22
- output = model.predict(new_data)
23
- return jsonify(output[0])
 
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
- if __name__ == '__main__':
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
- Flask
 
 
2
  scikit-learn
3
  pandas
4
  numpy
 
1
+ fastapi
2
+ uvicorn
3
+ python-multipart
4
  scikit-learn
5
  pandas
6
  numpy