skip to Main Content

Description:
I’m encountering an issue while attempting to create a Flask RESTful API to deploy a Machine Learning model. I have set up all the necessary files, including app.py, index.html, and imported the model.pkl file containing my trained ML model. However, despite multiple attempts, the HTML server only displays the input page and fails to respond with any output.

In my app.py file, I’ve defined routes for rendering the HTML template and handling POST requests for prediction. Here’s a snippet of the code:

python:

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

model = pickle.load(open("C:/Users/pathi/OneDrive/Documents/ML Model Deployment Project/model.pkl", "rb"))

app = Flask(__name__)

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

@app.route ("/predict", methods = ['POST'])
def Predict_species():
    spl = float(request.form.get('sepal length (cm)'))
    spw = float(request.form.get('sepal width (cm)'))
    ptl = float(request.form.get('petal length (cm)'))
    ptw = float(request.form.get('petal width (cm)'))

    Output = model.predict(np.array([spl, spw, ptl, ptw]).reshape(1,4))

    return(str(Output))

In my index.html, I have a simple form for input:

html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Flower Species Predictor</title>
</head>
<body>

    <h1>Flower Species Predictor</h1>
    <form action="/predict" method="POST">
        <label>sepal length (cm)</label><br>
        <input type="text" name="sepal length (cm)"><br><br>

        <label>sepal width (cm)</label><br>
        <input type="text" name="sepal width (cm)"><br><br>

        <label>petal length (cm)</label><br>
        <input type="text" name="petal length (cm)"><br><br>

        <label>petal width (cm)</label><br>
        <input type="text" name="petal width (cm)"><br><br>

        <input type="submit" value="Predict"><br><br>
    </form>

</body>
</html>

Despite filling out the form and submitting the input, I’m not receiving any output from my model.predict function.

I’m seeking assistance to identify what could be causing this issue and how I can resolve it to obtain output from my Machine Learning model. Any guidance or suggestions would be greatly appreciated. Thank you.

2

Answers


  1. Chosen as BEST ANSWER

    I was just missing the action tag in my HTML. Now, the model is working fine.

    <form action="/Output" method="POST">
            <label>sepal length (cm)</label><br>
            <input type="text" name="sepal length (cm)"><br><br>
    
            <label>sepal width (cm)</label><br>
            <input type="text" name="sepal width (cm)"><br><br>
    
            <label>petal length (cm)</label><br>
            <input type="text" name="petal length (cm)"><br><br>
    
            <label>petal width (cm)</label><br>
            <input type="text" name="petal width (cm)"><br><br>
    
            <input type="submit" value="Output"><br><br>
        </form>
    

  2. It seems that the problem is with your model, not flask app. you can see value of model’s output with print(Output) in console. another way to check that if your model is doing its job is filling creating example data like these:

    @app.route("/predict", methods=["POST"])
    def predict_species():
        spl = float(request.form.get("sepal length (cm)"))
        spw = float(request.form.get("sepal width (cm)"))
        ptl = float(request.form.get("petal length (cm)"))
        ptw = float(request.form.get("petal width (cm)"))
    
        # You can consider this as mock model that takes input
        # and gives x100 output. Run flask code with this and check
        # if it works or not.
        Output = [spl * 100, spw * 100, ptl * 100, ptw * 100]
    
        return str(Output)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search