Spaces:
Running
Running
Commit
·
4304bb5
0
Parent(s):
app.py
Browse files- .gitattributes +35 -0
- README.md +13 -0
- app.py +358 -0
- effectSound/avg_rating_transformer_effectSound.joblib +3 -0
- effectSound/effectSound_onehot_cols.joblib +3 -0
- effectSound/effectSound_subcategory_cols.joblib +3 -0
- effectSound/effect_onehot_tags.joblib +3 -0
- effectSound/est_num_downloads_effectSound.joblib +3 -0
- effectSound/scaler_effectSamplerate.joblib +3 -0
- effectSound/scaler_effectSound_age_days_log.joblib +3 -0
- effectSound/username_freq_dict_effectSound.joblib +3 -0
- music/avg_rating_transformer_music.joblib +3 -0
- music/est_num_downloads_music.joblib +3 -0
- music/music_onehot_cols.joblib +3 -0
- music/music_onehot_tags.joblib +3 -0
- music/music_subcategory_cols.joblib +3 -0
- music/music_xgb_avg_rating.joblib +3 -0
- music/music_xgb_model_smote_balanced_avg_rating.joblib +3 -0
- music/music_xgb_model_smote_balanced_num_downloads.joblib +3 -0
- music/scaler_music_age_days_log.joblib +3 -0
- music/scaler_music_samplerate.joblib +3 -0
- music/username_freq_dict_music.joblib +3 -0
- requirements.txt +8 -0
.gitattributes
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
| 2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
| 3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
| 4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
| 5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
| 6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
| 7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
| 8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
| 9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
| 10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
| 11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
| 12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
| 13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
| 14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
| 15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
| 16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
| 17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
| 18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
| 19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
| 21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
| 22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
| 24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
| 25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
| 26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
| 27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
| 28 |
+
*.tar filter=lfs diff=lfs merge=lfs -text
|
| 29 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 30 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 31 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
| 32 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
| 33 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Freesound Popularity
|
| 3 |
+
emoji: 🌍
|
| 4 |
+
colorFrom: red
|
| 5 |
+
colorTo: blue
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 6.5.0
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: false
|
| 10 |
+
short_description: freesound popularity music & effectSound
|
| 11 |
+
---
|
| 12 |
+
|
| 13 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
|
@@ -0,0 +1,358 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import numpy as np
|
| 5 |
+
from sklearn.preprocessing import KBinsDiscretizer, StandardScaler, OneHotEncoder
|
| 6 |
+
from sklearn.feature_extraction.text import HashingVectorizer
|
| 7 |
+
from collections import Counter
|
| 8 |
+
import joblib
|
| 9 |
+
import freesound
|
| 10 |
+
import gensim.downloader as api
|
| 11 |
+
|
| 12 |
+
# -------- FreeSound API --------
|
| 13 |
+
client = freesound.FreesoundClient()
|
| 14 |
+
client.set_token("zE9NjEOgUMzH9K7mjiGBaPJiNwJLjSM53LevarRK", "token")
|
| 15 |
+
|
| 16 |
+
dataset_dir = "dataset_audio"
|
| 17 |
+
os.makedirs(dataset_dir, exist_ok=True)
|
| 18 |
+
|
| 19 |
+
class AvgRatingTransformer:
|
| 20 |
+
def __init__(self, est, class_mapping=None):
|
| 21 |
+
self.est = est
|
| 22 |
+
if class_mapping is None:
|
| 23 |
+
self.class_mapping = {0:"MissedInfo", 1:"Low", 2:"Medium", 3:"High"}
|
| 24 |
+
else:
|
| 25 |
+
self.class_mapping = class_mapping
|
| 26 |
+
|
| 27 |
+
def transform(self, X):
|
| 28 |
+
X = X.copy()
|
| 29 |
+
mask_non_zero = X != 0
|
| 30 |
+
Xt = np.zeros_like(X, dtype=int)
|
| 31 |
+
if mask_non_zero.any():
|
| 32 |
+
Xt[mask_non_zero] = self.est.transform(X[mask_non_zero].reshape(-1,1)).flatten() + 1
|
| 33 |
+
X_transformed = np.array([self.class_mapping.get(v, "MissedInfo") for v in Xt])
|
| 34 |
+
return X_transformed
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
# -------- Charger les objets sauvegardés --------
|
| 38 |
+
# Music
|
| 39 |
+
scaler_samplerate_music = joblib.load("music/scaler_music_samplerate.joblib")
|
| 40 |
+
scaler_age_days_music = joblib.load("music/scaler_music_age_days_log.joblib")
|
| 41 |
+
username_freq_music = joblib.load("music/username_freq_dict_music.joblib")
|
| 42 |
+
est_num_downloads_music = joblib.load("music/est_num_downloads_music.joblib")
|
| 43 |
+
avg_rating_transformer_music = joblib.load("music/avg_rating_transformer_music.joblib")
|
| 44 |
+
music_subcategory_cols = joblib.load("music/music_subcategory_cols.joblib")
|
| 45 |
+
music_onehot_cols = joblib.load("music/music_onehot_cols.joblib")
|
| 46 |
+
music_onehot_tags = joblib.load("music/music_onehot_tags.joblib")
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
# -------- MODELS --------
|
| 50 |
+
|
| 51 |
+
# Music
|
| 52 |
+
music_model_num_downloads = joblib.load(
|
| 53 |
+
"models/music/music_model_num_downloads.joblib"
|
| 54 |
+
)
|
| 55 |
+
music_model_avg_rating = joblib.load(
|
| 56 |
+
"models/music/music_xgb_avg_rating.joblib"
|
| 57 |
+
)
|
| 58 |
+
music_avg_rating_le = joblib.load(
|
| 59 |
+
"models/music/music_xgb_avg_rating_label_encoder.joblib"
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
# EffectSound
|
| 63 |
+
effect_model_num_downloads = joblib.load(
|
| 64 |
+
"models/effectSound/effectSound_model_num_downloads.joblib"
|
| 65 |
+
)
|
| 66 |
+
effect_model_avg_rating = joblib.load(
|
| 67 |
+
"models/effectSound/effectSound_xgb_avg_rating.joblib"
|
| 68 |
+
)
|
| 69 |
+
effect_avg_rating_le = joblib.load(
|
| 70 |
+
"models/effectSound/effectSound_xgb_avg_rating_label_encoder.joblib"
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
# EffectSound
|
| 75 |
+
scaler_samplerate_effect = joblib.load("effectSound/scaler_effectSamplerate.joblib")
|
| 76 |
+
scaler_age_days_effect = joblib.load("effectSound/scaler_effectSound_age_days_log.joblib")
|
| 77 |
+
username_freq_effect = joblib.load("effectSound/username_freq_dict_effectSound.joblib")
|
| 78 |
+
est_num_downloads_effect = joblib.load("effectSound/est_num_downloads_effectSound.joblib")
|
| 79 |
+
avg_rating_transformer_effect = joblib.load("effectSound/avg_rating_transformer_effectSound.joblib")
|
| 80 |
+
effect_subcategory_cols = joblib.load("effectSound/effectSound_subcategory_cols.joblib")
|
| 81 |
+
effect_onehot_cols = joblib.load("effectSound/effectSound_onehot_cols.joblib")
|
| 82 |
+
effect_onehot_tags = joblib.load("effectSound/effect_onehot_tags.joblib")
|
| 83 |
+
|
| 84 |
+
# GloVe pour description
|
| 85 |
+
glove_model = api.load("glove-wiki-gigaword-100")
|
| 86 |
+
|
| 87 |
+
# -------- Fonctions --------
|
| 88 |
+
|
| 89 |
+
def fetch_sound_metadata(sound_url):
|
| 90 |
+
"""Télécharge les métadonnées du son FreeSound"""
|
| 91 |
+
sound_id = int(sound_url.rstrip("/").split("/")[-1])
|
| 92 |
+
sound = client.get_sound(sound_id)
|
| 93 |
+
file_name = f"{sound.name.replace(' ', '_')}.mp3"
|
| 94 |
+
file_path = os.path.join(dataset_dir, file_name)
|
| 95 |
+
try:
|
| 96 |
+
sound.retrieve_preview(dataset_dir, file_name)
|
| 97 |
+
except Exception as e:
|
| 98 |
+
print(f"Erreur téléchargement {file_name}: {e}")
|
| 99 |
+
file_path = None
|
| 100 |
+
data = {
|
| 101 |
+
"file_path": file_path,
|
| 102 |
+
"name": sound.name,
|
| 103 |
+
"num_ratings": sound.num_ratings,
|
| 104 |
+
"tags": ",".join(sound.tags) if getattr(sound, "tags", None) else "",
|
| 105 |
+
"username": sound.username,
|
| 106 |
+
"description": sound.description if sound.description else "",
|
| 107 |
+
"created": getattr(sound, "created", ""),
|
| 108 |
+
"license": getattr(sound, "license", ""),
|
| 109 |
+
"num_downloads": getattr(sound, "num_downloads", 0),
|
| 110 |
+
"channels": getattr(sound, "channels", 0),
|
| 111 |
+
"filesize": getattr(sound, "filesize", 0),
|
| 112 |
+
"num_comments": getattr(sound, "num_comments", 0),
|
| 113 |
+
"category_is_user_provided": getattr(sound, "category_is_user_provided", 0),
|
| 114 |
+
"duration": getattr(sound, "duration", 0),
|
| 115 |
+
"avg_rating": getattr(sound, "avg_rating", 0),
|
| 116 |
+
"category": getattr(sound, "category", "Unknown"),
|
| 117 |
+
"subcategory": getattr(sound, "subcategory", "Other"),
|
| 118 |
+
"type": getattr(sound, "type", ""),
|
| 119 |
+
"samplerate": getattr(sound, "samplerate", 0)
|
| 120 |
+
}
|
| 121 |
+
return pd.DataFrame([data])
|
| 122 |
+
|
| 123 |
+
def description_to_vec(text, model, dim=100):
|
| 124 |
+
if not text:
|
| 125 |
+
return np.zeros(dim)
|
| 126 |
+
words = text.lower().split()
|
| 127 |
+
vecs = [model[w] for w in words if w in model]
|
| 128 |
+
if len(vecs) == 0:
|
| 129 |
+
return np.zeros(dim)
|
| 130 |
+
return np.mean(vecs, axis=0)
|
| 131 |
+
|
| 132 |
+
def preprocess_sound(df):
|
| 133 |
+
"""Applique le preprocessing complet selon duration pour choisir music ou effectSound"""
|
| 134 |
+
df = df.copy()
|
| 135 |
+
dur = df["duration"].iloc[0]
|
| 136 |
+
|
| 137 |
+
if 0.5 <= dur <= 3:
|
| 138 |
+
dataset_type = "effectSound"
|
| 139 |
+
scaler_samplerate = scaler_samplerate_effect
|
| 140 |
+
scaler_age = scaler_age_days_effect
|
| 141 |
+
username_freq = username_freq_effect
|
| 142 |
+
est_num_downloads = est_num_downloads_effect
|
| 143 |
+
avg_rating_transformer = avg_rating_transformer_effect
|
| 144 |
+
subcat_cols = effect_subcategory_cols
|
| 145 |
+
onehot_cols = effect_onehot_cols
|
| 146 |
+
onehot_tags = effect_onehot_tags
|
| 147 |
+
elif 10 <= dur <= 60:
|
| 148 |
+
dataset_type = "music"
|
| 149 |
+
scaler_samplerate = scaler_samplerate_music
|
| 150 |
+
scaler_age = scaler_age_days_music
|
| 151 |
+
username_freq = username_freq_music
|
| 152 |
+
est_num_downloads = est_num_downloads_music
|
| 153 |
+
avg_rating_transformer = avg_rating_transformer_music
|
| 154 |
+
subcat_cols = music_subcategory_cols
|
| 155 |
+
onehot_cols = music_onehot_cols
|
| 156 |
+
onehot_tags = music_onehot_tags
|
| 157 |
+
else:
|
| 158 |
+
return f"❌ Son trop court ou trop long ({dur} sec)"
|
| 159 |
+
|
| 160 |
+
# ----------------- Features -----------------
|
| 161 |
+
# Category bool
|
| 162 |
+
df["category_is_user_provided"] = df["category_is_user_provided"].astype(int)
|
| 163 |
+
|
| 164 |
+
|
| 165 |
+
# Username frequency
|
| 166 |
+
df["username_freq"] = df["username"].map(username_freq).fillna(0)
|
| 167 |
+
|
| 168 |
+
# Numeric features
|
| 169 |
+
for col in ["num_ratings", "num_comments", "filesize", "duration"]:
|
| 170 |
+
df[col] = np.log1p(df[col])
|
| 171 |
+
df["samplerate"] = scaler_samplerate.transform(df[["samplerate"]])
|
| 172 |
+
|
| 173 |
+
# Age_days
|
| 174 |
+
df["created"] = pd.to_datetime(df["created"], errors="coerce").dt.tz_localize(None)
|
| 175 |
+
df["age_days"] = (pd.Timestamp.now() - df["created"]).dt.days
|
| 176 |
+
df["age_days_log"] = np.log1p(df["age_days"])
|
| 177 |
+
df["age_days_log_scaled"] = scaler_age.transform(df[["age_days_log"]])
|
| 178 |
+
df = df.drop(columns=["created", "age_days", "age_days_log"])
|
| 179 |
+
|
| 180 |
+
# num_downloads
|
| 181 |
+
df["num_downloads_class"] = est_num_downloads.transform(df[["num_downloads"]])
|
| 182 |
+
|
| 183 |
+
# avg_rating
|
| 184 |
+
df["avg_rating"] = avg_rating_transformer.transform(df["avg_rating"].to_numpy())
|
| 185 |
+
|
| 186 |
+
# Subcategory
|
| 187 |
+
for col in subcat_cols:
|
| 188 |
+
df[col] = 0 # toutes les colonnes initialisées à 0
|
| 189 |
+
# activer 1 pour la bonne subcategory
|
| 190 |
+
subcat_val = df["subcategory"].iloc[0]
|
| 191 |
+
for col in subcat_cols:
|
| 192 |
+
cat_name = col.replace("subcategory_", "")
|
| 193 |
+
if subcat_val == cat_name:
|
| 194 |
+
df[col] = 1
|
| 195 |
+
df.drop(columns=["subcategory"], inplace=True)
|
| 196 |
+
|
| 197 |
+
|
| 198 |
+
|
| 199 |
+
# créer toutes les colonnes attendues à 0
|
| 200 |
+
for col in onehot_cols:
|
| 201 |
+
if col not in df.columns:
|
| 202 |
+
df[col] = 0
|
| 203 |
+
|
| 204 |
+
# activer les bonnes colonnes one-hot
|
| 205 |
+
license_val = df.loc[0, "license"]
|
| 206 |
+
category_val = df.loc[0, "category"]
|
| 207 |
+
type_val = df.loc[0, "type"]
|
| 208 |
+
|
| 209 |
+
for col_name in [
|
| 210 |
+
f"license_{license_val}",
|
| 211 |
+
f"category_{category_val}",
|
| 212 |
+
f"type_{type_val}",
|
| 213 |
+
]:
|
| 214 |
+
if col_name in df.columns:
|
| 215 |
+
df[col_name] = 1
|
| 216 |
+
|
| 217 |
+
|
| 218 |
+
|
| 219 |
+
|
| 220 |
+
|
| 221 |
+
# Tags
|
| 222 |
+
# Si la colonne "tags" n'existe pas, on la crée avec une valeur vide
|
| 223 |
+
for col in ["name", "tags", "description"]:
|
| 224 |
+
if col not in df.columns:
|
| 225 |
+
df[col] = ""
|
| 226 |
+
|
| 227 |
+
df["tags_list"] = df["tags"].fillna("").astype(str).str.lower().str.split(",")
|
| 228 |
+
|
| 229 |
+
# Si aucun tag n'existe ou que la liste est vide, mettre "Other"
|
| 230 |
+
if not df["tags_list"].iloc[0] or df["tags_list"].iloc[0] == [""]:
|
| 231 |
+
df["tags_list"] = [["Other"]]
|
| 232 |
+
|
| 233 |
+
# One-hot sur toutes les colonnes enregistrées
|
| 234 |
+
for col in onehot_tags:
|
| 235 |
+
tag_name = col.replace("tag_", "").replace("_", " ")
|
| 236 |
+
df[col] = int(tag_name in df["tags_list"].iloc[0])
|
| 237 |
+
|
| 238 |
+
# Supprimer les colonnes temporaires
|
| 239 |
+
df.drop(columns=["tags_list", "tags"], inplace=True)
|
| 240 |
+
|
| 241 |
+
|
| 242 |
+
# Name
|
| 243 |
+
df["name_clean"] = df["name"].astype(str).str.lower().str.rsplit(".", n=1).str[0]
|
| 244 |
+
vectorizer = HashingVectorizer(n_features=8, alternate_sign=False, norm=None)
|
| 245 |
+
name_vec = vectorizer.transform(df["name_clean"])
|
| 246 |
+
for i in range(8):
|
| 247 |
+
df[f"name_vec_{i}"] = name_vec.toarray()[0][i]
|
| 248 |
+
df.drop(columns=["name","name_clean"], inplace=True)
|
| 249 |
+
|
| 250 |
+
|
| 251 |
+
# Description
|
| 252 |
+
desc_vec = description_to_vec(df["description"].iloc[0], glove_model)
|
| 253 |
+
for i in range(100):
|
| 254 |
+
df[f"description_glove_{i}"] = desc_vec[i]
|
| 255 |
+
df.drop(columns=["description"], inplace=True)
|
| 256 |
+
|
| 257 |
+
|
| 258 |
+
df.drop(columns=[ "license","category","type","created","subcategory","id","num_downloads","file_path","username"],inplace=True, errors="ignore")
|
| 259 |
+
|
| 260 |
+
# --- SAFE REORDER (CRUCIAL) ---
|
| 261 |
+
|
| 262 |
+
final_cols = []
|
| 263 |
+
|
| 264 |
+
for col in onehot_cols:
|
| 265 |
+
if col in df.columns:
|
| 266 |
+
final_cols.append(col)
|
| 267 |
+
|
| 268 |
+
# subcategories
|
| 269 |
+
for col in subcat_cols:
|
| 270 |
+
if col in df.columns:
|
| 271 |
+
final_cols.append(col)
|
| 272 |
+
|
| 273 |
+
# le reste
|
| 274 |
+
final_cols += [c for c in df.columns if c not in final_cols]
|
| 275 |
+
|
| 276 |
+
df = df[final_cols]
|
| 277 |
+
|
| 278 |
+
|
| 279 |
+
|
| 280 |
+
|
| 281 |
+
return df
|
| 282 |
+
|
| 283 |
+
# -------- Gradio --------
|
| 284 |
+
def predict_with_metadata(url):
|
| 285 |
+
if url.strip() == "":
|
| 286 |
+
return "❌ Veuillez entrer une URL FreeSound."
|
| 287 |
+
|
| 288 |
+
# 1️ Récupérer les métadonnées brutes
|
| 289 |
+
df_raw = fetch_sound_metadata(url)
|
| 290 |
+
|
| 291 |
+
# Affichage ligne par ligne pour les métadonnées brutes
|
| 292 |
+
raw_lines = ["=== Métadonnées brutes ==="]
|
| 293 |
+
for col in df_raw.columns:
|
| 294 |
+
raw_lines.append(f"{col}: {df_raw[col].iloc[0]}")
|
| 295 |
+
raw_str = "\n".join(raw_lines)
|
| 296 |
+
|
| 297 |
+
# 2️ Vérifier la durée
|
| 298 |
+
dur = df_raw["duration"].iloc[0]
|
| 299 |
+
if dur < 0.5:
|
| 300 |
+
return raw_str + f"\n\n Son trop court ({dur} sec), veuillez entrer un son qui est court (0.5 à 3 s) ou un son long (10 à 60 s)"
|
| 301 |
+
elif 3 < dur < 10 or dur > 60:
|
| 302 |
+
return raw_str + f"\n\n Son trop long ou hors plage acceptable ({dur} sec) , veuillez entrer un son qui est court (0.5 à 3 s) ou un son long (10 à 60 s)"
|
| 303 |
+
|
| 304 |
+
# 3️ Prétraitement seulement si durée ok
|
| 305 |
+
df_processed = preprocess_sound(df_raw)
|
| 306 |
+
|
| 307 |
+
# PRÉDICTIONS
|
| 308 |
+
# =======================
|
| 309 |
+
|
| 310 |
+
if 0.5 <= dur <= 3:
|
| 311 |
+
model_nd = effect_model_num_downloads
|
| 312 |
+
model_ar = effect_model_avg_rating
|
| 313 |
+
le_ar = effect_avg_rating_le
|
| 314 |
+
sound_type = "EffectSound"
|
| 315 |
+
else:
|
| 316 |
+
model_nd = music_model_num_downloads
|
| 317 |
+
model_ar = music_model_avg_rating
|
| 318 |
+
le_ar = music_avg_rating_le
|
| 319 |
+
sound_type = "Music"
|
| 320 |
+
|
| 321 |
+
# Num downloads
|
| 322 |
+
pred_num_downloads = model_nd.predict(df_processed)[0]
|
| 323 |
+
|
| 324 |
+
# Avg rating
|
| 325 |
+
pred_avg_rating_enc = model_ar.predict(df_processed)[0]
|
| 326 |
+
pred_avg_rating = le_ar.inverse_transform([pred_avg_rating_enc])[0]
|
| 327 |
+
|
| 328 |
+
# Affichage ligne par ligne pour les features après preprocessing
|
| 329 |
+
processed_lines = ["\n=== Features après preprocessing ==="]
|
| 330 |
+
for col in df_processed.columns:
|
| 331 |
+
processed_lines.append(f"{col}: {df_processed[col].iloc[0]}")
|
| 332 |
+
processed_str = "\n".join(processed_lines)
|
| 333 |
+
|
| 334 |
+
prediction_lines = [
|
| 335 |
+
"\n=== Prédictions ===",
|
| 336 |
+
f"Type détecté : {sound_type}",
|
| 337 |
+
f"📥 Num downloads prédit : {pred_num_downloads}",
|
| 338 |
+
f"⭐ Avg rating prédit : {pred_avg_rating}"
|
| 339 |
+
]
|
| 340 |
+
|
| 341 |
+
prediction_str = "\n".join(prediction_lines)
|
| 342 |
+
|
| 343 |
+
return raw_str + processed_str + prediction_str
|
| 344 |
+
|
| 345 |
+
|
| 346 |
+
|
| 347 |
+
|
| 348 |
+
with gr.Blocks(title="FreeSound Popularity Detector") as demo:
|
| 349 |
+
gr.Markdown("# 🎧 FreeSound Popularity Detector")
|
| 350 |
+
gr.Markdown("Collez l'URL d'un son FreeSound et le preprocessing complet sera appliqué automatiquement.")
|
| 351 |
+
|
| 352 |
+
url_input = gr.Textbox(label="URL du son FreeSound")
|
| 353 |
+
btn_meta = gr.Button("📊 Prétraiter et afficher features")
|
| 354 |
+
output = gr.Textbox(label="Résultat")
|
| 355 |
+
|
| 356 |
+
btn_meta.click(fn=predict_with_metadata, inputs=url_input, outputs=output)
|
| 357 |
+
|
| 358 |
+
demo.launch()
|
effectSound/avg_rating_transformer_effectSound.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:37b2862f96050ad72ab3964d30f4f2d3908dd6b5e746f150c1baea5d2cdb2bbf
|
| 3 |
+
size 944
|
effectSound/effectSound_onehot_cols.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:3de47c718ff02e366470f28167a8e5736829fa84b0d34531ac046ceaec5371fa
|
| 3 |
+
size 761
|
effectSound/effectSound_subcategory_cols.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:6796b185bc36b2a0961c0a0b22f813f473eec2962cfa5c20a013f0f328ae8021
|
| 3 |
+
size 418
|
effectSound/effect_onehot_tags.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:9504d82fd7b4691fdc61b00f2e8ae15e28665fce17c60cf44655ccd60cf09f36
|
| 3 |
+
size 69808
|
effectSound/est_num_downloads_effectSound.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:cd69b4b945f61331c7778a7ff3366a856191beff40ed439ed78705c1f94440ef
|
| 3 |
+
size 831
|
effectSound/scaler_effectSamplerate.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:5ac8d3018ca0d1477592952a1aa6b9d582ad589c46314854efd56b607d175b3a
|
| 3 |
+
size 879
|
effectSound/scaler_effectSound_age_days_log.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:332ee96e7bca4c412bc0d5ac20c0876d5bf8304142d4fd57d4d5524e03228e61
|
| 3 |
+
size 895
|
effectSound/username_freq_dict_effectSound.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:457517d900b3c05061f398d37b00f8087ae9edb1a4776c7cbc2fc77fa60a4036
|
| 3 |
+
size 209269
|
music/avg_rating_transformer_music.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:29e054a504fd9193b232384f5a2799cad18e54903f03cbfc999a77547feff2d2
|
| 3 |
+
size 944
|
music/est_num_downloads_music.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:435f88fd8e8f46a970b39b2f255920c298c20e41cf558276dae8b09a40bd56be
|
| 3 |
+
size 831
|
music/music_onehot_cols.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:554cb8135c47967de9f480942f6d09c79b2ac8440adecebd1dd8c013444d195a
|
| 3 |
+
size 771
|
music/music_onehot_tags.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:b8d510dc14604d2d69333e144cc3212ecb3b446d5192f15940347d65610e6eb1
|
| 3 |
+
size 36877
|
music/music_subcategory_cols.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:a51f89fa69f26d5785cd8518fc594ceffbc959493572ac9b06162bfd4f509247
|
| 3 |
+
size 377
|
music/music_xgb_avg_rating.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:528b63dab12f2d20b07086f7d7b1a8747fbc09798d5c6a199185cec57bda823d
|
| 3 |
+
size 7961465
|
music/music_xgb_model_smote_balanced_avg_rating.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:f9266eb3b73bbe34dcbbf84a5cefd758b8dae561f1ee7abd11ea9e79dcb9a756
|
| 3 |
+
size 4144472
|
music/music_xgb_model_smote_balanced_num_downloads.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:fca062c7c044eeb44c0acaaad0f1ee91ff79b733d877d7893795580c74b68f87
|
| 3 |
+
size 5322685
|
music/scaler_music_age_days_log.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:1941f5f48e21243a939080d9d7a1cedc677e2b0b813a451a50f64d00ce149588
|
| 3 |
+
size 895
|
music/scaler_music_samplerate.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:0339152b44141d57f6be072c67c50c074d7b6e12280a57f0434520188af83483
|
| 3 |
+
size 879
|
music/username_freq_dict_music.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:f6ca2078e0e0c5c0d5f871362bba1e787c6860fb547dd1f9f3c4f0f3c366b447
|
| 3 |
+
size 214933
|
requirements.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio==6.5.0
|
| 2 |
+
scikit-learn
|
| 3 |
+
numpy
|
| 4 |
+
pandas
|
| 5 |
+
gensim
|
| 6 |
+
pytz
|
| 7 |
+
git+https://github.com/MTG/freesound-python
|
| 8 |
+
|