flask - 无法在 Google Colab 中访问 Flask App URL 获取站点不可用
问题描述
我创建了一个 Flask 预测应用程序(在 Google Colab 中),当我尝试运行它时,在 colab 环境中添加所有依赖项后,我得到了 url,但是当我点击它时,它显示无法访问站点。
我有 Procfile、腌制模型和需求文本文件,但由于某种原因它不起作用。另外,我尝试使用 Heroku 部署这个应用程序,它遇到了同样的命运,我得到了应用程序错误。
任何帮助或指导将不胜感激。
from flask import Flask, url_for, redirect, render_template, jsonify
from pycaret.classification import*
import pandas as pd
import numpy as np
import pickle
app = Flask(__name__)
model = load_model('Final RF Model 23JUL2021')
cols = ['AHT','NTT','Sentiment','Complaints','Repeats']
@app.route('/')
def home():
return render_template("home.html")
@app.route('/predict',methods=['POST'])
def predict():
int_features = [x for x in request.form.values()]
final = np.array(int_features)
data_unseen = pd.DataFrame([finak], columns = cols)
prediction = predict_model(model, data=data_unseen, round=0)
prediction = int(prediction.Label[0])
return render_template('home.html',pred='Predicted Maturiy Level is{}'.format(prediction))
@app.route('/predict_api',methods=['POST'])
def predict_api():
data = request.get_json(force=True)
data_unseen = pd.DataFrame([data])
prediction = predict_model(model, data=data_unseen)
output = prediction.Label[0]
return jsonify(output)
if __name__ == '__main__':
app.run(debug=True)
解决方案
您不能像在您的机器上一样运行烧瓶应用程序。你需要使用flask-ngrok
.
!pip install flask-ngrok
from flask_ngrok import run_with_ngrok
[...]
app = Flask(__name__)
run_with_ngrok(app)
[...]
app.run()
您不能在 ngrok 中使用 debug=True 参数。
推荐阅读
- javascript - 如何为每个结果javascript获取新行
- flutter - 将 TextField InputDecoration LabelText 位置和 CounterText 位置更改为后缀
- php - php 项目在本地主机服务器上工作,但在实时服务器上不工作
- python - 如何遍历两个数据帧以在第二个数据帧上找到最接近的值而不循环
- css - 再次滚动时如何防止关键帧动画重新启动?
- azure - ADF Copy Activity FTP Source 奇怪的行为
- python - 安装 dask-ml 引发“解决环境”错误
- mysql - MySQL 我无法向表中添加外键
- android - android溢出菜单项文本未显示
- javascript - 找不到 React.JS 道具