简体   繁体   中英

Deploying Flask API on Heroku gives "app crashed" Error

I am deploying a Flask API to Heroku. That API perfectly works on localhost but I am trying to deploy it on Heroku. This is my API code:

from flask import Flask, request, jsonify
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
import tensorflow_hub as hub

model = hub.load('https://tfhub.dev/google/universal-sentence-encoder/4')
app = Flask(__name__)

@app.route('/')
def index():
    return "Hello, World!"


@app.route('/embed', methods=['GET'])
def embed():
    text = request.args.get('text')
    try:
        embedding = model([text]).numpy().tolist()
        return jsonify({'text': text, 'embedding': embedding, 'status': 'ok'})

    except Exception as e:
        print(e)
        return jsonify({'text': text, 'status': 'error'})

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

This is my Procfile, I have changed it several times after seeing the answer from this question none works.

web: gunicorn

This is my requirement.txt

numpy==1.22.3
tensorflow-hub==0.12.0
flask

So, I ran the following commands (after logging in and setting git) to push my flask API to Heroku

$ git add -A
$ git commit -m "commit message"
$ git push heroku master

It shows this,

...
remote: -----> Installing requirements with pip
remote: -----> Discovering process types
remote:        Procfile declares types -> web
remote:
remote: -----> Compressing...
remote:        Done: 80M
remote: -----> Launching...
remote:        Released v7
remote:        https://<app_name>.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy... done.
To https://git.heroku.com/<app_name>.git
   7b4ea0b..0fe40fe  master -> master

But when I go to https://<app_name>.herokuapp.com it shows this,

在此处输入图像描述

I checked heroku logs and this is the output,

2022-05-01T07:35:00.333123+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=<app_name>.herokuapp.com request_id=992baf8b-cae3-4c7a-9949-653bfdac3c65 fwd="103.88.83.161" dyno= connect= service= status=503 bytes= protocol=https

I want solution for this I have checked many StackOverflow answers but it doesn't works, If you need any extra information let me know I will add it!

You need to add gunicorn to your requirements.txt ( reference )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM