-
Notifications
You must be signed in to change notification settings - Fork 24
/
main.py
48 lines (38 loc) · 1.05 KB
/
main.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
import pickle
import os
import pandas as pd
from fastapi import FastAPI
from pydantic import BaseModel
script_path = os.path.dirname(os.path.abspath(__file__))
app = FastAPI()
default_model_path = os.path.join(script_path, "..", "results", "models", "regression", "full", "best_model.pickle")
model_path = os.environ.get("MODEL_PATH", default_model_path)
with open(model_path, 'rb') as f:
model = pickle.load(f)
class PredictionInput(BaseModel):
artist_name: str
track_name: str
track_id: str
year: int
genre: str
danceability: float
energy: float
key: int
loudness: float
mode: int
speechiness: float
acousticness: float
instrumentalness: float
liveness: float
valence: float
tempo: float
duration_ms: int
time_signature: int
@app.get("/model_info/")
def model_info():
return str(model)
@app.post("/predict/")
def predict(input_data: PredictionInput):
data = pd.DataFrame([dict(input_data)])
prediction = model.predict(data)
return prediction.to_dict(orient="records")