烧瓶通过POST方法返回流式响应

时间:2019-02-04 11:19:29

标签: python post flask stream

我是不熟悉烧瓶的人,我正试图即时流式传输ML模型的响应,以便对数据批次进行实时预测。当我在flask中调用GET方法时,该过程有效。但是,当我尝试POST方法时,我在执行结束时得到了所有预测。 我想知道为什么这不适用于Post。

非常感谢您的支持

这是我的Client.py

import requests

"""Setting the headers to send and accept json responses & the URL
"""
headers = ({'Content-Type': 'application/json' , \
                  #'Accept': 'text/html;charset=UTF-8'})
          'Accept': 'text/html ; event-stream'})


url = 'http://127.0.0.1:8070/try'

# POST <url>/predict

response = requests.post(url, \
                    #data = json.dumps({'Vin' : ['G472618', '0U30530', 'GE12249']}),\
                    json = {'Vin' : ['G472618', '0U30530', 'GE12249']} ,\
                    headers= headers, stream = True)


print(response.text)

**This is the Flask script**


@app.route('/try', methods=['POST', 'GET']) 
def Prediction():
    
    def eventStream():        
            
        #Load the model
            
        #Preprocess the data part
             .
             .

                        #Start making predictions On_Batches
                                .
                                .
                                .
                                                    
                                # driving mode predition - DANGER vs. NORMAL
                                                                
                                is_dangerous = loaded_model.predict_on_batch(Predictors)
                                pred_proba = loaded_model.predict_proba(Predictors)
                                is_dangerous_category = [ np.argmax(x) for x in is_dangerous ]
                                if is_dangerous_category == [1]:
                                    response = 'Dangerous driving event for ' + str(ID) + '<br/>'
                                    confidence = np.round(pred_proba[0][1], 3)
                                    df['driving_style'] = 'dangerous'
                                else:
                                    response = 'Normal driving event for ' + str(id) + '<br/>'
                                    confidence = np.round((pred_proba[0][0] * 100), 3)
                                    df['driving_style'] = 'normal'
                                    
                                counter += 1
                                Prediction_df = Prediction_df.append(df, sort = True)
                                    
                                
                                yield (response)
                                                   
                                
                            else:
                                pass
                                
                            if cursor.alive:
                                pass
                            else:                                       
                                K.clear_session()
                            
        cursor.close()
    
    if request.method == 'POST':
    
        data = request.get_json(force = True)
        Vin = data['ID'][0]
        
        #Establish the connection to the DB
        
        Prediction_df = pd.DataFrame()

    
    else:  
        
        ID = 'G472618'
        
        #Establish the connection to the DB
        
        Prediction_df = pd.DataFrame()

   
    if request.method == 'POST':
        return Response (stream_with_context(eventStream()), content_type = 'text/event-stream')
    else:
        return Response (response = stream_with_context(eventStream()))
    
        
#Run flask

if __name__ == '__main__':
     
    app.run(debug = True, port=8070, threaded = True)

0 个答案:

没有答案