Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- src/streamlit_app.py +35 -53
- src/xgb_classifier_pipeline.pkl +3 -0
src/streamlit_app.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
import numpy as np
|
| 2 |
import pickle
|
| 3 |
import pandas as pd
|
| 4 |
import streamlit as st
|
|
@@ -14,15 +13,15 @@ data = [
|
|
| 14 |
{"key": "mode", "label": "Mode", "default": 0, "type": "bool"},
|
| 15 |
{"key": "valence", "label": "Valence", "min": 0.00,
|
| 16 |
"max": 1.00, "default": 0.48, "type": "number"},
|
| 17 |
-
{"key": "
|
| 18 |
"max": 1.00, "default": 0.79, "type": "number"},
|
| 19 |
-
{"key": "
|
| 20 |
-
"
|
| 21 |
-
{"key": "
|
| 22 |
"max": 1.00, "default": 0.19, "type": "number"},
|
| 23 |
-
{"key": "
|
| 24 |
"min": 0.00, "max": 1.00, "default": 0.17, "type": "number"},
|
| 25 |
-
{"key": "
|
| 26 |
"min": 0, "max": 1000000, "default": 317, "type": "number"},
|
| 27 |
]
|
| 28 |
|
|
@@ -52,26 +51,14 @@ response = [
|
|
| 52 |
]
|
| 53 |
|
| 54 |
# LOGIC
|
| 55 |
-
@st.cache_resource
|
| 56 |
-
def load_model(path="./src/xgb_model.pkl"):
|
| 57 |
-
try:
|
| 58 |
-
with open(path, "rb") as f:
|
| 59 |
-
model = pickle.load(f)
|
| 60 |
-
return model
|
| 61 |
-
except FileNotFoundError:
|
| 62 |
-
st.error(
|
| 63 |
-
f"Error: Model file not found at {path}. Please ensure the model file is in the directory.")
|
| 64 |
-
return None
|
| 65 |
-
except Exception as e:
|
| 66 |
-
st.error(f"An error occurred while loading the model: {e}")
|
| 67 |
-
return None
|
| 68 |
|
| 69 |
|
| 70 |
-
|
| 71 |
-
def load_model_scaler(path="./src/scaler.pkl"):
|
| 72 |
try:
|
| 73 |
with open(path, "rb") as f:
|
| 74 |
-
|
|
|
|
| 75 |
except FileNotFoundError:
|
| 76 |
st.error(
|
| 77 |
f"Error: Model file not found at {path}. Please ensure the model file is in the directory.")
|
|
@@ -82,13 +69,13 @@ def load_model_scaler(path="./src/scaler.pkl"):
|
|
| 82 |
|
| 83 |
|
| 84 |
model = load_model()
|
| 85 |
-
scaler = load_model_scaler()
|
| 86 |
input_data_original = {}
|
| 87 |
prediction = -1
|
| 88 |
datos_bool = []
|
| 89 |
|
| 90 |
|
| 91 |
# VIEW
|
|
|
|
| 92 |
# Custom CSS for styling
|
| 93 |
st.markdown("""<style>
|
| 94 |
.main {
|
|
@@ -240,43 +227,38 @@ with col1:
|
|
| 240 |
st.markdown('<h1>Predict Music Genre</h1>', unsafe_allow_html=True)
|
| 241 |
|
| 242 |
for data_point in data:
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
|
| 251 |
if st.button("Predict Genre"):
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
|
|
|
|
|
|
| 260 |
|
| 261 |
-
input_df_model = pd.DataFrame([input_data_original])
|
| 262 |
-
input_df_scaled = scaler.transform(input_df_model)
|
| 263 |
-
input_df_model = pd.DataFrame(
|
| 264 |
-
input_df_scaled, columns=input_df_model.columns)
|
| 265 |
-
prediction = int(model.predict(input_df_model)[0])
|
| 266 |
-
except Exception as e:
|
| 267 |
-
st.error(f"An error occurred during prediction: {e}")
|
| 268 |
with col2:
|
| 269 |
-
|
| 270 |
-
|
| 271 |
|
| 272 |
-
|
| 273 |
-
|
| 274 |
<div class="fixed-height-image-container">
|
| 275 |
<img src="{response[prediction]["image"]}" alt="Imagen grande">
|
| 276 |
</div>
|
| 277 |
""", unsafe_allow_html=True)
|
| 278 |
|
| 279 |
-
|
| 280 |
<div class="genre-box">
|
| 281 |
<h3>{response[prediction]["label"]}</h3>
|
| 282 |
<p>{response[prediction]["descripcion"]}</p>
|
|
@@ -288,8 +270,8 @@ with col2:
|
|
| 288 |
<p>Pop: [Popularity: 79, Danceability: 0.80, Mode: False, Valance: 0.05, Speechines: 0.79, Acousticness: 0.48, Liveness: 0.03, Instrumentalness: 0.61, Duration in seg: 352]</p>
|
| 289 |
</div>
|
| 290 |
""", unsafe_allow_html=True)
|
| 291 |
-
|
| 292 |
-
|
| 293 |
<div class="genre-box max-box">
|
| 294 |
<h3>Select values and predict genre</h3>
|
| 295 |
<h3>Ejemplos</h3>
|
|
@@ -299,4 +281,4 @@ with col2:
|
|
| 299 |
<p>Alternative: [Popularity: 44, Danceability: 0.66, Mode: False, Valance: 0.48, Speechines: 0.79, Acousticness: 0.34, Liveness: 0.19, Instrumentalness: 0.17, Duration in seg: 317]</p>
|
| 300 |
<p>Pop: [Popularity: 79, Danceability: 0.80, Mode: False, Valance: 0.05, Speechines: 0.79, Acousticness: 0.48, Liveness: 0.03, Instrumentalness: 0.61, Duration in seg: 352]</p>
|
| 301 |
</div>
|
| 302 |
-
""", unsafe_allow_html=True)
|
|
|
|
|
|
|
| 1 |
import pickle
|
| 2 |
import pandas as pd
|
| 3 |
import streamlit as st
|
|
|
|
| 13 |
{"key": "mode", "label": "Mode", "default": 0, "type": "bool"},
|
| 14 |
{"key": "valence", "label": "Valence", "min": 0.00,
|
| 15 |
"max": 1.00, "default": 0.48, "type": "number"},
|
| 16 |
+
{"key": "speechiness", "label": "Speechiness", "min": 0.00,
|
| 17 |
"max": 1.00, "default": 0.79, "type": "number"},
|
| 18 |
+
{"key": "acousticness", "label": "Acousticness", "min": 0.00,
|
| 19 |
+
"max": 1.00, "default": 0.34, "type": "number"},
|
| 20 |
+
{"key": "liveness", "label": "Liveness", "min": 0.00,
|
| 21 |
"max": 1.00, "default": 0.19, "type": "number"},
|
| 22 |
+
{"key": "instrumentalness", "label": "Instrumentalness",
|
| 23 |
"min": 0.00, "max": 1.00, "default": 0.17, "type": "number"},
|
| 24 |
+
{"key": "duration_in_min_sg", "label": "Duration in seg",
|
| 25 |
"min": 0, "max": 1000000, "default": 317, "type": "number"},
|
| 26 |
]
|
| 27 |
|
|
|
|
| 51 |
]
|
| 52 |
|
| 53 |
# LOGIC
|
| 54 |
+
# @st.cache_resource
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
|
| 57 |
+
def load_model(path="xgb_classifier_pipeline.pkl"):
|
|
|
|
| 58 |
try:
|
| 59 |
with open(path, "rb") as f:
|
| 60 |
+
model = pickle.load(f)
|
| 61 |
+
return model
|
| 62 |
except FileNotFoundError:
|
| 63 |
st.error(
|
| 64 |
f"Error: Model file not found at {path}. Please ensure the model file is in the directory.")
|
|
|
|
| 69 |
|
| 70 |
|
| 71 |
model = load_model()
|
|
|
|
| 72 |
input_data_original = {}
|
| 73 |
prediction = -1
|
| 74 |
datos_bool = []
|
| 75 |
|
| 76 |
|
| 77 |
# VIEW
|
| 78 |
+
|
| 79 |
# Custom CSS for styling
|
| 80 |
st.markdown("""<style>
|
| 81 |
.main {
|
|
|
|
| 227 |
st.markdown('<h1>Predict Music Genre</h1>', unsafe_allow_html=True)
|
| 228 |
|
| 229 |
for data_point in data:
|
| 230 |
+
if data_point["type"] == "number":
|
| 231 |
+
input_data_original[data_point["key"]] = st.number_input(
|
| 232 |
+
label=data_point["label"], min_value=data_point["min"], max_value=data_point["max"], value=data_point["default"])
|
| 233 |
+
elif data_point["type"] == "bool":
|
| 234 |
+
input_data_original[data_point["key"]] = st.checkbox(
|
| 235 |
+
label=data_point["label"], value=data_point["default"])
|
| 236 |
+
datos_bool.append(data_point["key"])
|
| 237 |
|
| 238 |
if st.button("Predict Genre"):
|
| 239 |
+
try:
|
| 240 |
+
for key in datos_bool:
|
| 241 |
+
input_data_original[key] = int(input_data_original[key])
|
| 242 |
|
| 243 |
+
input_df_model = pd.DataFrame([input_data_original])
|
| 244 |
+
input_df_model = pd.DataFrame(
|
| 245 |
+
input_df_model, columns=input_df_model.columns)
|
| 246 |
+
prediction = int(model.predict(input_df_model.head(1)))
|
| 247 |
+
except Exception as e:
|
| 248 |
+
st.error(f"An error occurred during prediction: {e}")
|
| 249 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 250 |
with col2:
|
| 251 |
+
st.markdown('<h2 class="predicted-genre-title">Predicted Genre</h2>',
|
| 252 |
+
unsafe_allow_html=True)
|
| 253 |
|
| 254 |
+
if prediction != -1:
|
| 255 |
+
st.markdown(f"""
|
| 256 |
<div class="fixed-height-image-container">
|
| 257 |
<img src="{response[prediction]["image"]}" alt="Imagen grande">
|
| 258 |
</div>
|
| 259 |
""", unsafe_allow_html=True)
|
| 260 |
|
| 261 |
+
st.markdown(f"""
|
| 262 |
<div class="genre-box">
|
| 263 |
<h3>{response[prediction]["label"]}</h3>
|
| 264 |
<p>{response[prediction]["descripcion"]}</p>
|
|
|
|
| 270 |
<p>Pop: [Popularity: 79, Danceability: 0.80, Mode: False, Valance: 0.05, Speechines: 0.79, Acousticness: 0.48, Liveness: 0.03, Instrumentalness: 0.61, Duration in seg: 352]</p>
|
| 271 |
</div>
|
| 272 |
""", unsafe_allow_html=True)
|
| 273 |
+
else:
|
| 274 |
+
st.markdown("""
|
| 275 |
<div class="genre-box max-box">
|
| 276 |
<h3>Select values and predict genre</h3>
|
| 277 |
<h3>Ejemplos</h3>
|
|
|
|
| 281 |
<p>Alternative: [Popularity: 44, Danceability: 0.66, Mode: False, Valance: 0.48, Speechines: 0.79, Acousticness: 0.34, Liveness: 0.19, Instrumentalness: 0.17, Duration in seg: 317]</p>
|
| 282 |
<p>Pop: [Popularity: 79, Danceability: 0.80, Mode: False, Valance: 0.05, Speechines: 0.79, Acousticness: 0.48, Liveness: 0.03, Instrumentalness: 0.61, Duration in seg: 352]</p>
|
| 283 |
</div>
|
| 284 |
+
""", unsafe_allow_html=True)
|
src/xgb_classifier_pipeline.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:bd68dbec42d6325bc5ad316a818a307458f2c64242eee05ff327cf8b3a2dc2d9
|
| 3 |
+
size 10442977
|