Saturday, 10 September 2022

Could not access the model deployed on lab server using Flask

I am trying to follow the tutorial to deploy a machine learning model on my lab's server so every one in my lab could give it a try. This tutorial is deploying a Logistic regression model to predict the sales, but I believe it should also work in my case.

The following is the complete code:

  • app.py on my lab server, where model.pkl is a pickled Logistic regression model and index.html is some GUI code.
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=False, host='0.0.0.0')

  • request.py on my local machine
import requests

data = {'rate':5, 'sales_in_first_month':200, 'sales_in_second_month':400}

url = "http://<ip-address>/results"
r = requests.post(
    url,
    json=data
)

print(r.json())

But when I python app.py to let the application running on the lab server (the real IP address is replaced with <ip-address>) and then python request.py on my local machine, it seems that I still could not access the server as there are no responses.

 * Serving Flask app 'app'
 * Debug mode: off
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Running on http://<ip-address>:5000

I do need a username and a password to log into my lab's server. But I am not sure login credential is required here. To my understanding, anyone with the IP address could interact with the model.

Did I do something wrong here? Any input is appreciated!



from Could not access the model deployed on lab server using Flask

No comments:

Post a Comment