-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
67 lines (44 loc) · 1.63 KB
/
app.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from fastapi import FastAPI
from fastapi import UploadFile, File, Request
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates
import os
import threading
import psutil
from multiprocessing import Process
from stock_load_model import test_csv
from metrics import Metrics, start_metrics
import time
app = FastAPI(debug=True)
templates = Jinja2Templates(directory=".") # Change this path accordingly
@app.get("/", response_class=HTMLResponse)
def index(request: Request):
Metrics.Request_counter.labels(method='GET').inc()
return templates.TemplateResponse("index.html", {"request": request})
@app.post("/predict")
def predict(file: UploadFile = File(...)):
start = time.perf_counter()
file_bytes = file.file.read()
try:
contents = file_bytes
with open("uploaded_" + file.filename, "wb") as f:
f.write(contents)
except Exception:
return {"message": "There was an error uploading the file"}
finally:
file.file.close()
Metrics.Request_counter.labels(method='POST').inc()
output = test_csv("uploaded_" + file.filename)
end = time.perf_counter() - start
Metrics.h.observe(end)
# TODO: Fix the label part
label_pred = int(output[0][0] >= 0.8)
if label_pred == 0:
Metrics.pred_counter.labels(pred='NEGATIVE').inc()
else:
Metrics.pred_counter.labels(pred='POSITIVE').inc()
return {"label": label_pred}
if __name__ == "__main__":
import uvicorn
start_metrics(8080)
uvicorn.run("app:app", host="0.0.0.0", port=8000, reload=True, access_log=False, workers=1 )