当我使用FLASK PYTHON调用app.run()
每当我们对DATA SCIENCE模型进行评分时,我们需要传递包含训练模型的Excel数据的字典对象
def dictCall():
model_root = "/app/RDD/Data/appdrain/"
predictions_root = "/app/RDD/Data/appdrain/Predictions"
model_stats_file = '/app/RDD/Data/appdrain/per_app_stats.tsv'
models_dict = dict()
with open(model_stats_file, 'r') as csvfile:
model_stats_reader = csv.reader(csvfile, delimiter='\t')
for row in model_stats_reader:
make = row[0]
model = row[1]
appname=row[2]
appversion=row[4]
threshold=row[5]
if row[5] == 'NULL':
continue
percentiles = [float(i) for i in row[8][1:-1].split(",")]
app_drain_model = AppDrainModel(make, model, appname, appversion, threshold , percentiles)
keys=make+model+appname+appversion
models_dict[keys] = app_drain_model
return models_dict
@app.route('/MachineLearning/AppDrain/score', methods=['GET', 'POST'])
modeldict=dictCall()
MODELRESULT2=AppDrainScoring.appcall(Make, Model, appName, appVer,
meandr,modeldict)
app.wsgi_app = ProxyFix(app.wsgi_app)
if __name__ == '__main__':
app.run()
答案 0 :(得分:0)
您是否尝试过将变量models_dict
设为全局:
models_dict = dict()
def dictCall():
global models_dict
model_root = "/app/RDD/Data/appdrain/"
predictions_root = "/app/RDD/Data/appdrain/Predictions"
model_stats_file = '/app/RDD/Data/appdrain/per_app_stats.tsv'
if len(models_dict) == 0:
with open(model_stats_file, 'r') as csvfile:
model_stats_reader = csv.reader(csvfile, delimiter='\t')
for row in model_stats_reader:
make = row[0]
model = row[1]
appname=row[2]
appversion=row[4]
threshold=row[5]
if row[5] == 'NULL':
continue
percentiles = [float(i) for i in row[8][1:-1].split(",")]
app_drain_model = AppDrainModel(make, model, appname, appversion, threshold , percentiles)
keys=make+model+appname+appversion
models_dict[keys] = app_drain_model
return models_dict
@app.route('/MachineLearning/AppDrain/score', methods=['GET', 'POST'])
modeldict=dictCall()
MODELRESULT2=AppDrainScoring.appcall(Make, Model, appName, appVer,
meandr,modeldict)
app.wsgi_app = ProxyFix(app.wsgi_app)
if __name__ == '__main__':
app.run()