-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
58 lines (44 loc) · 1.85 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import pickle
import pandas as pd
from asgiref.wsgi import WsgiToAsgi
from flask import Flask, request, jsonify, render_template, send_from_directory
from UvicornWrapper import UvicornWrapper
app = Flask(__name__)
asgi_app = WsgiToAsgi(app)
# Load the preprocessor
with open('Models/data_preprocessor.pkl', 'rb') as preprocessor_file:
preprocessor = pickle.load(preprocessor_file)
model = pickle.load(open('Models/model.pkl', 'rb'))
# Create the home page
@app.route('/')
def home():
return render_template('home.html')
@app.route('/images/<filename>')
def get_image(filename):
return send_from_directory('static/images', filename)
# Create the api end point for prediction
@app.route('/predict_api', methods=['POST'])
def predict_api():
data = request.json
# Convert JSON to DataFrame
data_df = pd.DataFrame([data])
transformed_data = preprocessor.transform(data_df)
output = model.predict(transformed_data.squeeze().reshape(1, -1))[0]
prediction = ""
output_pro = model.predict_proba(transformed_data.squeeze().reshape(1, -1))[0]
prediction = f"The person has stage {output} liver cirrhosis with {output_pro[1]*100:.2f}% of chance"
return jsonify(prediction)
@app.route('/predict', methods=['POST'])
def predict():
data = dict(request.form)
data_df = pd.DataFrame([data])
transformed_data = preprocessor.transform(data_df)
output = model.predict(transformed_data.squeeze().reshape(1, -1))[0]
prediction = ""
output_pro = model.predict_proba(transformed_data.squeeze().reshape(1, -1))[0]
prediction = f"The person has stage {output} liver cirrhosis with {output_pro[1]*100:.2f}% of chance"
return render_template("home.html", prediction_text=prediction)
if __name__ == '__main__':
# Only for local development. To run the app from IDE.
uvicorn = UvicornWrapper(asgi_app)
uvicorn.run()