Spaces:
Running
Running
Update song_matching.py
Browse files- song_matching.py +19 -20
song_matching.py
CHANGED
@@ -11,29 +11,28 @@ class SongMatcher:
|
|
11 |
self.songs_df = pd.read_csv(songs_data_file)
|
12 |
self.sim_model = SentenceTransformer(model_name)
|
13 |
|
14 |
-
def match_songs_with_sentiment(
|
15 |
-
|
16 |
-
Matches songs from the dataset with the user's sentiment.
|
17 |
-
:param user_sentiment_label: The sentiment label of the user input
|
18 |
-
:param user_sentiment_score: The sentiment score of the user input
|
19 |
-
:param user_input: Text input from the user
|
20 |
-
:param score_range: Range for filtering songs based on sentiment score
|
21 |
-
:return: DataFrame of top 5 matched songs
|
22 |
-
"""
|
23 |
# Filter songs with the same sentiment label
|
24 |
-
matched_songs =
|
25 |
-
|
26 |
# Calculate the score range
|
27 |
score_min = max(0, user_sentiment_score - score_range)
|
28 |
score_max = min(1, user_sentiment_score + score_range)
|
29 |
-
|
30 |
# Further filter songs whose scores fall within the specified range
|
31 |
matched_songs = matched_songs[(matched_songs['score'] >= score_min) & (matched_songs['score'] <= score_max)]
|
32 |
-
|
33 |
-
#
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
top_5 = matched_songs
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
self.songs_df = pd.read_csv(songs_data_file)
|
12 |
self.sim_model = SentenceTransformer(model_name)
|
13 |
|
14 |
+
def match_songs_with_sentiment(user_sentiment_label, user_sentiment_score,inputVector, score_range,songs_df):
|
15 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
# Filter songs with the same sentiment label
|
17 |
+
matched_songs = songs_df[songs_df['sentiment'] == user_sentiment_label]
|
18 |
+
|
19 |
# Calculate the score range
|
20 |
score_min = max(0, user_sentiment_score - score_range)
|
21 |
score_max = min(1, user_sentiment_score + score_range)
|
22 |
+
|
23 |
# Further filter songs whose scores fall within the specified range
|
24 |
matched_songs = matched_songs[(matched_songs['score'] >= score_min) & (matched_songs['score'] <= score_max)]
|
25 |
+
|
26 |
+
# Shuffle the matched songs to get a random order
|
27 |
+
matched_songs = matched_songs.sample(frac=1).reset_index(drop=True)
|
28 |
+
|
29 |
+
matched_songs['similarity'] = matched_songs['seq'].apply(lambda x: util.pytorch_cos_sim(sim_model.encode(x), inputVector))
|
30 |
+
|
31 |
+
top_5 = matched_songs['similarity'].sort_values(ascending=False).head(5)
|
32 |
+
|
33 |
+
# Sort the songs by how close their score is to the user's sentiment score
|
34 |
+
# matched_songs['score_diff'] = abs(matched_songs['score'] - user_sentiment_score)
|
35 |
+
# matched_songs = matched_songs.sort_values(by='score_diff')
|
36 |
+
|
37 |
+
# Select the top five songs and return
|
38 |
+
return matched_songs.loc[top_5.index, ['song','artist','seq','similarity','sentiment','score']]
|