jggomez commited on
Commit
926e88f
·
verified ·
1 Parent(s): 5d0009c

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +30 -52
src/streamlit_app.py CHANGED
@@ -1,57 +1,39 @@
1
- import pickle
2
- import pandas as pd
3
  import streamlit as st
 
 
 
4
 
5
  st.set_page_config(layout="centered", page_title="Predict Music Genre")
6
 
7
  # DATA
8
  data = [
9
- {"key": "popularity", "label": "Popularity", "min": 1,
10
- "max": 100, "default": 44, "type": "number", "format": "%1f"},
11
- {"key": "danceability", "label": "Danceability", "min": 0.00,
12
- "max": 1.00, "default": 0.66, "type": "number"},
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
 
28
  response = [
29
- {"label": "Acoustic/Folk", "descripcion": "Acoustic/Folk music emphasizes unplugged instruments like guitars and violins. It often features narrative storytelling and heartfelt lyrics, drawing on traditional sounds and cultural roots. This genre evokes a sense of intimacy and authenticity, focusing on raw musical expression.",
30
- "image": "https://i.pinimg.com/736x/03/80/eb/0380eb05f1ce83ea0f429413f8daebef.jpg"},
31
- {"label": "Alternative Music", "descripcion": "Alternative music emerged from the indie underground, rejecting mainstream rock and pop conventions. It encompasses diverse styles, often characterized by unconventional song structures, experimental sounds, and emotionally charged lyrics. Alt_Music prioritizes artistic expression over commercial appeal.",
32
- "image": "https://i.pinimg.com/736x/87/68/3c/87683cd8c9dafe80b6bd927167efcbaa.jpg"},
33
- {"label": "Blues", "descripcion": "Blues is a foundational genre born from African-American spirituals, work songs, and chants. It's known for its melancholic themes, expressive vocal delivery, and distinctive 12-bar chord progressions. Blues music powerfully conveys feelings of hardship, resilience, and hope.",
34
- "image": "https://i.pinimg.com/736x/69/89/03/6989032f191d50fe98f76a3e7503e79a.jpg"},
35
- {"label": "Bollywood", "descripcion": "Bollywood music is the vibrant soundtrack of Indian cinema, a fusion of traditional Indian melodies with Western pop, folk, and classical influences. It's highly energetic, often featuring elaborate orchestral arrangements, rich vocals, and rhythmic dance numbers. Bollywood music is integral to the storytelling and emotional core of the films.",
36
- "image": "https://i.pinimg.com/736x/6d/d2/f1/6dd2f14df0e77a432df1cd9703a7d3f7.jpg"},
37
- {"label": "Country", "descripcion": "Country music originates from the American South and Southwest, blending folk, blues, and gospel traditions. It typically features themes of rural life, love, loss, and patriotism, often driven by instruments like guitars, banjos, and fiddles. Country music tells relatable stories with a down-to-earth sensibility.",
38
- "image": "https://i.pinimg.com/736x/58/a3/ad/58a3ad5a34925b1735d29c617e4e55d6.jpg"},
39
- {"label": "HipHop",
40
- "descripcion": "HipHop is a cultural movement originating in the Bronx, New York, known for its rhythmic spoken word (rapping) over sampled beats. It incorporates elements of DJing, breakdancing, and graffiti art, serving as a powerful medium for social commentary and storytelling. HipHop is diverse, influential, and constantly evolving.", "image": "https://i.pinimg.com/736x/0f/5e/6d/0f5e6d5466ac7e0b7a99a9aa4df88d89.jpg"},
41
- {"label": "Indie Alternative", "descripcion": "Indie Alternative music emphasizes independent production and distribution, fostering a unique sound distinct from major labels. It often combines raw, DIY aesthetics with experimental sounds and introspective lyrics. This genre celebrates artistic freedom and a non-conformist approach to music creation.",
42
- "image": "https://i.pinimg.com/736x/8c/10/6d/8c106dfea2b51acfad3eaa6d12b886d0.jpg"},
43
- {"label": "Instrumental", "descripcion": "Instrumental music focuses solely on musical instruments without the presence of vocals. It can span any genre, allowing for pure melodic and harmonic exploration. This genre allows listeners to interpret the music based on its sonic qualities and emotional resonance, often used for background or cinematic purposes.",
44
- "image": "https://i.pinimg.com/736x/ce/36/d7/ce36d70c2b71b931bceb7b744f871944.jpg"},
45
- {"label": "Metal", "descripcion": "Metal music is characterized by its aggressive, powerful sound, typically featuring distorted guitars, heavy drumming, and often intense vocals. Subgenres vary widely, but common themes include fantasy, mythology, and social issues. Metal is known for its strong subculture and energetic live performances.",
46
- "image": "https://i.pinimg.com/736x/bd/0c/dc/bd0cdc59569c0bd45876f80ad673ad70.jpg"},
47
- {"label": "Pop", "descripcion": "Pop music is a broad genre characterized by catchy melodies, simple song structures, and widespread appeal. It's designed for mass consumption, often incorporating elements from various genres and focusing on relatable themes like love and everyday life. Pop music is highly commercial and constantly evolving with trends.",
48
- "image": "https://i.pinimg.com/736x/f1/63/5e/f1635e44415b7eb86876ab0d181049b8.jpg"},
49
- {"label": "Rock", "descripcion": "Rock music emerged from blues and country, known for its prominent electric guitar, bass, drums, and often strong vocals. It's a diverse genre encompassing many subgenres, often exploring themes of rebellion, love, and social change. Rock music is celebrated for its energy and cultural impact.",
50
- "image": "https://i.pinimg.com/736x/37/e4/e1/37e4e1add7dbe95fb90b962d015edd26.jpg"},
51
  ]
52
 
53
- # LOGIC
54
- @st.cache_resource
55
  def load_model(path="./src/xgb_classifier_pipeline.pkl"):
56
  try:
57
  with open(path, "rb") as f:
@@ -72,7 +54,7 @@ prediction = -1
72
  datos_bool = []
73
 
74
 
75
- # VIEW
76
 
77
  # Custom CSS for styling
78
  st.markdown("""<style>
@@ -226,11 +208,9 @@ with col1:
226
 
227
  for data_point in data:
228
  if data_point["type"] == "number":
229
- input_data_original[data_point["key"]] = st.number_input(
230
- label=data_point["label"], min_value=data_point["min"], max_value=data_point["max"], value=data_point["default"])
231
  elif data_point["type"] == "bool":
232
- input_data_original[data_point["key"]] = st.checkbox(
233
- label=data_point["label"], value=data_point["default"])
234
  datos_bool.append(data_point["key"])
235
 
236
  if st.button("Predict Genre"):
@@ -239,15 +219,13 @@ with col1:
239
  input_data_original[key] = int(input_data_original[key])
240
 
241
  input_df_model = pd.DataFrame([input_data_original])
242
- input_df_model = pd.DataFrame(
243
- input_df_model, columns=input_df_model.columns)
244
  prediction = int(model.predict(input_df_model.head(1)))
245
  except Exception as e:
246
  st.error(f"An error occurred during prediction: {e}")
247
 
248
  with col2:
249
- st.markdown('<h2 class="predicted-genre-title">Predicted Genre</h2>',
250
- unsafe_allow_html=True)
251
 
252
  if prediction != -1:
253
  st.markdown(f"""
 
 
 
1
  import streamlit as st
2
+ import pandas as pd
3
+ import pickle
4
+ import numpy as np
5
 
6
  st.set_page_config(layout="centered", page_title="Predict Music Genre")
7
 
8
  # DATA
9
  data = [
10
+ {"key": "popularity", "label": "Popularity", "min": 1, "max": 100, "default": 44, "type": "number", "format": "%1f" },
11
+ {"key": "danceability", "label": "Danceability", "min": 0.00, "max": 1.00, "default": 0.66, "type": "number" },
12
+ {"key": "mode", "label": "Mode", "default": 0, "type": "bool" },
13
+ {"key": "valence", "label": "Valence", "min": 0.00, "max": 1.00, "default": 0.48, "type": "number" },
14
+ {"key": "speechiness", "label": "Speechiness", "min": 0.00, "max": 1.00, "default": 0.79, "type": "number" },
15
+ {"key": "acousticness", "label": "Acousticness", "min": 0.00, "max": 1.00, "default": 0.34, "type": "number" },
16
+ {"key": "liveness", "label": "Liveness", "min": 0.00, "max": 1.00, "default": 0.19, "type": "number" },
17
+ {"key": "instrumentalness", "label": "Instrumentalness", "min": 0.00, "max": 1.00, "default": 0.17, "type": "number" },
18
+ {"key": "duration_in_min_sg", "label": "Duration in seg", "min": 0, "max": 1000000, "default": 317, "type": "number" },
 
 
 
 
 
 
 
 
19
  ]
20
 
21
  response = [
22
+ {"label": "Acoustic/Folk", "descripcion": "Acoustic/Folk music emphasizes unplugged instruments like guitars and violins. It often features narrative storytelling and heartfelt lyrics, drawing on traditional sounds and cultural roots. This genre evokes a sense of intimacy and authenticity, focusing on raw musical expression.", "image": "https://i.pinimg.com/736x/03/80/eb/0380eb05f1ce83ea0f429413f8daebef.jpg"},
23
+ {"label": "Alternative Music", "descripcion": "Alternative music emerged from the indie underground, rejecting mainstream rock and pop conventions. It encompasses diverse styles, often characterized by unconventional song structures, experimental sounds, and emotionally charged lyrics. Alt_Music prioritizes artistic expression over commercial appeal.", "image": "https://i.pinimg.com/736x/87/68/3c/87683cd8c9dafe80b6bd927167efcbaa.jpg"},
24
+ {"label": "Blues", "descripcion": "Blues is a foundational genre born from African-American spirituals, work songs, and chants. It's known for its melancholic themes, expressive vocal delivery, and distinctive 12-bar chord progressions. Blues music powerfully conveys feelings of hardship, resilience, and hope.", "image": "https://i.pinimg.com/736x/69/89/03/6989032f191d50fe98f76a3e7503e79a.jpg"},
25
+ {"label": "Bollywood", "descripcion": "Bollywood music is the vibrant soundtrack of Indian cinema, a fusion of traditional Indian melodies with Western pop, folk, and classical influences. It's highly energetic, often featuring elaborate orchestral arrangements, rich vocals, and rhythmic dance numbers. Bollywood music is integral to the storytelling and emotional core of the films.", "image": "https://i.pinimg.com/736x/6d/d2/f1/6dd2f14df0e77a432df1cd9703a7d3f7.jpg"},
26
+ {"label": "Country", "descripcion": "Country music originates from the American South and Southwest, blending folk, blues, and gospel traditions. It typically features themes of rural life, love, loss, and patriotism, often driven by instruments like guitars, banjos, and fiddles. Country music tells relatable stories with a down-to-earth sensibility.", "image": "https://i.pinimg.com/736x/58/a3/ad/58a3ad5a34925b1735d29c617e4e55d6.jpg"},
27
+ {"label": "HipHop", "descripcion": "HipHop is a cultural movement originating in the Bronx, New York, known for its rhythmic spoken word (rapping) over sampled beats. It incorporates elements of DJing, breakdancing, and graffiti art, serving as a powerful medium for social commentary and storytelling. HipHop is diverse, influential, and constantly evolving.", "image": "https://i.pinimg.com/736x/0f/5e/6d/0f5e6d5466ac7e0b7a99a9aa4df88d89.jpg"},
28
+ {"label": "Indie Alternative", "descripcion": "Indie Alternative music emphasizes independent production and distribution, fostering a unique sound distinct from major labels. It often combines raw, DIY aesthetics with experimental sounds and introspective lyrics. This genre celebrates artistic freedom and a non-conformist approach to music creation.", "image": "https://i.pinimg.com/736x/8c/10/6d/8c106dfea2b51acfad3eaa6d12b886d0.jpg"},
29
+ {"label": "Instrumental", "descripcion": "Instrumental music focuses solely on musical instruments without the presence of vocals. It can span any genre, allowing for pure melodic and harmonic exploration. This genre allows listeners to interpret the music based on its sonic qualities and emotional resonance, often used for background or cinematic purposes.", "image": "https://i.pinimg.com/736x/ce/36/d7/ce36d70c2b71b931bceb7b744f871944.jpg"},
30
+ {"label": "Metal", "descripcion": "Metal music is characterized by its aggressive, powerful sound, typically featuring distorted guitars, heavy drumming, and often intense vocals. Subgenres vary widely, but common themes include fantasy, mythology, and social issues. Metal is known for its strong subculture and energetic live performances.", "image": "https://i.pinimg.com/736x/bd/0c/dc/bd0cdc59569c0bd45876f80ad673ad70.jpg"},
31
+ {"label": "Pop", "descripcion": "Pop music is a broad genre characterized by catchy melodies, simple song structures, and widespread appeal. It's designed for mass consumption, often incorporating elements from various genres and focusing on relatable themes like love and everyday life. Pop music is highly commercial and constantly evolving with trends.", "image": "https://i.pinimg.com/736x/f1/63/5e/f1635e44415b7eb86876ab0d181049b8.jpg"},
32
+ {"label": "Rock", "descripcion": "Rock music emerged from blues and country, known for its prominent electric guitar, bass, drums, and often strong vocals. It's a diverse genre encompassing many subgenres, often exploring themes of rebellion, love, and social change. Rock music is celebrated for its energy and cultural impact.", "image": "https://i.pinimg.com/736x/37/e4/e1/37e4e1add7dbe95fb90b962d015edd26.jpg"},
 
 
 
 
 
 
 
 
 
 
 
33
  ]
34
 
35
+ ## LOGIC
36
+ #@st.cache_resource
37
  def load_model(path="./src/xgb_classifier_pipeline.pkl"):
38
  try:
39
  with open(path, "rb") as f:
 
54
  datos_bool = []
55
 
56
 
57
+ ### VIEW
58
 
59
  # Custom CSS for styling
60
  st.markdown("""<style>
 
208
 
209
  for data_point in data:
210
  if data_point["type"] == "number":
211
+ input_data_original[data_point["key"]] = st.number_input(label=data_point["label"], min_value=data_point["min"], max_value=data_point["max"], value=data_point["default"])
 
212
  elif data_point["type"] == "bool":
213
+ input_data_original[data_point["key"]] = st.checkbox(label=data_point["label"], value=data_point["default"])
 
214
  datos_bool.append(data_point["key"])
215
 
216
  if st.button("Predict Genre"):
 
219
  input_data_original[key] = int(input_data_original[key])
220
 
221
  input_df_model = pd.DataFrame([input_data_original])
222
+ input_df_model = pd.DataFrame(input_df_model, columns=input_df_model.columns)
 
223
  prediction = int(model.predict(input_df_model.head(1)))
224
  except Exception as e:
225
  st.error(f"An error occurred during prediction: {e}")
226
 
227
  with col2:
228
+ st.markdown('<h2 class="predicted-genre-title">Predicted Genre</h2>', unsafe_allow_html=True)
 
229
 
230
  if prediction != -1:
231
  st.markdown(f"""