简体   繁体   中英

How to apply the Python function with predictive model into Flask application

I have a code that consists of two logical parts: receiving POST json request in Flask and function with prediction model. So here how it looks in code representation:

from flask import Flask
from flask import request
import io
import json
import pandas as pd
import numpy as np
from fbprophet import Prophet

app = Flask(__name__) 

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

def postJsonHandler():

  print (request.is_json)
  content = request.get_json()
  df = pd.io.json.json_normalize(content, ['Days', 'Orders'])
  print (df)
  return 'JSON posted'

app.run(host='0.0.0.0', port= 8090)

And here is the part with the function with model:

def TimeSeries ():

  df['Days'] = pd.to_datetime(df['Days'])
  df = df.rename(columns={'Days': 'ds',
                    'Orders': 'y'})

  my_model = Prophet(interval_width=0.95, yearly_seasonality=False, daily_seasonality=False, weekly_seasonality=True)

  df['y'] = np.log(df['y'])

  my_model.fit(df)

  future_dates = my_model.make_future_dataframe(periods=30)

  forecast = my_model.predict(future_dates)

  yhat=forecast.yhat
  ser=np.exp(yhat)
  df_upd=pd.DataFrame(ser[-30:])
  df_upd.reset_index(drop=True, inplace=True)
  js=df_upd.to_dict(orient='split')
  del js['index']
  res=json.dumps(js)    

  return res

My questions will bу following:

  1. How can I transfer the result dataframe df from first part function postJsonHandler () and use it as an input in the second part function TimeSeries()?
  2. How can I integrate my predictive function TimeSeries() in Flask environment to be able to run everything at once- to receive a json request, transform it into pandas dataframe, caluclates the result of prediction in json format and transfer this to server.

Big Thanks!

You combine your functions:

from flask import Flask
from flask import request
import io
import json
import pandas as pd
import numpy as np
from fbprophet import Prophet

app = Flask(__name__) 

my_model = Prophet(interval_width=0.95, yearly_seasonality=False, daily_seasonality=False, weekly_seasonality=True)

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

def postJsonHandler():

  print (request.is_json)
  content = request.get_json()
  df = pd.io.json.json_normalize(content, ['Days', 'Orders'])
  df['Days'] = pd.to_datetime(df['Days'])
  df = df.rename(columns={'Days': 'ds',
                    'Orders': 'y'})

  df['y'] = np.log(df['y'])

  my_model.fit(df)

  future_dates = my_model.make_future_dataframe(periods=30)

  forecast = my_model.predict(future_dates)

  yhat=forecast.yhat
  ser=np.exp(yhat)
  df_upd=pd.DataFrame(ser[-30:])
  df_upd.reset_index(drop=True, inplace=True)
  js=df_upd.to_dict(orient='split')
  del js['index']
  res=json.dumps(js)  



app.run(host='0.0.0.0', port= 8090

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