Application error in opening app in heroku

41 Views Asked by At

I have made a stock predicting model and saved it using Pickle. Then I load the model to Python flask code and launch it to Heroku. after pushing the app to Heroku it says application error.

I have tested my main.py code with Postman software, and it works perfectly. But when I load it to Heroku and open the app is say the error, which is:

Application error
An error occurred in the application and your page could not be served. If you are the application owner, check your logs for details. You can do this from the Heroku CLI with the command
heroku logs --tail

This is my main.py

import os
import pickle as pk
from flask import Flask, request, jsonify
import numpy as np

app = Flask(__name__)

model_path = 'models/model'
if not os.path.exists(model_path):
    raise FileNotFoundError(f"Model file not found at: {model_path}")

with open(model_path, 'rb') as model_file:
    model = pk.load(model_file)

@app.route('/')
def index():
    return "Hello world"

@app.route('/predict', methods=['POST'])
def predict():
    try:
        sex = float(request.form.get('sex'))
        age = float(request.form.get('age'))
        hypertension = int(request.form.get('hypertension'))
        disease = int(request.form.get('disease'))
        work = int(request.form.get('work'))
        glucose = float(request.form.get('glucose'))
        bmi = float(request.form.get('bmi'))
        smoking = int(request.form.get('smoking'))

        input_query = np.array([[sex, age, hypertension, disease, work, glucose, bmi, smoking]])
        result = model.predict(input_query)[0]

        return jsonify({'placement': str(result)})

    except Exception as e:
        return jsonify({'error': str(e)})

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

Other files are

requirements.txt
Flask==3.0.0
gunicorn==21.2.0
numpy==1.26.2
scikit-learn==1.3.2

.gitignore
__pycache__
*.pyc
*.pyo
*.pyd
*.db
*.sqlite
venv/

Procfile
web: gunicorn main:app

The error I am facing is:

2023-12-18T14:55:25.125742+00:00 app[web.1]: raise FileNotFoundError(f"Model file not found at: {model_path}")
2023-12-18T14:55:25.125742+00:00 app[web.1]: FileNotFoundError: Model file not found at: C:/Users/Anik/PycharmProjects/ML/models/model

0

There are 0 best solutions below