Skip to content

Commit 56043b9

Browse files
author
Alan
committed
last
1 parent 1ceb42c commit 56043b9

File tree

3 files changed

+52
-36
lines changed

3 files changed

+52
-36
lines changed

app/main.py

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@
1212
from config import CONFIG
1313

1414

15-
from schema import (
16-
ErrorResponse,
17-
ModelDayInput,
18-
)
15+
from schema import ErrorResponse, ModelDayInput, IntervalResponce, IntervalRequest
16+
17+
import pandas as pd
1918

2019

2120
@asynccontextmanager
@@ -28,8 +27,10 @@ async def lifespan(app: FastAPI):
2827
# Initialize the pytorch model
2928
model = Model()
3029

30+
df = pd.read_csv("./model_ser/pred.csv")
31+
df["date"] = pd.to_datetime(df["date"])
3132
# add model and other preprocess tools too app state
32-
app.package = {"model": model}
33+
app.package = {"model": model, "predict": df}
3334
yield
3435

3536

@@ -114,34 +115,39 @@ def bash(command):
114115

115116

116117
@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+
# }
145151

146152

147153
@app.post(

app/model.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
from schema import ModelDayInput
22
from functools import lru_cache
33
from collections import defaultdict
4+
import random
45

56

67
@lru_cache(maxsize=None)
78
def zatchka(x):
89
return x
910

1011

11-
import random
12-
13-
1412
class Model:
1513
"""
1614
мы создали в airflow даги и уже все работало и все сериализовалось joblibом,

app/schema.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,18 @@ class InferenceResponse(BaseModel):
5050
data: InferenceOutput
5151

5252

53+
class IntervalRequest(BaseModel):
54+
left: date
55+
right: date
56+
57+
58+
class IntervalResponce(BaseModel):
59+
dates: list[date]
60+
x: list[int]
61+
y_pred: list[float]
62+
y_true: list[float]
63+
64+
5365
########
5466
########
5567
########

0 commit comments

Comments
 (0)