Spaces:
Runtime error
Runtime error
test feedback
Browse files- app.py +127 -8
- feedback_data/data-00000-of-00001.arrow +3 -0
- feedback_data/dataset_info.json +19 -0
- feedback_data/state.json +13 -0
app.py
CHANGED
@@ -280,12 +280,123 @@
|
|
280 |
|
281 |
# interface.launch()
|
282 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
283 |
import gradio as gr
|
284 |
import torch
|
285 |
import torch.nn as nn
|
286 |
from joblib import load
|
287 |
import numpy as np
|
288 |
-
import
|
289 |
|
290 |
# Define the neural network model
|
291 |
class ImprovedSongRecommender(nn.Module):
|
@@ -346,16 +457,23 @@ def recommend_songs(tags, artist_name):
|
|
346 |
output = model(input_tensor)
|
347 |
recommendations_indices = torch.topk(output, 5).indices.squeeze().tolist()
|
348 |
recommendations = [label_encoders['title'].inverse_transform([idx])[0] for idx in recommendations_indices]
|
349 |
-
print("Recommendations:", recommendations)
|
350 |
return recommendations
|
351 |
|
352 |
def record_feedback(recommendation, feedback):
|
353 |
-
|
354 |
-
|
355 |
-
|
356 |
-
|
357 |
-
|
358 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
359 |
return "Feedback recorded!"
|
360 |
|
361 |
app = gr.Blocks()
|
@@ -390,3 +508,4 @@ with app:
|
|
390 |
)
|
391 |
|
392 |
app.launch()
|
|
|
|
280 |
|
281 |
# interface.launch()
|
282 |
|
283 |
+
# import gradio as gr
|
284 |
+
# import torch
|
285 |
+
# import torch.nn as nn
|
286 |
+
# from joblib import load
|
287 |
+
# import numpy as np
|
288 |
+
# import os
|
289 |
+
|
290 |
+
# # Define the neural network model
|
291 |
+
# class ImprovedSongRecommender(nn.Module):
|
292 |
+
# def __init__(self, input_size, num_titles):
|
293 |
+
# super(ImprovedSongRecommender, self).__init__()
|
294 |
+
# self.fc1 = nn.Linear(input_size, 128)
|
295 |
+
# self.bn1 = nn.BatchNorm1d(128)
|
296 |
+
# self.fc2 = nn.Linear(128, 256)
|
297 |
+
# self.bn2 = nn.BatchNorm1d(256)
|
298 |
+
# self.fc3 = nn.Linear(256, 128)
|
299 |
+
# self.bn3 = nn.BatchNorm1d(128)
|
300 |
+
# self.output = nn.Linear(128, num_titles)
|
301 |
+
# self.dropout = nn.Dropout(0.5)
|
302 |
+
|
303 |
+
# def forward(self, x):
|
304 |
+
# x = torch.relu(self.bn1(self.fc1(x)))
|
305 |
+
# x = self.dropout(x)
|
306 |
+
# x = torch.relu(self.bn2(self.fc2(x)))
|
307 |
+
# x = self.dropout(x)
|
308 |
+
# x = torch.relu(self.bn3(self.fc3(x)))
|
309 |
+
# x = self.dropout(x)
|
310 |
+
# x = self.output(x)
|
311 |
+
# return x
|
312 |
+
|
313 |
+
# # Load the trained model
|
314 |
+
# model_path = "models/improved_model.pth"
|
315 |
+
# num_unique_titles = 4855
|
316 |
+
# model = ImprovedSongRecommender(input_size=2, num_titles=num_unique_titles)
|
317 |
+
# model.load_state_dict(torch.load(model_path, map_location=torch.device('cpu')))
|
318 |
+
# model.eval()
|
319 |
+
|
320 |
+
# # Load the label encoders and scaler
|
321 |
+
# label_encoders_path = "data/new_label_encoders.joblib"
|
322 |
+
# label_encoders = load(label_encoders_path)
|
323 |
+
|
324 |
+
# def encode_input(tags, artist_name):
|
325 |
+
# tags_list = [tag.strip() for tag in tags.split(',')]
|
326 |
+
# encoded_tags_list = []
|
327 |
+
# for tag in tags_list:
|
328 |
+
# try:
|
329 |
+
# encoded_tags_list.append(label_encoders['tags'].transform([tag])[0])
|
330 |
+
# except ValueError:
|
331 |
+
# encoded_tags_list.append(label_encoders['tags'].transform(['unknown'])[0])
|
332 |
+
|
333 |
+
# encoded_tags = np.mean(encoded_tags_list).astype(int) if encoded_tags_list else label_encoders['tags'].transform(['unknown'])[0]
|
334 |
+
|
335 |
+
# try:
|
336 |
+
# encoded_artist = label_encoders['artist_name'].transform([artist_name])[0]
|
337 |
+
# except ValueError:
|
338 |
+
# encoded_artist = label_encoders['artist_name'].transform(['unknown'])[0]
|
339 |
+
|
340 |
+
# return [encoded_tags, encoded_artist]
|
341 |
+
|
342 |
+
# def recommend_songs(tags, artist_name):
|
343 |
+
# encoded_input = encode_input(tags, artist_name)
|
344 |
+
# input_tensor = torch.tensor([encoded_input]).float()
|
345 |
+
# with torch.no_grad():
|
346 |
+
# output = model(input_tensor)
|
347 |
+
# recommendations_indices = torch.topk(output, 5).indices.squeeze().tolist()
|
348 |
+
# recommendations = [label_encoders['title'].inverse_transform([idx])[0] for idx in recommendations_indices]
|
349 |
+
# print("Recommendations:", recommendations)
|
350 |
+
# return recommendations
|
351 |
+
|
352 |
+
# def record_feedback(recommendation, feedback):
|
353 |
+
# feedback_path = "feedback_data.csv"
|
354 |
+
# if not os.path.exists(feedback_path):
|
355 |
+
# with open(feedback_path, 'w') as f:
|
356 |
+
# f.write("Recommendation,Feedback\n")
|
357 |
+
# with open(feedback_path, 'a') as f:
|
358 |
+
# f.write(f"{recommendation},{feedback}\n")
|
359 |
+
# return "Feedback recorded!"
|
360 |
+
|
361 |
+
# app = gr.Blocks()
|
362 |
+
|
363 |
+
# with app:
|
364 |
+
# gr.Markdown("## Music Recommendation System")
|
365 |
+
# tags_input = gr.Textbox(label="Enter Tags (e.g., rock, jazz, pop)", placeholder="rock, pop")
|
366 |
+
# artist_name_input = gr.Textbox(label="Enter Artist Name (optional)", placeholder="The Beatles")
|
367 |
+
# submit_button = gr.Button("Get Recommendations")
|
368 |
+
# recommendations_output = gr.HTML(label="Recommendations")
|
369 |
+
# feedback_input = gr.Radio(choices=["Thumbs Up", "Thumbs Down"], label="Feedback")
|
370 |
+
# feedback_button = gr.Button("Submit Feedback")
|
371 |
+
# feedback_result = gr.Label(label="Feedback Result")
|
372 |
+
|
373 |
+
# def display_recommendations(tags, artist_name):
|
374 |
+
# recommendations = recommend_songs(tags, artist_name)
|
375 |
+
# if recommendations:
|
376 |
+
# return recommendations
|
377 |
+
# else:
|
378 |
+
# return ["No recommendations found"]
|
379 |
+
|
380 |
+
# submit_button.click(
|
381 |
+
# fn=display_recommendations,
|
382 |
+
# inputs=[tags_input, artist_name_input],
|
383 |
+
# outputs=recommendations_output
|
384 |
+
# )
|
385 |
+
|
386 |
+
# feedback_button.click(
|
387 |
+
# fn=record_feedback,
|
388 |
+
# inputs=[recommendations_output, feedback_input],
|
389 |
+
# outputs=feedback_result
|
390 |
+
# )
|
391 |
+
|
392 |
+
# app.launch()
|
393 |
+
|
394 |
import gradio as gr
|
395 |
import torch
|
396 |
import torch.nn as nn
|
397 |
from joblib import load
|
398 |
import numpy as np
|
399 |
+
from datasets import load_dataset, Dataset, DatasetDict
|
400 |
|
401 |
# Define the neural network model
|
402 |
class ImprovedSongRecommender(nn.Module):
|
|
|
457 |
output = model(input_tensor)
|
458 |
recommendations_indices = torch.topk(output, 5).indices.squeeze().tolist()
|
459 |
recommendations = [label_encoders['title'].inverse_transform([idx])[0] for idx in recommendations_indices]
|
460 |
+
print("Recommendations:", recommendations)
|
461 |
return recommendations
|
462 |
|
463 |
def record_feedback(recommendation, feedback):
|
464 |
+
# Load the dataset or create a new one if it doesn't exist
|
465 |
+
try:
|
466 |
+
feedback_dataset = load_dataset("feedback_data", split='train')
|
467 |
+
except:
|
468 |
+
feedback_dataset = Dataset.from_dict({"Recommendation": [], "Feedback": []})
|
469 |
+
|
470 |
+
# Append new feedback
|
471 |
+
new_feedback = {"Recommendation": recommendation, "Feedback": feedback}
|
472 |
+
feedback_dataset = feedback_dataset.add_item(new_feedback)
|
473 |
+
|
474 |
+
# Save the dataset
|
475 |
+
feedback_dataset.save_to_disk("feedback_data")
|
476 |
+
|
477 |
return "Feedback recorded!"
|
478 |
|
479 |
app = gr.Blocks()
|
|
|
508 |
)
|
509 |
|
510 |
app.launch()
|
511 |
+
|
feedback_data/data-00000-of-00001.arrow
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:b76de5239adae063b11279ee3a4d3d1809d580516767b964725a47501e711c19
|
3 |
+
size 872
|
feedback_data/dataset_info.json
ADDED
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"citation": "",
|
3 |
+
"description": "",
|
4 |
+
"features": {
|
5 |
+
"Recommendation": {
|
6 |
+
"feature": {
|
7 |
+
"dtype": "string",
|
8 |
+
"_type": "Value"
|
9 |
+
},
|
10 |
+
"_type": "Sequence"
|
11 |
+
},
|
12 |
+
"Feedback": {
|
13 |
+
"dtype": "string",
|
14 |
+
"_type": "Value"
|
15 |
+
}
|
16 |
+
},
|
17 |
+
"homepage": "",
|
18 |
+
"license": ""
|
19 |
+
}
|
feedback_data/state.json
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"_data_files": [
|
3 |
+
{
|
4 |
+
"filename": "data-00000-of-00001.arrow"
|
5 |
+
}
|
6 |
+
],
|
7 |
+
"_fingerprint": "ea23739ca0f6199e",
|
8 |
+
"_format_columns": null,
|
9 |
+
"_format_kwargs": {},
|
10 |
+
"_format_type": null,
|
11 |
+
"_output_all_columns": false,
|
12 |
+
"_split": null
|
13 |
+
}
|