【问题标题】:How to make API by using Flask Python如何使用 Flask Python 制作 API
【发布时间】:2019-01-02 05:59:00
【问题描述】:

我正在尝试为我的 SVM 模型创建一个 API,以通过 API 预测数据。

我尝试了以下代码,但在运行http://127.0.0.1:5000/predict/ URL 时出现错误。

错误:

* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [02/Jan/2019 11:36:14] "GET / HTTP/1.1" 404 -
NameError: name 'svmModel' is not defined
127.0.0.1 - - [02/Jan/2019 11:36:32] "GET /predict HTTP/1.1" 500 -

我有一些地址以及通过我的模型预测城市 ID 的内容。我的模型工作正常。

更新错误:

An exception has occurred, use %tb to see the full traceback.

SystemExit: 1

D:\Conda\Conda_install\lib\site-packages\IPython\core\interactiveshell.py:3275: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D.
  warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)

编辑 1

http://127.0.0.1:5000/predict?property_address=<address>我只得到一个地址输出,但我想在浏览器上发布所有地址预测。

例如:

@app.route('/predict', methods=['GET'])
def predict():
    property_address = request.args.get('property_address')
    print (property_address)
    # Get values from browser
    input_data = "SELECT Detail_ID,PROPERTY_ADD + ', ' + MAIN_LOCALITY + ', ' + CITY AS PROPERTY_ADDRESS FROM NHB.DBO.HFC_UNPROCESS_01JUL2018TO30SEP2018 WHERE PROPERTY_ADD is not null"
    df = pd.read_sql(input_data,cnxn)  
    df = pd.DataFrame(df)  
    df.fillna({'PROPERTY_ADDRESS': 'NA'}, inplace=True)
    test_data = df['PROPERTY_ADDRESS'].values.tolist()

    for i in range(0, 5):
            #print (test_data[i])
        class_prediced = svmModel.predict(test_data)[0] 
        output = "Predicted City ID: " + str(class_prediced)
        #print (output)
        return (output)

在这里,我使用 for 循环来获得多个输出。

输入:

['Cabin K-1, Laxmi Rd, Aarey Colony, Goregaon East, Mumbai, Maharashtra 400065, India',
'Aarey Colony, Goregaon East, Mumbai, Maharashtra, India',
'Goregaon East, Mumbai, Maharashtra, India']`

预期输出:

在浏览器上:

'Cabin K-1, Laxmi Rd, Aarey Colony, Goregaon East, Mumbai, Maharashtra 400065, India'
Predicted City ID: 1

'Aarey Colony, Goregaon East, Mumbai, Maharashtra, India'
Predicted City ID: 1

'Goregaon East, Mumbai, Maharashtra, India'
Predicted City ID: 1

请推荐

【问题讨论】:

  • 我认为您不小心再次粘贴了问题而不是错误消息
  • 改成这个@app.route('/predict', methods=['GET'])
  • @P.hunter @app.route() 默认只监听GET
  • 您不应该在 Python 中使用 global 关键字,尤其是在 Flask 等 Web 框架中。在您的情况下,全球化打开和后来关闭的文件甚至没有任何意义。

标签: python python-3.x flask


【解决方案1】:

由于您期望 property_address 作为请求参数,

property_address = request.args.get('property_address')

将此作为 URL 请求可能会使您免于错误:

http://127.0.0.1:5000/predict?property_address=<address>

您的自定义property_address 以获得您想要的输出。

【讨论】:

  • 收到错误:NameError: name 'svmModel' is not defined 127.0.0.1 - - [02/Jan/2019 11:40:34] "GET /predict?property_address=%3Caddress%3E HTTP/1.1" 500 -
  • @user10600066 您对property_address 的期望值是多少?这必须是某个值,而不是默认的&lt;address&gt;,因为这只是一个模板值。可以是http://127.0.0.1:5000/predict?property_address=123http://127.0.0.1:5000/predict?property_address=345
  • 请求以您想要的值作为属性地址的 URL
  • property address我是从sql server中取的,所以有多个地址
  • 是的,我完成了这个过程,但无法返回列表,但现在我可以使用以下方法返回所有值:class_prediced = svmModel.predict(test_data)
【解决方案2】:

您在这一行中使用了svmModel 而不是svmIrisModel,这是全局变量

class_prediced = svmModel.predict(test_data)[0]

【讨论】:

  • 是的,这是类型错误,但在浏览器上仍然出现错误:Internal Server Error The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application. 和 spyder 控制台中:NameError: name 'svmModel' is not defined 127.0.0.1 - - [02/Jan/2019 11:45:13] "GET /predict?property_address=%3Caddress%3E HTTP/1.1" 500 -
  • @user10600066 只需发布服务器端错误以便于调试
  • @user10600066 您的新代码没有意义,因为您刚刚更改了全局变量名称。数据被加载到预测函数无法访问的变量中。
猜你喜欢
  • 1970-01-01
  • 2021-07-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-07
  • 2017-11-06
  • 1970-01-01
相关资源
最近更新 更多