-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
49 lines (43 loc) · 1.32 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import uvicorn
from fastapi import FastAPI, File, UploadFile
import model
import fun
import os, zipfile
import shutil
from starlette.responses import FileResponse
app = FastAPI()
@app.post("/upload")
async def upload(file: UploadFile = File(...)):
with open(file.filename, "wb") as buffer:
shutil.copyfileobj(file.file, buffer)
with zipfile.ZipFile(file.filename) as zip_file:
zip_file.extractall('Dataset/')
os.remove(file.filename)
data,code = fun.convert()
model.preprocess(data,'train')
f = open("Code/code.txt",'w+')
for i in range(len(code)):
f.write("%s\r\n" % code[i])
f.close()
return
@app.post("/train")
async def train():
oob,cv = model.train()
return "Success"
@app.post("/test")
async def test(file: UploadFile = File(...)):
with open(file.filename, "wb") as buffer:
shutil.copyfileobj(file.file, buffer)
with zipfile.ZipFile(file.filename) as zip_file:
zip_file.extractall('Testset/')
os.remove(file.filename)
data = fun.converttest()
model.preprocess(data,test)
model.test()
return download("Result/result.csv")
def download(file_path):
if os.path.isfile(file_path):
return FileResponse(file_path)
return None
if __name__ == "__main__":
uvicorn.run(app,host = '127.0.0.1',port=8080)