0
$\begingroup$

I am trying to deploy my ML model using flask. My model contains both categorical and numerical variables. Below is my model.py code:-

#PIPELINE FOR PREPROCESSING 
dtr_pipe = Pipeline(steps = [('preproc', preproc), ('model', 
dtr_model)])

train_x, test_x, train_y, test_y = train_test_split(data2, y, test_size 
= 0.2, random_state = 69)

#OPTUNA
def objective(trial):

    model__max_depth = trial.suggest_int('model__max_depth', 2, 32)
    model__max_leaf_nodes = trial.suggest_int('model__max_leaf_nodes', 
                                               50, 500)
    model__max_features = trial.suggest_float('model__max_features', 
                                               0.0, 1.0)
    model__min_samples_leaf=trial.suggest_int('model__min_samples_leaf', 
                                               1, 50)

    params = {'model__max_depth' : model__max_depth, 
             'model__max_leaf_nodes' : model__max_leaf_nodes,
             'model__max_features' : model__max_features,
             'model__min_samples_leaf' : model__min_samples_leaf}

    dtr_pipe.set_params(**params)

    return np.mean(-1 * cross_val_score(dtr_pipe, train_x, train_y,
                                 cv = 5, n_jobs = -1, scoring = 
                                         'neg_mean_squared_error'))


 dtr_study = optuna.create_study(direction = 'minimize')
 dtr_study.optimize(objective, n_trials = 10)

 dtr_pipe.set_params(**dtr_study.best_params)
 dtr_pipe.fit(train_x, train_y)

 pickle.dump(dtr_pipe, open('model.pkl', 'wb'))
 model = pickle.load(open('model.pkl', 'rb'))

Below is my app.py code:-

@app.route('/', methods = ['GET', 'POST'])
def main():
   if request.method == 'GET':
     return render_template('index.html')

  if request.method == 'POST':
     powerPS = request.form['powerPS']
     model = request.form['model']
     kilometer = request.form['kilometer']
     fuelType = request.form['fuelType']
     vehicleType = request.form['vehicleType']
     gearbox = request.form['gearbox']
     notRepairedDamage = request.form['notRepairedDamage']
     brand = request.form['brand']
     age = request.form['age']
    
     data = [[powerPS, model, kilometer, fuelType, vehicleType, gearbox, 
             notRepairedDamage, brand, age]]
    
     input_variables = pd.DataFrame(data,columns=['powerPS', 'model', 'kilometer', 'fuelType', 
                                                 'vehicleType','gearbox', 'notRepairedDamage', 
                                                 'brand', 'age'],
                                   dtype='float',
                                   index=['input'])

     predictions = model.predict(input_variables)[0]
     print(predictions)

     return render_template('index.html', original_input={'powerPS': powerPS, 'model': model, 
                                                         'kilometer': kilometer, 
                                                         'fuelType':fuelType, 
                                                         'vehicleType': vehicleType, 
                                                         'gearbox': gearbox, 
                                                         'notRepairedDamage': 
                                                          notRepairedDamage, 'brand': brand, 
                                                         'age':age}, result=predictions)

If I run only my ML model, it runs perfectly without error. But when I deploy it using flask (above code), and enter the values in the respective fields and press submit, I get following error:-

AttributeError: 'str' object has no attribute 'predict'

Why am I getting this error and what's the solution?

$\endgroup$

1 Answer 1

2
$\begingroup$

You are overwriting the model variable in the following statement:

model = request.form['model']

You should not use the same variable name.

$\endgroup$
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.