Deploy a machine learning model using Flask
I developed this web app for a client who needs to estimate how many new release books will sell in the first 12 months. I used historical sales data for books published in Argentina across several years and external data regarding the specifications of books and authors. The trained model was an LGBMRegressor classifier.
Steps:
- Once you select the machine learning model, use Python’s pickle module to export a file named model.pkl;
- Follow the instructions on Render;
- Create a templates folder and design an html form where the user will input all the attribute values and the data will be given the model, based on the training given to the model;
- Save the model.pkl in the directory;
- Create an html result file and add to the templates folder;
- Edit the app.py: import the libraries, then using app=Flask(name) create an instance of flask. @app.route(‘/’) is used to tell flask what url should trigger the function index() and in the function index we use render_template(‘form.html’) to display the script form.html in the browser.
- After the form is submitted, the form values are stored in variable to_predict_list in the form of dictionary. Convert it into a list of the dictionary’s values and pass it as an argument to ValuePredictor() function. In this function, we load the model.pkl file and predict the new values and return the result. This result/prediction is then passed as an argument to the template engine with the html page to be displayed. Remember to import every libraries you use in the model.
Source: https://www.geeksforgeeks.org/deploy-machine-learning-model-using-flask/
The complete code can be viewed at here