Deploying ML with Flask Guide

Aug 5, 2024

Deploying a Machine Learning Model Using Flask

Overview

  • Discussion on deploying a machine learning model using Flask.
  • Apologies for the delay in the presentation.
  • Focus on simplicity in deployment techniques.

Required Components

  1. Model Building File (Python file)

    • Responsible for creating the model, feature engineering, and data pre-processing.
    • Example model: Linear regression.
  2. Index.html File

    • Acts as the front-end web app for user input.
    • Interacts with the model's API to produce output.
  3. Additional Python File (app.py)

    • Contains Flask code to create APIs.
    • APIs:
      • /reading (example)
      • /predict (to get predictions based on user input)

Dataset

  • Dataset used: hiring.csv
  • Fields: Experience, Test Score, Interview Score, Salary
  • Independent Features: Experience, Test Score, Interview Score
  • Dependent Feature: Salary

Data Pre-Processing Steps

  • Replace missing experience data with 0 (no experience).
  • Handle NaN values in test scores by replacing with the mean.
  • Convert strings to integers for experience data.
  • Test and interview scores range from 0 to 10.

Feature Engineering

  • Convert experience strings to integers.
  • Replace missing values with mean values for test scores and interview scores.
  • Independent features are prepared for linear regression.

Model Creation

  • Apply linear regression using the processed dataset.
  • Save the model using the pickle library (creates a .pkl file).
  • Ensure the model file is ready for deployment.

Creating Flask Environment

  • Import necessary libraries: numpy, pickle, flask.
  • Initialize Flask app.
  • Load the pre-trained model using pickle.

Flask Routes

  1. Home Route (/)

    • Redirects to the index.html file.
  2. Predict Route (/predict)

    • Accepts POST requests with inputs for prediction.
    • Uses input values to generate predictions through the model.
    • Outputs the predicted salary back into the index.html file.
  3. Hard-coded JSON API

    • Example API for testing with hard-coded values.

Running the Application

  • Structure the directories: static files, templates, and the main Python app file.
  • Use command prompt to run the Flask app: python app.py.
  • Access the web app via a browser at the specified URL.

User Interaction

  • Input fields for:
    • Years of Experience
    • Test Score (0-10)
    • Interview Score (0-10)
  • After submitting, the predicted salary is displayed.

Conclusion

  • Reminder to handle input properly to ensure accurate predictions.
  • Encouragement to subscribe for more content
  • Emphasis on continuing to learn and improve.
  • Apologies for previous delays in content.