mercredi 21 octobre 2020

How to deploy a machine learning model to a webpage

I have deployed a little example of a machine learning model following a tutorial of flask. The idea is making a webpage out of it, in order to show the predictions online. The code I used was the following:


import numpy as np
from flask import Flask, request, jsonify, render_template
import pickle

app = Flask(__name__)
model = pickle.load(open('model.pkl', 'rb'))

@app.route('/')
def home():
    return render_template('index.html')

@app.route('/predict',methods=['POST'])
def predict():

    int_features = [int(x) for x in request.form.values()]
    final_features = [np.array(int_features)]
    prediction = model.predict(final_features)

    output = round(prediction[0], 2)

    return render_template('index.html', prediction_text='Sales should be $ {}'.format(output))

@app.route('/results',methods=['POST'])
def results():

    data = request.get_json(force=True)
    prediction = model.predict([np.array(list(data.values()))])

    output = prediction[0]
    return jsonify(output)

if __name__ == "__main__":
    app.run(debug=True)


import requests

url = 'http://localhost:5000/results'
r = requests.post(url,json={'rate':5, 'sales_in_first_month':200, 'sales_in_second_month':400})

print(r.json())

as you can see I loaded the machine learning model through pickle and used it seems to work well when I run the API typing python app.py:

enter image description here

I did not show the html and css code because it is irrelevant for this case. My questions would be:

  1. When I access http://127.0.0.1:5000/ from my computer it works. When I try to access from another device there seems to be no access. Why is that?

  2. My idea is to create a webpage with the model working forever. Obviously I cannot be running "python app.py" all the time locally from my computer. So I guess that I have to upload it to a server and make it run. I guess the easiest way would be uploading it to AWS but I don't know exactly how to proceed... because it is a dynamic webpage with a machine learning model running below... does it make sense to make a docker? do you have any suggestion?

Thanks in advance




Aucun commentaire:

Enregistrer un commentaire