我正在尝试为我的模型制作API。
我正在尝试上传csv文件,然后在csv中读取数据,然后在API中使用模型进行预测。
我能够上传文件并保存在路径中,但无法通过在python中使用web.py读取csv数据进行预测
我已经保存了用于预测的模型,并在此代码中加载了然后预测数据。
upload.py
node_modules
编辑1
import web
from sklearn.externals import joblib
import requests
urls = ('/upload', 'Upload')
class Upload:
def GET(self):
web.header("Content-Type","text/html; charset=utf-8")
return """<html><head></head><body>
<form method="POST" enctype="multipart/form-data" action="">
<input type="file" name="myfile" />
<br/>
<br/>
<input type="submit" />
</form>
</body></html>"""
def POST(self):
x = web.input(myfile=[])
filedir = 'D:/API_CITY_PRED/Upload' # change this to the directory you want to store the file in.
svmModel = open('D:/Model/model_city_id_predictor.pkl', 'rb')
svmModel = joblib.load(svmModel)
class_prediced = svmModel.predict(x)
output = "Predicted City ID: " + str(class_prediced)
print (output)
if 'myfile' in x: # to check if the file-object is created
filepath=x.myfile.filename.replace('\\','/') # replaces the windows-style slashes with linux ones.
filename=filepath.split('/')[-1] # splits the and chooses the last part (the filename with extension)
fout = open(filedir +'/'+ filename,'wb') # creates the file where the uploaded file should be stored
fout.write(x.myfile.file.read()) # writes the uploaded file to the newly created file.
fout.close() # closes the file, upload complete.
return output
raise web.seeother('/upload')
if __name__ == "__main__":
app = web.application(urls, globals())
app.run()
# x is the input data
请提出建议
答案 0 :(得分:0)
使用
x = web.input(file={})
fout = open('path/to/location/', 'wb') # creates the file where the uploaded file should be stored
fout.write(x.file.file.read()) # writes the uploaded file to the newly created file.
fout.close()
这会将文件写入指定的位置。