Spaces:
Runtime error
Runtime error
Commit
·
749c535
1
Parent(s):
d6155b9
updated feed-back
Browse files- app.py +83 -62
- feedback_data.csv +0 -5
- feedback_data/feedback_data.csv +78 -0
- feedback_data/feedback_data.json +0 -2
app.py
CHANGED
@@ -1,13 +1,13 @@
|
|
1 |
-
|
2 |
-
|
3 |
import gradio as gr
|
4 |
import torch
|
5 |
import torch.nn as nn
|
6 |
from joblib import load
|
7 |
import numpy as np
|
8 |
-
|
9 |
|
10 |
# Define the neural network model
|
|
|
|
|
11 |
class ImprovedSongRecommender(nn.Module):
|
12 |
def __init__(self, input_size, num_titles):
|
13 |
super(ImprovedSongRecommender, self).__init__()
|
@@ -30,109 +30,130 @@ class ImprovedSongRecommender(nn.Module):
|
|
30 |
x = self.output(x)
|
31 |
return x
|
32 |
|
|
|
33 |
# Load the trained model
|
34 |
model_path = "models/improved_model.pth"
|
35 |
-
num_unique_titles = 4855
|
36 |
model = ImprovedSongRecommender(input_size=2, num_titles=num_unique_titles)
|
37 |
model.load_state_dict(torch.load(model_path, map_location=torch.device('cpu')))
|
38 |
model.eval()
|
39 |
|
40 |
-
# Load the label encoders
|
41 |
label_encoders_path = "data/new_label_encoders.joblib"
|
42 |
label_encoders = load(label_encoders_path)
|
43 |
|
|
|
44 |
def encode_input(tags, artist_name):
|
45 |
tags_list = [tag.strip() for tag in tags.split(',')]
|
46 |
encoded_tags_list = []
|
47 |
for tag in tags_list:
|
48 |
try:
|
49 |
-
encoded_tags_list.append(
|
|
|
50 |
except ValueError:
|
51 |
-
encoded_tags_list.append(
|
52 |
-
|
53 |
-
|
54 |
-
|
|
|
|
|
55 |
try:
|
56 |
-
encoded_artist = label_encoders['artist_name'].transform([artist_name])[
|
|
|
57 |
except ValueError:
|
58 |
-
encoded_artist = label_encoders['artist_name'].transform(['unknown'])[
|
|
|
59 |
|
60 |
return [encoded_tags, encoded_artist]
|
61 |
|
|
|
62 |
def recommend_songs(tags, artist_name):
|
63 |
encoded_input = encode_input(tags, artist_name)
|
64 |
input_tensor = torch.tensor([encoded_input]).float()
|
65 |
with torch.no_grad():
|
66 |
output = model(input_tensor)
|
67 |
recommendations_indices = torch.topk(output, 5).indices.squeeze().tolist()
|
68 |
-
recommendations = [label_encoders['title'].inverse_transform(
|
69 |
-
|
70 |
return recommendations
|
71 |
|
72 |
-
|
73 |
-
|
|
|
74 |
try:
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
#
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
return "Feedback recorded!"
|
88 |
|
89 |
-
def show_feedback():
|
90 |
-
try:
|
91 |
-
feedback_dataset = load_dataset("json", data_files="feedback_data/feedback_data.json")
|
92 |
-
feedback_dataset = feedback_dataset['train']
|
93 |
-
return feedback_dataset.to_pandas().to_html()
|
94 |
-
except:
|
95 |
-
return "No feedback data found."
|
96 |
|
97 |
app = gr.Blocks()
|
98 |
|
99 |
with app:
|
100 |
gr.Markdown("## Music Recommendation System")
|
101 |
-
tags_input = gr.Textbox(
|
102 |
-
|
103 |
submit_button = gr.Button("Get Recommendations")
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
|
|
|
|
|
|
|
|
|
|
117 |
|
118 |
submit_button.click(
|
119 |
fn=display_recommendations,
|
120 |
-
inputs=[tags_input
|
121 |
-
outputs=
|
122 |
)
|
123 |
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
)
|
129 |
|
130 |
-
|
131 |
-
fn=
|
132 |
-
inputs=[],
|
133 |
-
outputs=
|
134 |
)
|
135 |
|
136 |
-
|
|
|
|
|
137 |
|
|
|
138 |
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
import torch.nn as nn
|
4 |
from joblib import load
|
5 |
import numpy as np
|
6 |
+
import pandas as pd
|
7 |
|
8 |
# Define the neural network model
|
9 |
+
|
10 |
+
|
11 |
class ImprovedSongRecommender(nn.Module):
|
12 |
def __init__(self, input_size, num_titles):
|
13 |
super(ImprovedSongRecommender, self).__init__()
|
|
|
30 |
x = self.output(x)
|
31 |
return x
|
32 |
|
33 |
+
|
34 |
# Load the trained model
|
35 |
model_path = "models/improved_model.pth"
|
36 |
+
num_unique_titles = 4855
|
37 |
model = ImprovedSongRecommender(input_size=2, num_titles=num_unique_titles)
|
38 |
model.load_state_dict(torch.load(model_path, map_location=torch.device('cpu')))
|
39 |
model.eval()
|
40 |
|
41 |
+
# Load the label encoders
|
42 |
label_encoders_path = "data/new_label_encoders.joblib"
|
43 |
label_encoders = load(label_encoders_path)
|
44 |
|
45 |
+
|
46 |
def encode_input(tags, artist_name):
|
47 |
tags_list = [tag.strip() for tag in tags.split(',')]
|
48 |
encoded_tags_list = []
|
49 |
for tag in tags_list:
|
50 |
try:
|
51 |
+
encoded_tags_list.append(
|
52 |
+
label_encoders['tags'].transform([tag])[0])
|
53 |
except ValueError:
|
54 |
+
encoded_tags_list.append(
|
55 |
+
label_encoders['tags'].transform(['unknown'])[0])
|
56 |
+
|
57 |
+
encoded_tags = np.mean(encoded_tags_list).astype(
|
58 |
+
int) if encoded_tags_list else label_encoders['tags'].transform(['unknown'])[0]
|
59 |
+
|
60 |
try:
|
61 |
+
encoded_artist = label_encoders['artist_name'].transform([artist_name])[
|
62 |
+
0]
|
63 |
except ValueError:
|
64 |
+
encoded_artist = label_encoders['artist_name'].transform(['unknown'])[
|
65 |
+
0]
|
66 |
|
67 |
return [encoded_tags, encoded_artist]
|
68 |
|
69 |
+
|
70 |
def recommend_songs(tags, artist_name):
|
71 |
encoded_input = encode_input(tags, artist_name)
|
72 |
input_tensor = torch.tensor([encoded_input]).float()
|
73 |
with torch.no_grad():
|
74 |
output = model(input_tensor)
|
75 |
recommendations_indices = torch.topk(output, 5).indices.squeeze().tolist()
|
76 |
+
recommendations = [label_encoders['title'].inverse_transform(
|
77 |
+
[idx])[0] for idx in recommendations_indices]
|
78 |
return recommendations
|
79 |
|
80 |
+
|
81 |
+
def record_feedback(tags, recommendations, feedbacks):
|
82 |
+
# Load existing feedback if it exists
|
83 |
try:
|
84 |
+
feedback_df = pd.read_csv("feedback_data/feedback_data.csv")
|
85 |
+
except FileNotFoundError:
|
86 |
+
feedback_df = pd.DataFrame(
|
87 |
+
columns=["Tags", "Recommendation", "Feedback"])
|
88 |
+
|
89 |
+
# Create new feedback entries
|
90 |
+
new_feedbacks = pd.DataFrame({
|
91 |
+
"Tags": [tags] * len(recommendations),
|
92 |
+
"Recommendation": recommendations,
|
93 |
+
"Feedback": feedbacks
|
94 |
+
})
|
95 |
+
|
96 |
+
# Only keep rows where both a song recommendation and a rating are present
|
97 |
+
new_feedbacks = new_feedbacks[new_feedbacks["Recommendation"]
|
98 |
+
!= "No recommendations found"]
|
99 |
+
new_feedbacks = new_feedbacks[new_feedbacks["Feedback"].notna()]
|
100 |
+
|
101 |
+
# Append new feedback to the existing dataframe
|
102 |
+
feedback_df = pd.concat([feedback_df, new_feedbacks], ignore_index=True)
|
103 |
+
|
104 |
+
# Save the updated dataframe to CSV
|
105 |
+
feedback_df.to_csv("feedback_data/feedback_data.csv", index=False)
|
106 |
+
|
107 |
return "Feedback recorded!"
|
108 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
109 |
|
110 |
app = gr.Blocks()
|
111 |
|
112 |
with app:
|
113 |
gr.Markdown("## Music Recommendation System")
|
114 |
+
tags_input = gr.Textbox(
|
115 |
+
label="Enter Tags (e.g., rock, jazz, pop)", placeholder="rock, pop")
|
116 |
submit_button = gr.Button("Get Recommendations")
|
117 |
+
|
118 |
+
recommendation_outputs = [
|
119 |
+
gr.HTML(label=f"Recommendation {i+1}") for i in range(5)]
|
120 |
+
feedback_inputs = [gr.Radio(
|
121 |
+
choices=["Thumbs Up", "Thumbs Down"], label=f"Feedback {i+1}") for i in range(5)]
|
122 |
+
|
123 |
+
feedback_submit_button = gr.Button("Submit Feedback")
|
124 |
+
|
125 |
+
song_recommendations = []
|
126 |
+
|
127 |
+
def display_recommendations(tags):
|
128 |
+
global song_recommendations
|
129 |
+
song_recommendations = recommend_songs(tags, "")
|
130 |
+
updated_recommendations = [
|
131 |
+
gr.update(value=song) for song in song_recommendations]
|
132 |
+
updated_feedbacks = [gr.update(label=song)
|
133 |
+
for song in song_recommendations]
|
134 |
+
return updated_recommendations + updated_feedbacks
|
135 |
|
136 |
submit_button.click(
|
137 |
fn=display_recommendations,
|
138 |
+
inputs=[tags_input],
|
139 |
+
outputs=recommendation_outputs + feedback_inputs
|
140 |
)
|
141 |
|
142 |
+
def collect_feedback(tags, *feedbacks):
|
143 |
+
global song_recommendations
|
144 |
+
feedbacks = list(feedbacks)
|
145 |
+
return record_feedback(tags, song_recommendations, feedbacks)
|
|
|
146 |
|
147 |
+
feedback_submit_button.click(
|
148 |
+
fn=collect_feedback,
|
149 |
+
inputs=[tags_input] + feedback_inputs,
|
150 |
+
outputs=None
|
151 |
)
|
152 |
|
153 |
+
for i in range(5):
|
154 |
+
with gr.Row():
|
155 |
+
gr.Column([recommendation_outputs[i], feedback_inputs[i]])
|
156 |
|
157 |
+
gr.Row([feedback_submit_button])
|
158 |
|
159 |
+
app.launch()
|
feedback_data.csv
DELETED
@@ -1,5 +0,0 @@
|
|
1 |
-
|
2 |
-
None,Thumbs Up
|
3 |
-
['Heartburn', 'Helium', 'Long Live The Party', 'Dame Un Besito (Version Salsa)', 'Crazy'],Thumbs Up
|
4 |
-
["Let's Get It Started", 'Be My Lover', 'Min Aporis', 'Turn The Heat Up', 'As The Hog Pisseth'],Thumbs Down
|
5 |
-
["Let's Get It Started", 'Be My Lover', 'Turn The Heat Up', 'Min Aporis', 'Happy Safari'],Thumbs Up
|
|
|
|
|
|
|
|
|
|
|
|
feedback_data/feedback_data.csv
ADDED
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Tags,Recommendation,Feedback
|
2 |
+
rock,,Thumbs Down
|
3 |
+
rock,,Thumbs Up
|
4 |
+
rock,,Thumbs Up
|
5 |
+
rock,,Thumbs Down
|
6 |
+
rock,,Thumbs Up
|
7 |
+
rock,,Thumbs Up
|
8 |
+
rock,,Crazy
|
9 |
+
rock,,Hurry Up And Come
|
10 |
+
rock,,Hotel
|
11 |
+
rock,,If I Ain't Got You
|
12 |
+
rock,,Thumbs Up
|
13 |
+
rock,,Crazy
|
14 |
+
rock,,Hurry Up And Come
|
15 |
+
rock,,Hotel
|
16 |
+
rock,,If I Ain't Got You
|
17 |
+
rock,,Thumbs Up
|
18 |
+
rock,,Thumbs Down
|
19 |
+
rock,,Thumbs Up
|
20 |
+
rock,,Thumbs Down
|
21 |
+
rock,,Thumbs Up
|
22 |
+
rock,,Thumbs Up
|
23 |
+
rock,,Thumbs Down
|
24 |
+
rock,,Thumbs Up
|
25 |
+
rock,,
|
26 |
+
rock,,
|
27 |
+
rock,,Thumbs Up
|
28 |
+
rock,,Thumbs Down
|
29 |
+
rock,,Thumbs Up
|
30 |
+
rock,,
|
31 |
+
rock,,
|
32 |
+
pop,,Thumbs Up
|
33 |
+
pop,,Thumbs Down
|
34 |
+
pop,,Let's Get It Started
|
35 |
+
pop,,Thumbs Up
|
36 |
+
pop,,Thumbs Down
|
37 |
+
pop,,Thumbs Up
|
38 |
+
pop,,Thumbs Down
|
39 |
+
pop,,Let's Get It Started
|
40 |
+
pop,,Thumbs Up
|
41 |
+
pop,,Thumbs Down
|
42 |
+
rock,,Thumbs Down
|
43 |
+
rock,,Crazy
|
44 |
+
rock,,Hurry Up And Come
|
45 |
+
rock,,Hotel
|
46 |
+
rock,,If I Ain't Got You
|
47 |
+
rock,,
|
48 |
+
rock,,Thumbs Down
|
49 |
+
rock,,Thumbs Up
|
50 |
+
rock,,Thumbs Down
|
51 |
+
rock,,
|
52 |
+
grgrg,I've Been Trying,Thumbs Up
|
53 |
+
grgrg,Ahmea,
|
54 |
+
grgrg,Heartburn,
|
55 |
+
grgrg,Crazy,
|
56 |
+
grgrg,Intro,
|
57 |
+
grgrg,I've Been Trying,Thumbs Up
|
58 |
+
grgrg,Ahmea,
|
59 |
+
grgrg,Heartburn,Thumbs Down
|
60 |
+
grgrg,Crazy,
|
61 |
+
grgrg,Intro,
|
62 |
+
rock,Heartburn,
|
63 |
+
rock,Crazy,Thumbs Down
|
64 |
+
rock,Hurry Up And Come,Thumbs Up
|
65 |
+
rock,Hotel,
|
66 |
+
rock,If I Ain't Got You,
|
67 |
+
rock,Heartburn,
|
68 |
+
rock,Crazy,Thumbs Down
|
69 |
+
rock,Hurry Up And Come,Thumbs Up
|
70 |
+
rock,Hotel,
|
71 |
+
rock,If I Ain't Got You,
|
72 |
+
rock,Heartburn,Thumbs Up
|
73 |
+
rock,Crazy,Thumbs Down
|
74 |
+
rock,Hurry Up And Come,Thumbs Up
|
75 |
+
rock,Heartburn,Thumbs Up
|
76 |
+
rock,Crazy,Thumbs Down
|
77 |
+
rock,Hurry Up And Come,Thumbs Up
|
78 |
+
rock,Hotel,Thumbs Down
|
feedback_data/feedback_data.json
DELETED
@@ -1,2 +0,0 @@
|
|
1 |
-
{"Recommendation":["Helium","Heartburn","Let's Get It Started","Dame Un Besito (Version Salsa)","Long Live The Party"],"Feedback":"Thumbs Up"}
|
2 |
-
{"Recommendation":["Heartburn","Hurry Up And Come","Helium","Crazy","Hotel"],"Feedback":"Thumbs Down"},
|
|
|
|
|
|