Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
def extract_and_predict(url):
|
| 2 |
try:
|
| 3 |
sound_id = int(url.rstrip("/").split("/")[-1])
|
|
@@ -35,4 +95,15 @@ def extract_and_predict(url):
|
|
| 35 |
return pd.DataFrame([{"Erreur": "Durée non supportée pour prédiction", "Duration": duration}])
|
| 36 |
|
| 37 |
except Exception as e:
|
| 38 |
-
return pd.DataFrame([{"Erreur": str(e)}])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import freesound
|
| 4 |
+
import joblib
|
| 5 |
+
import xgboost as xgb
|
| 6 |
+
|
| 7 |
+
API_TOKEN = "zE9NjEOgUMzH9K7mjiGBaPJiNwJLjSM53LevarRK"
|
| 8 |
+
client = freesound.FreesoundClient()
|
| 9 |
+
client.set_token(API_TOKEN, "token")
|
| 10 |
+
|
| 11 |
+
# ----------------------------
|
| 12 |
+
# 1️⃣ Charger les modèles
|
| 13 |
+
# ----------------------------
|
| 14 |
+
# Music
|
| 15 |
+
xgb_music_num = joblib.load("xgb_num_downloads_music_model.pkl")
|
| 16 |
+
xgb_music_feat_num = joblib.load("xgb_num_downloads_music_features.pkl")
|
| 17 |
+
xgb_music_avg = joblib.load("xgb_avg_rating_music_model.pkl")
|
| 18 |
+
xgb_music_feat_avg = joblib.load("xgb_avg_rating_music_features.pkl")
|
| 19 |
+
le_music_avg = joblib.load("xgb_avg_rating_music_label_encoder.pkl")
|
| 20 |
+
|
| 21 |
+
# Effect Sound
|
| 22 |
+
xgb_effect_num = joblib.load("xgb_num_downloads_effectsound_model.pkl")
|
| 23 |
+
xgb_effect_feat_num = joblib.load("xgb_num_downloads_effectsound_features.pkl")
|
| 24 |
+
xgb_effect_avg = joblib.load("xgb_avg_rating_effectsound_model.pkl")
|
| 25 |
+
xgb_effect_feat_avg = joblib.load("xgb_avg_rating_effectsound_features.pkl")
|
| 26 |
+
le_effect_avg = joblib.load("xgb_avg_rating_effectsound_label_encoder.pkl")
|
| 27 |
+
|
| 28 |
+
# ----------------------------
|
| 29 |
+
# 2️⃣ Fonctions utilitaires
|
| 30 |
+
# ----------------------------
|
| 31 |
+
def safe_float(v):
|
| 32 |
+
try:
|
| 33 |
+
return float(v)
|
| 34 |
+
except:
|
| 35 |
+
return 0.0
|
| 36 |
+
|
| 37 |
+
def predict_with_model(model, features, feat_list, le=None):
|
| 38 |
+
# Préparer la ligne
|
| 39 |
+
row = []
|
| 40 |
+
for col in feat_list:
|
| 41 |
+
val = features.get(col, 0)
|
| 42 |
+
if val is None or isinstance(val, (list, dict)):
|
| 43 |
+
val = 0
|
| 44 |
+
row.append(safe_float(val))
|
| 45 |
+
|
| 46 |
+
X = pd.DataFrame([row], columns=feat_list)
|
| 47 |
+
|
| 48 |
+
# Transformer en DMatrix
|
| 49 |
+
dmatrix = xgb.DMatrix(X.values, feature_names=feat_list)
|
| 50 |
+
|
| 51 |
+
# Prédiction
|
| 52 |
+
pred_int = int(model.get_booster().predict(dmatrix)[0])
|
| 53 |
+
|
| 54 |
+
if le:
|
| 55 |
+
return le.inverse_transform([pred_int])[0]
|
| 56 |
+
return pred_int
|
| 57 |
+
|
| 58 |
+
# ----------------------------
|
| 59 |
+
# 3️⃣ Extraction + prédiction
|
| 60 |
+
# ----------------------------
|
| 61 |
def extract_and_predict(url):
|
| 62 |
try:
|
| 63 |
sound_id = int(url.rstrip("/").split("/")[-1])
|
|
|
|
| 95 |
return pd.DataFrame([{"Erreur": "Durée non supportée pour prédiction", "Duration": duration}])
|
| 96 |
|
| 97 |
except Exception as e:
|
| 98 |
+
return pd.DataFrame([{"Erreur": str(e)}])
|
| 99 |
+
# ----------------------------
|
| 100 |
+
# 4️⃣ Interface Gradio
|
| 101 |
+
# ----------------------------
|
| 102 |
+
with gr.Blocks() as demo:
|
| 103 |
+
gr.Markdown("## 🎧 FreeSound – Prédiction XGBoost (DMatrix)")
|
| 104 |
+
url = gr.Textbox(label="URL FreeSound")
|
| 105 |
+
btn = gr.Button("Prédire")
|
| 106 |
+
out = gr.Dataframe()
|
| 107 |
+
btn.click(extract_and_predict, url, out)
|
| 108 |
+
|
| 109 |
+
demo.launch()
|