-
Notifications
You must be signed in to change notification settings - Fork 3
/
fastapi_demo.py
35 lines (26 loc) · 951 Bytes
/
fastapi_demo.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
import uvicorn
import pickle
from fastapi import FastAPI
from music import Music
app = FastAPI()
with open("model.pkl", "rb") as f:
model = pickle.load(f)
@app.get('/')
def index():
return {'message': 'This is the homepage of the API '}
@app.post('/predict')
def get_music_category(data: Music):
received = data.dict()
acousticness = received['acousticness']
danceability = received['danceability']
energy = received['energy']
instrumentalness = received['instrumentalness']
liveness = received['liveness']
speechiness = received['speechiness']
tempo = received['tempo']
valence = received['valence']
pred_name = model.predict([[acousticness, danceability, energy,
instrumentalness, liveness, speechiness, tempo, valence]]).tolist()[0]
return {'prediction': pred_name}
if __name__ == '__main__':
uvicorn.run(app, host='127.0.0.1', port=4000, debug=True)