Home > Software engineering >  Python Flask API deployed on heroku- application error
Python Flask API deployed on heroku- application error

Time:07-01

I made an API for my ML model which is working fine locally. but when I'm deploying it on heroku its showing me 'Application Error', though every things seems right to me.

My Python file code:

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

model = pickle.load(open('pune4.pkl','rb'))

app = Flask(__name__)

@app.route('/')
def home():
    return "Pune House Price Prediction App"

@app.route('/predict',methods=['POST'])

#"Viman Nagar","Plot  Area",1000,2,3,1,"Ready To Move"
#"Kothrud","Plot  Area",1000,2,3,1,"Ready To Move"

def predict():
    #location,area,sqft,bath,bhk,balcony,ready

    Col_Name = ['bath', 'balcony', 'bhk', 'new_total_sqft', 'Alandi Road', 'Ambegaon Budruk', 'Anandnagar', 'Aundh', 'Aundh Road', 'Balaji Nagar', 'Baner', 'Baner road', 'Bhandarkar Road', 'Bhavani Peth', 'Bibvewadi', 'Bopodi', 'Budhwar Peth', 'Bund Garden Road', 'Camp', 'Chandan Nagar', 'Dapodi', 'Deccan Gymkhana', 'Dehu Road', 'Dhankawadi', 'Dhayari Phata', 'Dhole Patil Road', 'Erandwane', 'Fatima Nagar', 'Fergusson College Road', 'Ganesh Peth', 'Ganeshkhind', 'Ghorpade Peth', 'Ghorpadi', 'Gokhale Nagar', 'Gultekdi', 'Guruwar peth', 'Hadapsar', 'Hadapsar Industrial Estate', 'Hingne Khurd', 'Jangali Maharaj Road', 'Kalyani Nagar', 'Karve Nagar', 'Karve Road', 'Kasba Peth', 'Katraj', 'Khadaki', 'Khadki', 'Kharadi', 'Kondhwa', 'Kondhwa Budruk', 'Kondhwa Khurd', 'Koregaon Park', 'Kothrud', 'Law College Road', 'Laxmi Road', 'Lulla Nagar', 'Mahatma Gandhi Road', 'Mangalwar peth', 'Manik Bagh', 'Market yard', 'Model colony', 'Mukund Nagar', 'Mundhawa', 'Nagar Road', 'Nana Peth', 'Narayan Peth', 'Narayangaon', 'Navi Peth', 'Padmavati', 'Parvati Darshan', 'Pashan', 'Paud Road', 'Pirangut', 'Prabhat Road', 'Pune Railway Station', 'Rasta Peth', 'Raviwar Peth', 'Sadashiv Peth', 'Sahakar Nagar', 'Salunke Vihar', 'Sasson Road', 'Satara Road', 'Senapati Bapat Road', 'Shaniwar Peth', 'Shivaji Nagar', 'Shukrawar Peth', 'Sinhagad Road', 'Somwar Peth', 'Swargate', 'Tilak Road', 'Uruli Devachi', 'Vadgaon Budruk', 'Viman Nagar', 'Vishrant Wadi', 'Wadgaon Sheri', 'Wagholi', 'Wakadewadi', 'Wanowrie', 'Warje', 'Yerawada', 'Ready To Move', 'Built-up  Area', 'Carpet  Area', 'Plot  Area']
 
    Col = np.array(Col_Name)
    location = request.form.get('location')
    area = request.form.get('area')
    sqft = request.form.get('sqft')
    bath = request.form.get('bath')
    bhk = request.form.get('bhk')
    balcony = request.form.get('balcony')
    ready = request.form.get('ready')
    
    x=np.zeros(104)
    x[0]=bath
    x[1]=balcony
    x[2]=bhk
    x[3]=sqft
    if location in Col_Name:
        x[Col_Name.index(location)]=1
    if area in Col_Name:
        x[Col_Name.index(area)]=1
    if ready in Col_Name:
        x[Col_Name.index(ready)]=1

    input_query = np.array([x])

    result = model.predict(input_query)[0]

    return jsonify({'price': str("{:.2f}".format(float(result)) " Lakhs")})

    

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

Procfile I have tried two ways and from both its not working...First one is

web: gunicorn -b :$PORT app:app

2nd one

web: gunicorn app:app

requirements.txt

flask
numpy
sklearn
gunicorn

This code is working locally but not when I'm deploying it on heroku...I'm sharing with you the build log for the same below..

-----> Building on the Heroku-20 stack
-----> Using buildpack: heroku/python
-----> Python app detected
-----> No Python version was specified. Using the same version as the last build: python-3.10.5
       To use a different version, see: https://devcenter.heroku.com/articles/python-runtimes
-----> Requirements file has been changed, clearing cached dependencies
-----> Installing python-3.10.5
-----> Installing pip 22.1.2, setuptools 60.10.0 and wheel 0.37.1
-----> Installing SQLite3
-----> Installing requirements with pip
       Collecting flask
         Downloading Flask-2.1.2-py3-none-any.whl (95 kB)
       Collecting numpy
         Downloading numpy-1.23.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (17.0 MB)
       Collecting sklearn
         Downloading sklearn-0.0.tar.gz (1.1 kB)
         Preparing metadata (setup.py): started
         Preparing metadata (setup.py): finished with status 'done'
       Collecting gunicorn==19.9.0
         Downloading gunicorn-19.9.0-py2.py3-none-any.whl (112 kB)
       Collecting Werkzeug>=2.0
         Downloading Werkzeug-2.1.2-py3-none-any.whl (224 kB)
       Collecting click>=8.0
         Downloading click-8.1.3-py3-none-any.whl (96 kB)
       Collecting itsdangerous>=2.0
         Downloading itsdangerous-2.1.2-py3-none-any.whl (15 kB)
       Collecting Jinja2>=3.0
         Downloading Jinja2-3.1.2-py3-none-any.whl (133 kB)
       Collecting scikit-learn
         Downloading scikit_learn-1.1.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (30.4 MB)
       Collecting MarkupSafe>=2.0
         Downloading MarkupSafe-2.1.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (25 kB)
       Collecting joblib>=1.0.0
         Downloading joblib-1.1.0-py2.py3-none-any.whl (306 kB)
       Collecting threadpoolctl>=2.0.0
         Downloading threadpoolctl-3.1.0-py3-none-any.whl (14 kB)
       Collecting scipy>=1.3.2
         Downloading scipy-1.8.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (42.2 MB)
       Building wheels for collected packages: sklearn
         Building wheel for sklearn (setup.py): started
         Building wheel for sklearn (setup.py): finished with status 'done'
         Created wheel for sklearn: filename=sklearn-0.0-py2.py3-none-any.whl size=1310 sha256=6dfdd78f6b3383217fa591572a1ddd52a2fb27f130bd908add0dadf94d9246b2
         Stored in directory: /tmp/pip-ephem-wheel-cache-mswus1_s/wheels/9b/13/01/6f3a7fd641f90e1f6c8c7cded057f3394f451f340371c68f3d
       Successfully built sklearn
       Installing collected packages: Werkzeug, threadpoolctl, numpy, MarkupSafe, joblib, itsdangerous, gunicorn, click, scipy, Jinja2, scikit-learn, flask, sklearn
       Successfully installed Jinja2-3.1.2 MarkupSafe-2.1.1 Werkzeug-2.1.2 click-8.1.3 flask-2.1.2 gunicorn-19.9.0 itsdangerous-2.1.2 joblib-1.1.0 numpy-1.23.0 scikit-learn-1.1.1 scipy-1.8.1 sklearn-0.0 threadpoolctl-3.1.0
-----> Discovering process types
       Procfile declares types -> web
-----> Compressing...
       Done: 115.9M
-----> Launching...
       Released v7
       https://house-price-prediction-pun-app.herokuapp.com/ deployed to Heroku
This app is using the Heroku-20 stack, however a newer stack is available.
To upgrade to Heroku-22, see:
https://devcenter.heroku.com/articles/upgrading-to-the-latest-stack

And this is what I'm getting by running 'heroku log --tail' command..

2022-06-30T15:12:54.757974 00:00 heroku[web.1]: State changed from crashed to starting
2022-06-30T15:12:59.995157 00:00 heroku[web.1]: Starting process with command `gunicorn -b :59444 app:app`
2022-06-30T15:13:00.848346 00:00 app[web.1]: [2022-06-30 15:13:00  0000] [4] [INFO] Starting gunicorn 19.9.0
2022-06-30T15:13:00.848665 00:00 app[web.1]: [2022-06-30 15:13:00  0000] [4] [INFO] Listening at: http://0.0.0.0:59444 (4)
2022-06-30T15:13:00.848708 00:00 app[web.1]: [2022-06-30 15:13:00  0000] [4] [INFO] Using worker: sync
2022-06-30T15:13:00.850104 00:00 app[web.1]: /app/.heroku/python/lib/python3.10/os.py:1029: RuntimeWarning: line buffering (buffering=1) isn't supported in binary mode, the default buffer size will be used
2022-06-30T15:13:00.850105 00:00 app[web.1]: return io.open(fd, mode, buffering, encoding, *args, **kwargs)
2022-06-30T15:13:00.852447 00:00 app[web.1]: [2022-06-30 15:13:00  0000] [9] [INFO] Booting worker with pid: 9
2022-06-30T15:13:00.855281 00:00 app[web.1]: [2022-06-30 15:13:00  0000] [9] [ERROR] Exception in worker process
2022-06-30T15:13:00.855281 00:00 app[web.1]: Traceback (most recent call last):
2022-06-30T15:13:00.855283 00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
2022-06-30T15:13:00.855283 00:00 app[web.1]: worker.init_process()
2022-06-30T15:13:00.855284 00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/gunicorn/workers/base.py", line 129, in init_process
2022-06-30T15:13:00.855284 00:00 app[web.1]: self.load_wsgi()
2022-06-30T15:13:00.855284 00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/gunicorn/workers/base.py", line 138, in load_wsgi
2022-06-30T15:13:00.855284 00:00 app[web.1]: self.wsgi = self.app.wsgi()
2022-06-30T15:13:00.855285 00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/gunicorn/app/base.py", line 67, in wsgi   
2022-06-30T15:13:00.855285 00:00 app[web.1]: self.callable = self.load()
2022-06-30T15:13:00.855285 00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/gunicorn/app/wsgiapp.py", line 52, in load2022-06-30T15:13:00.855286 00:00 app[web.1]: return self.load_wsgiapp()
2022-06-30T15:13:00.855286 00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/gunicorn/app/wsgiapp.py", line 41, in load_wsgiapp
2022-06-30T15:13:00.855286 00:00 app[web.1]: return util.import_app(self.app_uri)
2022-06-30T15:13:00.855286 00:00 app[web.1]: File "/app/.heroku/python/lib/python3.10/site-packages/gunicorn/util.py", line 350, in import_app2022-06-30T15:13:00.855286 00:00 app[web.1]: __import__(module)
2022-06-30T15:13:00.855287 00:00 app[web.1]: ModuleNotFoundError: No module named 'app'
2022-06-30T15:13:00.855356 00:00 app[web.1]: [2022-06-30 15:13:00  0000] [9] [INFO] Worker exiting (pid: 9)
2022-06-30T15:13:00.885465 00:00 app[web.1]: [2022-06-30 15:13:00  0000] [4] [INFO] Shutting down: Master
2022-06-30T15:13:00.885501 00:00 app[web.1]: [2022-06-30 15:13:00  0000] [4] [INFO] Reason: Worker failed to boot.
2022-06-30T15:13:01.022806 00:00 heroku[web.1]: Process exited with status 3
2022-06-30T15:13:01.080976 00:00 heroku[web.1]: State changed from starting to crashed
2022-06-30T15:13:11.000000 00:00 app[api]: Build succeeded
2022-06-30T15:14:07.431170 00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=house-price-prediction-pun-app.herokuapp.com request_id=9c02f2d9-969f-4118-aa10-12d37a9e9a9b fwd="103.86.183.215" dyno= connect= service= status=503 bytes= protocol=https   
2022-06-30T15:14:32.646211 00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=house-price-prediction-pun-app.herokuapp.com request_id=d5917fd2-6f3a-4dfd-8238-666f75708dd6 fwd="103.86.183.215" dyno= connect= service= status=503 bytes= protocol=https   
2022-06-30T15:14:35.036907 00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=house-price-prediction-pun-app.herokuapp.com request_id=2a130348-f99b-4611-b814-6ae82b192cad fwd="103.86.183.215" dyno= connect= service= status=503 bytes= protocol=https   
2022-06-30T15:23:09.325242 00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=house-price-prediction-pun-app.herokuapp.com request_id=e26dd793-0151-48ca-94b0-9360f3d34986 fwd="103.86.183.215" dyno= connect= service= status=503 bytes= protocol=https   

CodePudding user response:

The name of your file should be app.py

  • Related