A2N15
A2N15

Reputation: 605

Perform predictions on a data in csv file using Flask and python

Hello world,

New in Python and Flask API, I am trying to import a csv file and export another csv file with prediction

The INPUT csv file looks like this

experience    test_score    interview   
five          10            110         
nan           5             140         
six           15            90          
.....

The Python script looks like this (app.py)

from flask import Flask, make_response, request
import io
import csv

app = Flask(__name__)

def transform(text_file_contents):
return text_file_contents.replace("=", ",")


@app.route('/')
def form():
return """
    <html>
        <body>
            <h1>Let's TRY to Predict..</h1>
            </br>
            </br>
            <p> Insert your CSV file and then download the Result
            <form action="/transform" method="post" enctype="multipart/form-data">
                <input type="file" name="data_file" class="btn btn-block"/>
                </br>
                </br>
                <button type="submit" class="btn btn-primary btn-block btn-large">Predict</button>
            </form>
        </body>
    </html>
"""

@app.route('/transform', methods=["POST"])
def transform_view():
f = request.files['data_file']
if not f:
    return "No file"

stream = io.StringIO(f.stream.read().decode("UTF8"), newline=None)
csv_input = csv.reader(stream)
#print("file contents: ", file_contents)
#print(type(file_contents))
print(csv_input)
for row in csv_input:
    print(row)

stream.seek(0)
result = transform(stream.read())

response = make_response(result)
response.headers["Content-Disposition"] = "attachment; filename=result.csv"
return response

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

Executing the previous script in anaconda prompt with the following code

base (C:\Users\username\ python app.py

The above code will give us a http url that we copy/paste to our browser

We get the follwing screen

enter image description here

Therefore I am able to insert a csv file and download another csv file.

What I want is to apply a model script and predict the salary for ALL rows in the CSV.

I have seen many tuto that provide a script that predict values such as https://www.youtube.com/watch?v=UbCWoMf80PY. However, in most of them, we have to insert each value in order to get the prediction. Therefore, I want to predict the whole csv file in once.

The model script looks like this

df = pd.read_csv('hiring.csv')
df['experience'].fillna(0, inplace=True)
# Call the Get_dummies function
X = df.drop('salary', axis=1)
y = df.salary
X_train, X_test, y_train, y_test = train_test_split (X, y, test_size=0.33, random_state=0) 

from sklearn.tree import DecisionTreeRegressor
tree = DecisionTreeTreeRegressor (max_depth = 4, random_state = 42)
tree.fit(X_train, y_train)
pred_tree = tree.predict(X_test)

The Output CSV should looks like this

experience    test_score    interview   salary   
ten           10            110         1500 
nan           5             140         1870
six           15            90          1650
.....

Upvotes: 1

Views: 3273

Answers (1)

Hillary Murefu
Hillary Murefu

Reputation: 96

you first train the model and save it:

import pandas as pd
import numpy as np
import pickle
df = pd.read_csv('hiring.csv')
df['experience'].fillna(0, inplace=True)
# Call the Get_dummies function
X = df.drop('salary', axis=1)
y = df.salary
X_train, X_test, y_train, y_test = train_test_split (X, y, test_size=0.33, random_state=0) 

from sklearn.tree import DecisionTreeRegressor
tree = DecisionTreeTreeRegressor (max_depth = 4, random_state = 42)
tree.fit(X_train, y_train)
pred_tree = tree.predict(X_test)

# save the model to disk
filename = 'finalized_model.sav'
pickle.dump(tree, open(filename, 'wb'))

Then import the model and run predictions on the uploaded file .... then the predictions can be downloaded using the same app:

from flask import Flask, make_response, request
import io
from io import StringIO
import csv
import pandas as pd
import numpy as np
import pickle



app = Flask(__name__)

def transform(text_file_contents):
    return text_file_contents.replace("=", ",")


@app.route('/')
def form():
    return """
        <html>
            <body>
                <h1>Let's TRY to Predict..</h1>
                </br>
                </br>
                <p> Insert your CSV file and then download the Result
                <form action="/transform" method="post" enctype="multipart/form-data">
                    <input type="file" name="data_file" class="btn btn-block"/>
                    </br>
                    </br>
                    <button type="submit" class="btn btn-primary btn-block btn-large">Predict</button>
                </form>
            </body>
        </html>
    """
@app.route('/transform', methods=["POST"])
def transform_view():
    f = request.files['data_file']
    if not f:
        return "No file"

    stream = io.StringIO(f.stream.read().decode("UTF8"), newline=None)
    csv_input = csv.reader(stream)
    #print("file contents: ", file_contents)
    #print(type(file_contents))
    print(csv_input)
    for row in csv_input:
        print(row)

    stream.seek(0)
    result = transform(stream.read())

    df = pd.read_csv(StringIO(result))
    

    # load the model from disk
    loaded_model = pickle.load(open(filename, 'rb'))
    df['prediction'] = loaded_model.predict(df)

    

    response = make_response(df.to_csv())
    response.headers["Content-Disposition"] = "attachment; filename=result.csv"
    return response

if __name__ == "__main__":
    app.run(debug=False,port=9000)

Upvotes: 2

Related Questions