├── .gitignore ├── requirements.txt ├── model_tree.pkl ├── tutorial.py ├── README.md ├── main.py └── music.py /.gitignore: -------------------------------------------------------------------------------- 1 | .vscode 2 | __pycache__/ -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | fastapi 2 | uvicorn 3 | sklearn -------------------------------------------------------------------------------- /model_tree.pkl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kaustubhgupta/FastAPI-Demo/HEAD/model_tree.pkl -------------------------------------------------------------------------------- /tutorial.py: -------------------------------------------------------------------------------- 1 | import uvicorn 2 | from fastapi import FastAPI 3 | 4 | app = FastAPI() 5 | 6 | @app.get("/") 7 | def home_end_point(name: str): 8 | return {"message": f"Hello! {name}"} 9 | 10 | if __name__ == "__main__": 11 | uvicorn.run(app, host='127.0.0.1', port=8000, debug=True) -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # FastAPI-Demo 2 | 3 | This repository holds the code used for article in Analytics Vidhya Blogathon. To read the corresponding article then head over to this link: 4 | https://www.analyticsvidhya.com/blog/2020/11/fastapi-the-right-replacement-for-flask/ 5 | 6 | ## Directory Explaination 7 | 8 | - [tutorial.py](./tutorial.py) file is the one used for FastAPI demo code in article 9 | - [music.py](./music.py) file is the class layout (Pydantic model) for data validation 10 | - [main.py](./main.py) file is the music genre classifier FastAPI code 11 | - [model_tree.pkl](./model_tree.pkl) is the decision tree classifier file 12 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | import uvicorn 2 | import pickle 3 | from fastapi import FastAPI 4 | from music import Music 5 | 6 | 7 | app = FastAPI() 8 | pickle_in = open("model_tree.pkl", "rb") 9 | model = pickle.load(pickle_in) 10 | 11 | 12 | @app.get('/') 13 | def index(): 14 | return {'message': 'This is the homepage of the API '} 15 | 16 | 17 | @app.post('/predict') 18 | def get_music_category(data: Music): 19 | received = data.dict() 20 | acousticness = received['acousticness'] 21 | danceability = received['danceability'] 22 | energy = received['energy'] 23 | instrumentalness = received['instrumentalness'] 24 | liveness = received['liveness'] 25 | speechiness = received['speechiness'] 26 | tempo = received['tempo'] 27 | valence = received['valence'] 28 | pred_name = model.predict([[acousticness, danceability, energy, 29 | instrumentalness, liveness, speechiness, tempo, valence]]).tolist()[0] 30 | return {'prediction': pred_name} 31 | 32 | 33 | if __name__ == '__main__': 34 | uvicorn.run(app, host='127.0.0.1', port=8000, debug=True) 35 | -------------------------------------------------------------------------------- /music.py: -------------------------------------------------------------------------------- 1 | from pydantic import BaseModel, Field 2 | 3 | 4 | class Music(BaseModel): 5 | acousticness: float = Field(..., description="Here is the description of the input to be sent") 6 | danceability: float = Field(..., description="Here is the description of the input to be sent") 7 | energy: float = Field(..., description="Here is the description of the input to be sent") 8 | instrumentalness: float = Field(..., description="Here is the description of the input to be sent") 9 | liveness: float = Field(..., description="Here is the description of the input to be sent") 10 | speechiness: float = Field(..., description="Here is the description of the input to be sent") 11 | tempo: float = Field(..., description="Here is the description of the input to be sent") 12 | valence: float = Field(..., description="Here is the description of the input to be sent") 13 | class Config: 14 | schema_extra = { 15 | "example": { 16 | "acousticness": 0.344719513, 17 | "danceability": 0.758067547, 18 | "energy": 0.323318405, 19 | "instrumentalness": 0.0166768347, 20 | "liveness": 0.0856723112, 21 | "speechiness": 0.0306624283, 22 | "tempo": 101.993, 23 | "valence": 0.443876228 , 24 | } 25 | } 26 | --------------------------------------------------------------------------------