Last active
February 4, 2021 19:13
-
-
Save kaustubhgupta/f84245a229e0f8edc817cc4da5ee8c92 to your computer and use it in GitHub Desktop.
ColabCode.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "ColabCode.ipynb", | |
"provenance": [], | |
"authorship_tag": "ABX9TyNVk2hgfgkfwrrZqaIyCsBf", | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/kaustubhgupta/f84245a229e0f8edc817cc4da5ee8c92/colabcode.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "eTuRuP6XSwJm" | |
}, | |
"source": [ | |
"!pip install colabcode" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "mFibHFld8nA_" | |
}, | |
"source": [ | |
"!pip install fastapi" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "hIsVdcqqKpJn" | |
}, | |
"source": [ | |
"from sklearn.tree import DecisionTreeClassifier\r\n", | |
"from sklearn.model_selection import train_test_split\r\n", | |
"import pandas as pd" | |
], | |
"execution_count": 30, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "Q2VfX1v4KuRW" | |
}, | |
"source": [ | |
"df = pd.read_csv(\"https://raw.githubusercontent.com/kaustubhgupta/Technocolab-Final-Project/master/Data/cleaned.csv\", index_col=None)" | |
], | |
"execution_count": 31, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 195 | |
}, | |
"id": "IeqAHgH6K5IZ", | |
"outputId": "2fee2afe-b089-43b0-d297-6c16917b0e4c" | |
}, | |
"source": [ | |
"df.head()" | |
], | |
"execution_count": 32, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/html": [ | |
"<div>\n", | |
"<style scoped>\n", | |
" .dataframe tbody tr th:only-of-type {\n", | |
" vertical-align: middle;\n", | |
" }\n", | |
"\n", | |
" .dataframe tbody tr th {\n", | |
" vertical-align: top;\n", | |
" }\n", | |
"\n", | |
" .dataframe thead th {\n", | |
" text-align: right;\n", | |
" }\n", | |
"</style>\n", | |
"<table border=\"1\" class=\"dataframe\">\n", | |
" <thead>\n", | |
" <tr style=\"text-align: right;\">\n", | |
" <th></th>\n", | |
" <th>track_id</th>\n", | |
" <th>genre_top</th>\n", | |
" <th>acousticness</th>\n", | |
" <th>danceability</th>\n", | |
" <th>energy</th>\n", | |
" <th>instrumentalness</th>\n", | |
" <th>liveness</th>\n", | |
" <th>speechiness</th>\n", | |
" <th>tempo</th>\n", | |
" <th>valence</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>0</th>\n", | |
" <td>153</td>\n", | |
" <td>Rock</td>\n", | |
" <td>0.988306</td>\n", | |
" <td>0.255661</td>\n", | |
" <td>0.979774</td>\n", | |
" <td>0.973006</td>\n", | |
" <td>0.121342</td>\n", | |
" <td>0.051740</td>\n", | |
" <td>90.241</td>\n", | |
" <td>0.034018</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>1</th>\n", | |
" <td>154</td>\n", | |
" <td>Rock</td>\n", | |
" <td>0.970135</td>\n", | |
" <td>0.352946</td>\n", | |
" <td>0.023852</td>\n", | |
" <td>0.957113</td>\n", | |
" <td>0.113261</td>\n", | |
" <td>0.032177</td>\n", | |
" <td>53.758</td>\n", | |
" <td>0.035632</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>2</th>\n", | |
" <td>155</td>\n", | |
" <td>Rock</td>\n", | |
" <td>0.981657</td>\n", | |
" <td>0.142249</td>\n", | |
" <td>0.912122</td>\n", | |
" <td>0.967294</td>\n", | |
" <td>0.363510</td>\n", | |
" <td>0.087527</td>\n", | |
" <td>91.912</td>\n", | |
" <td>0.034325</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>3</th>\n", | |
" <td>169</td>\n", | |
" <td>Rock</td>\n", | |
" <td>0.989141</td>\n", | |
" <td>0.225978</td>\n", | |
" <td>0.722835</td>\n", | |
" <td>0.263076</td>\n", | |
" <td>0.092371</td>\n", | |
" <td>0.053406</td>\n", | |
" <td>94.322</td>\n", | |
" <td>0.028347</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>4</th>\n", | |
" <td>170</td>\n", | |
" <td>Rock</td>\n", | |
" <td>0.886660</td>\n", | |
" <td>0.298518</td>\n", | |
" <td>0.744333</td>\n", | |
" <td>0.920950</td>\n", | |
" <td>0.139587</td>\n", | |
" <td>0.088781</td>\n", | |
" <td>97.880</td>\n", | |
" <td>0.073548</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"</div>" | |
], | |
"text/plain": [ | |
" track_id genre_top acousticness ... speechiness tempo valence\n", | |
"0 153 Rock 0.988306 ... 0.051740 90.241 0.034018\n", | |
"1 154 Rock 0.970135 ... 0.032177 53.758 0.035632\n", | |
"2 155 Rock 0.981657 ... 0.087527 91.912 0.034325\n", | |
"3 169 Rock 0.989141 ... 0.053406 94.322 0.028347\n", | |
"4 170 Rock 0.886660 ... 0.088781 97.880 0.073548\n", | |
"\n", | |
"[5 rows x 10 columns]" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 32 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "TE-wYBYdK7ch" | |
}, | |
"source": [ | |
"X = df.drop(['track_id', 'genre_top'], axis=1)\r\n", | |
"y = df.genre_top" | |
], | |
"execution_count": 33, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "4bD2cxaTJ91j" | |
}, | |
"source": [ | |
"X_train, X_test, y_train, y_test = train_test_split(X,y,stratify=y)\r\n", | |
"\r\n", | |
"tree = DecisionTreeClassifier(random_state=42)\r\n", | |
"\r\n", | |
"tree.fit(X_train, y_train)\r\n", | |
"\r\n", | |
"Pkl_Filename = \"model_tree.pkl\" \r\n", | |
"\r\n", | |
"with open(Pkl_Filename, 'wb') as file: \r\n", | |
" pickle.dump(tree, file)" | |
], | |
"execution_count": 34, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "dDBi8zc7JhrM" | |
}, | |
"source": [ | |
"from pydantic import BaseModel\r\n", | |
"\r\n", | |
"class Music(BaseModel):\r\n", | |
" acousticness: float \r\n", | |
" danceability: float \r\n", | |
" energy: float \r\n", | |
" instrumentalness: float \r\n", | |
" liveness: float \r\n", | |
" speechiness: float \r\n", | |
" tempo: float \r\n", | |
" valence: float\r\n", | |
" class Config:\r\n", | |
" schema_extra = {\r\n", | |
" \"example\": {\r\n", | |
" \"acousticness\": 0.838816, \r\n", | |
" \"danceability\": 0.542950, \r\n", | |
" \"energy\": 0.669215,\r\n", | |
" \"instrumentalness\": 0.000006,\r\n", | |
" \"liveness\": 0.105610,\r\n", | |
" \"speechiness\": 0.391221,\r\n", | |
" \"tempo\": 111.894,\r\n", | |
" \"valence\": 0.796073\r\n", | |
" }\r\n", | |
" }" | |
], | |
"execution_count": 39, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "K8BjvQWz8u4J" | |
}, | |
"source": [ | |
"from fastapi import FastAPI\r\n", | |
"import pickle\r\n", | |
"\r\n", | |
"app = FastAPI()\r\n", | |
"\r\n", | |
"@app.on_event(\"startup\")\r\n", | |
"def load_model():\r\n", | |
" global model\r\n", | |
" model = pickle.load(open(\"model_tree.pkl\", \"rb\"))\r\n", | |
"\r\n", | |
"@app.get('/')\r\n", | |
"def index():\r\n", | |
" return {'message': 'This is the homepage of the API '}\r\n", | |
"\r\n", | |
"\r\n", | |
"@app.post('/predict')\r\n", | |
"def get_music_category(data: Music):\r\n", | |
" received = data.dict()\r\n", | |
" acousticness = received['acousticness']\r\n", | |
" danceability = received['danceability']\r\n", | |
" energy = received['energy']\r\n", | |
" instrumentalness = received['instrumentalness']\r\n", | |
" liveness = received['liveness']\r\n", | |
" speechiness = received['speechiness']\r\n", | |
" tempo = received['tempo']\r\n", | |
" valence = received['valence']\r\n", | |
" pred_name = model.predict([[acousticness, danceability, energy,\r\n", | |
" instrumentalness, liveness, speechiness, tempo, valence]]).tolist()[0]\r\n", | |
" return {'prediction': pred_name}" | |
], | |
"execution_count": 40, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "CaMyhg1r9suW" | |
}, | |
"source": [ | |
"from colabcode import ColabCode\r\n", | |
"server = ColabCode(port=10000, code=False)" | |
], | |
"execution_count": 41, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "Tnbfrz1oFYfB" | |
}, | |
"source": [ | |
"server.run_app(app=app)" | |
], | |
"execution_count": null, | |
"outputs": [] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment