-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathmain.py
More file actions
70 lines (61 loc) · 2.44 KB
/
Copy pathmain.py
File metadata and controls
70 lines (61 loc) · 2.44 KB
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
68
69
70
# This is an example FastAPI application that provides endpoints to start a prediction task,
# the current implementations of the dockerfile makes no use of it, but it can be useful for future extensions.
# To use this uncomment the following llines in your Dockerfile:
# #RUN pip3 install fastapi uvicorn
# #ENTRYPOINT ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
# and comment the ENTRYPOINT line that runs predict.py directly.
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from typing import Dict
import threading
import uuid
import time
import predict # your predict.py
app = FastAPI()
task_store: Dict[str, Dict] = {}
class PredictRequest(BaseModel):
prediction_data: str = r"/input/mini.las"
path_las: str = ""
model_path: str = "./model_ft_202412171652_3"
tree_id_col: str = "TreeID"
n_aug: int = 10
@app.post("/predict/start")
def start_prediction(req: PredictRequest):
task_id = str(uuid.uuid4())
task_store[task_id] = {"status": "running", "result": None}
def run_task():
try:
outfile, outfile_probs, joined, data_probs_df = predict.run_predict(
prediction_data=req.prediction_data,
path_las=req.path_las,
model_path=req.model_path,
tree_id_col=req.tree_id_col,
n_aug=req.n_aug
)
task_store[task_id] = {
"status": "completed",
"result": {
"outfile": outfile,
"outfile_probs": outfile_probs,
"joined": joined.to_dict(orient="records"),
"data_probs": data_probs_df.to_dict(orient="records")
}
}
except Exception as e:
task_store[task_id] = {"status": "failed", "error": str(e)}
threading.Thread(target=run_task).start()
return {"task_id": task_id}
@app.get("/predict/status/{task_id}")
def get_status(task_id: str):
task = task_store.get(task_id)
if not task:
raise HTTPException(status_code=404, detail="Task ID not found")
return {"status": task["status"]}
@app.get("/predict/result/{task_id}")
def get_result(task_id: str):
task = task_store.get(task_id)
if not task:
raise HTTPException(status_code=404, detail="Task ID not found")
if task["status"] != "completed":
raise HTTPException(status_code=202, detail="Task not yet completed")
return task["result"]