12
12
from config import CONFIG
13
13
14
14
15
- from schema import (
16
- ErrorResponse ,
17
- ModelDayInput ,
18
- )
15
+ from schema import ErrorResponse , ModelDayInput , IntervalResponce , IntervalRequest
16
+
17
+ import pandas as pd
19
18
20
19
21
20
@asynccontextmanager
@@ -28,8 +27,10 @@ async def lifespan(app: FastAPI):
28
27
# Initialize the pytorch model
29
28
model = Model ()
30
29
30
+ df = pd .read_csv ("./model_ser/pred.csv" )
31
+ df ["date" ] = pd .to_datetime (df ["date" ])
31
32
# add model and other preprocess tools too app state
32
- app .package = {"model" : model }
33
+ app .package = {"model" : model , "predict" : df }
33
34
yield
34
35
35
36
@@ -114,34 +115,39 @@ def bash(command):
114
115
115
116
116
117
@app .post ("/api/v1/predict_interval" )
117
- def do_predict_interval ():
118
- """
119
- TODO: Perform prediction on input data
120
- """
121
- from random import uniform
122
-
123
- return {
124
- "data" : {
125
- "dates" : [
126
- "2020-01-01" ,
127
- "2020-01-02" ,
128
- "2020-01-03" ,
129
- "2020-01-04" ,
130
- "2020-01-05" ,
131
- "2020-01-06" ,
132
- ],
133
- "x" : [1 , 2 , 3 , 4 , 5 , 6 ],
134
- "y_pred" : [
135
- 1.8 ,
136
- 2.6 ,
137
- uniform (1.3 , 2.2 ),
138
- uniform (2.2 , 3.8 ),
139
- uniform (3.3 , 3.5 ),
140
- uniform (1.0 , 1.3 ),
141
- ],
142
- "y_true" : [2 , 3 ],
143
- }
144
- }
118
+ def do_predict_interval (data : IntervalRequest ):
119
+ df = app .package ["predict" ]
120
+ filter_df = df [
121
+ df ["date" ]
122
+ >= pd .to_datetime (data .left ) & df ["date" ]
123
+ <= pd .to_datetime (data .right )
124
+ ]
125
+ length = [i + 1 for i in range (len (filter_df ))]
126
+
127
+ return IntervalResponce (
128
+ dates = filter_df ["date" ].tolist (),
129
+ x = length ,
130
+ y_pred = filter_df ["preds" ].tolist (),
131
+ y_true = filter_df ["true_y" ].tolist (),
132
+ )
133
+ # return {
134
+ # "data": {
135
+ # "dates": [
136
+ # "2020-01-01",
137
+ # "2020-01-02",
138
+ # "2020-01-03",
139
+ # "2020-01-04",
140
+ # "2020-01-05",
141
+ # "2020-01-06",
142
+ # ],
143
+ # "x": [1, 2, 3, 4, 5, 6],
144
+ # "y_pred": [
145
+ # 1.8,
146
+ # 2.6
147
+ # ],
148
+ # "y_true": [2, 3],
149
+ # }
150
+ # }
145
151
146
152
147
153
@app .post (
0 commit comments