I have created a ML model using logistic regression. I am trying to deploy it on local machine using Flask. I have written html code to get the input values from the user through form. I am accepting the input from user and feeding it to ML model whose output will be returned by the flask's predict function and it is supposed to be printed on screen as pop-up box. In a nutshell, the process is as follows :
- User submits html form
- The data is passed to flask's predict function which contains trained ML model weights
- Flask function returns the output (it is a string - a statement stating whether patient is positive or not)
- This string returned by flask's predict function needs to be printed on the screen as a pop-up box on the same page where we had collected data from the user using html form. Please note that pop-up box pops up as soon as 'submit' button is pressed so all this will occur in a moment. If in case model takes more time to predict, it pop-up box should display 'loading' followed by the output.
I have written the code for predict function and the code for pop up box using online resources and code-snippets. However, I don't know how to fetch the output of flask and provide it to html pop-up box. The code of predict function is as follows :
@app.route('/index')
def index():
return flask.render_template('index.html')
@app.route('/predict', methods=['POST'])
def predict():
input_values = request.form.to_dict()
X_datapoint = list(input_values.values())
encoder = pickle.load(open('OneHot_Encoder.sav', 'rb'))
X_datapoint = encoder.transform(X_datapoint.values.reshape(1,-1))
model = pickle.load(open('finalized_model.sav', 'rb'))
y = model.predict(X_datapoint)[0]
if y == 0 :
y_proba = round(model.predict_proba(X_datapoint)[0][0]*100)
final_output = "We are {} sure that the patient will NOT be readmitted in 30 days".format(y_proba)
else :
y_proba = round(model.predict_proba(X_datapoint)[0][1]*100)
final_output = "We are {} sure that the patient will be readmitted in 30 days".format(y_proba)
return final_output
if __name__ == '__main__':
app.debug = True
app.run(host='0.0.0.0', port=8080)
Here, 'OneHot_Encoder.sav' and 'finalized_model.sav' are the files that contain trained One Hot encoder model weights and trained logistic regression model weights respectively.
The code for pop-up box is as follows :
<div id="popup1" class="overlay">
<div class="popup">
<h2>Will the patient be readmitted in 30 days ?</h2>
<a class="close" href="#">×</a>
<div class="content">
<script>Not sure what to write here, I want to print the output returned by flask here </script>
</div>
</div>
</div>
My question is, how can I print the value returned by predict function in the pop-up box which pops up as soon as the form's 'submit' button is pressed. I am not sure, if this is the right way to do it.