Spaces:
Sleeping
Sleeping
Update model.py
Browse files
model.py
CHANGED
@@ -1,130 +1,113 @@
|
|
1 |
-
import pandas as pd
|
2 |
-
import numpy as np
|
3 |
-
from sklearn.decomposition import TruncatedSVD
|
4 |
-
import time
|
5 |
-
import gradio as gr
|
6 |
-
from scipy.sparse import csr_matrix
|
7 |
-
|
8 |
-
class MatrixFactorization:
|
9 |
-
def __init__(self, n_factors=
|
10 |
-
self.n_factors = n_factors
|
11 |
-
self.model = TruncatedSVD(n_components=n_factors, random_state=42)
|
12 |
-
self.user_title_matrix = None
|
13 |
-
self.titles_df = None
|
14 |
-
self.column_names = None
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
'
|
34 |
-
'
|
35 |
-
'
|
36 |
-
|
37 |
-
|
38 |
-
self.
|
39 |
-
self.
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
row
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
with gr.Column():
|
115 |
-
recommend_btn = gr.Button("Get Recommendations", size="lg")
|
116 |
-
output_table = gr.DataFrame(
|
117 |
-
headers=["Song", "Artist", "Year", "Confidence"],
|
118 |
-
label="Recommended Songs"
|
119 |
-
)
|
120 |
-
|
121 |
-
recommend_btn.click(
|
122 |
-
fn=mf_model.get_recommendations_from_titles,
|
123 |
-
inputs=input_songs,
|
124 |
-
outputs=output_table
|
125 |
-
)
|
126 |
-
|
127 |
-
return demo
|
128 |
-
except Exception as e:
|
129 |
-
print(f"Error creating interface: {str(e)}")
|
130 |
return None
|
|
|
1 |
+
import pandas as pd
|
2 |
+
import numpy as np
|
3 |
+
from sklearn.decomposition import TruncatedSVD
|
4 |
+
import time
|
5 |
+
import gradio as gr
|
6 |
+
from scipy.sparse import csr_matrix
|
7 |
+
|
8 |
+
class MatrixFactorization:
|
9 |
+
def __init__(self, n_factors=50): # Reduced factors
|
10 |
+
self.n_factors = n_factors
|
11 |
+
self.model = TruncatedSVD(n_components=n_factors, random_state=42)
|
12 |
+
self.user_title_matrix = None
|
13 |
+
self.titles_df = None
|
14 |
+
self.column_names = None
|
15 |
+
self._cached_choices = None
|
16 |
+
|
17 |
+
def fit(self, df):
|
18 |
+
print("Training model...")
|
19 |
+
start_time = time.time()
|
20 |
+
|
21 |
+
# Get top songs by play count
|
22 |
+
top_songs = (df.groupby('title')['play_count']
|
23 |
+
.sum()
|
24 |
+
.sort_values(ascending=False)
|
25 |
+
.head(10000)
|
26 |
+
.index)
|
27 |
+
|
28 |
+
df_filtered = df[df['title'].isin(top_songs)]
|
29 |
+
print(f"Filtered to {len(top_songs)} most played songs")
|
30 |
+
|
31 |
+
pivot = pd.pivot_table(
|
32 |
+
df_filtered,
|
33 |
+
values='play_count',
|
34 |
+
index='user',
|
35 |
+
columns='title',
|
36 |
+
fill_value=0
|
37 |
+
)
|
38 |
+
self.column_names = pivot.columns
|
39 |
+
self.user_title_matrix = csr_matrix(pivot.values)
|
40 |
+
|
41 |
+
self.titles_df = df_filtered.groupby('title').agg({
|
42 |
+
'artist_name': 'first',
|
43 |
+
'year': 'first',
|
44 |
+
'play_count': 'sum',
|
45 |
+
'release': 'first'
|
46 |
+
})
|
47 |
+
|
48 |
+
print("Training SVD model...")
|
49 |
+
self.user_vectors = self.model.fit_transform(self.user_title_matrix)
|
50 |
+
self.item_vectors = self.model.components_
|
51 |
+
|
52 |
+
# Pre-cache choices
|
53 |
+
self._cached_choices = self._generate_choices()
|
54 |
+
|
55 |
+
print(f"Training completed in {time.time() - start_time:.2f} seconds")
|
56 |
+
|
57 |
+
def _generate_choices(self):
|
58 |
+
choices = []
|
59 |
+
for title, row in self.titles_df.iterrows():
|
60 |
+
display_text = f"{title} β’ by {row['artist_name']}"
|
61 |
+
extra_info = []
|
62 |
+
if pd.notna(row['release']):
|
63 |
+
extra_info.append(row['release'])
|
64 |
+
if pd.notna(row['year']):
|
65 |
+
extra_info.append(str(int(row['year'])))
|
66 |
+
if extra_info:
|
67 |
+
display_text += f" [{', '.join(extra_info)}]"
|
68 |
+
choices.append(display_text)
|
69 |
+
return sorted(choices)
|
70 |
+
|
71 |
+
def create_title_choices(self):
|
72 |
+
return self._cached_choices if self._cached_choices else self._generate_choices()
|
73 |
+
|
74 |
+
def create_gradio_interface(mf_model):
|
75 |
+
try:
|
76 |
+
with gr.Blocks() as demo:
|
77 |
+
gr.Markdown("""# π΅ Music Recommendation System πΆ
|
78 |
+
|
79 |
+
### Instructions:
|
80 |
+
1. β³ Loading ~10,000 most popular songs
|
81 |
+
2. π Search by title, artist, album, or year
|
82 |
+
3. π§ Select up to 5 songs
|
83 |
+
4. π Click for recommendations
|
84 |
+
5. π View confidence scores (30-100%)
|
85 |
+
""")
|
86 |
+
|
87 |
+
with gr.Row():
|
88 |
+
input_songs = gr.Dropdown(
|
89 |
+
choices=mf_model.create_title_choices(),
|
90 |
+
label="Search and select songs (up to 5)",
|
91 |
+
info="Format: Title β’ by Artist [Album, Year]",
|
92 |
+
multiselect=True,
|
93 |
+
max_choices=5,
|
94 |
+
filterable=True
|
95 |
+
)
|
96 |
+
|
97 |
+
with gr.Column():
|
98 |
+
recommend_btn = gr.Button("Get Recommendations", size="lg")
|
99 |
+
output_table = gr.DataFrame(
|
100 |
+
headers=["Song", "Artist", "Year", "Confidence"],
|
101 |
+
label="Recommended Songs"
|
102 |
+
)
|
103 |
+
|
104 |
+
recommend_btn.click(
|
105 |
+
fn=mf_model.get_recommendations_from_titles,
|
106 |
+
inputs=input_songs,
|
107 |
+
outputs=output_table
|
108 |
+
)
|
109 |
+
|
110 |
+
return demo
|
111 |
+
except Exception as e:
|
112 |
+
print(f"Error creating interface: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
113 |
return None
|