[英]How to process uploaded csv file by using web.py in python
I am trying to make a API for my model. 我正在尝试为我的模型制作一个API。
I am trying to upload csv file and then read data in csv and then use model for prediction in API. 我正在尝试上传csv文件,然后在csv中读取数据,然后在API中使用模型进行预测。
I am able to upload the file and save in path but unable to read csv data for prediction by using web.py in python 我能够上传文件并保存在路径中,但无法通过在python中使用web.py读取csv数据进行预测
I have saved model for prediction and loaded in this code then predicting data. 我已经保存了预测模型,并在此代码中加载了预测数据。
upload.py upload.py
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()
Edit-1 编辑1
# x is the input data
svmModel = open('D:/Model/model_city_id_predictor.pkl', 'rb') # SVM Model Imported svmModel = joblib.load(svmModel) # Model Loaded class_prediced = svmModel.predict(x) # here we are using to predict
Please suggest 请建议
Use 采用
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()
This will write your file to the specified location. 这会将您的文件写入指定的位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.