Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,104 +2,103 @@ import gradio as gr
|
|
| 2 |
import pandas as pd
|
| 3 |
import freesound
|
| 4 |
|
| 5 |
-
#
|
| 6 |
API_TOKEN = "zE9NjEOgUMzH9K7mjiGBaPJiNwJLjSM53LevarRK"
|
| 7 |
|
| 8 |
-
# Initialiser le client FreeSound
|
| 9 |
client = freesound.FreesoundClient()
|
| 10 |
client.set_token(API_TOKEN, "token")
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
def
|
| 14 |
-
if
|
| 15 |
return ""
|
| 16 |
-
if isinstance(
|
| 17 |
-
return
|
| 18 |
-
return
|
| 19 |
|
| 20 |
-
#
|
| 21 |
-
def
|
| 22 |
-
if not url.strip():
|
| 23 |
-
return pd.DataFrame() # retourne un DataFrame vide si aucun URL
|
| 24 |
-
|
| 25 |
try:
|
| 26 |
-
# Extraire l'ID du son depuis l'URL
|
| 27 |
sound_id = int(url.rstrip("/").split("/")[-1])
|
| 28 |
sound = client.get_sound(sound_id)
|
| 29 |
-
|
| 30 |
-
#
|
|
|
|
|
|
|
| 31 |
sound_data = {
|
|
|
|
| 32 |
"id": sound.id,
|
| 33 |
-
"num_downloads":
|
| 34 |
-
"avg_rating":
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
"
|
| 38 |
-
"
|
| 39 |
-
"
|
| 40 |
-
"
|
| 41 |
-
"
|
| 42 |
-
"
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
"
|
| 46 |
-
"
|
| 47 |
-
"
|
| 48 |
-
"
|
| 49 |
-
"
|
| 50 |
-
"
|
| 51 |
-
"
|
| 52 |
-
"
|
| 53 |
-
"
|
| 54 |
-
"
|
| 55 |
-
"
|
| 56 |
-
"
|
| 57 |
-
"
|
| 58 |
-
|
| 59 |
-
"
|
| 60 |
-
"
|
| 61 |
-
"
|
| 62 |
-
"
|
| 63 |
-
"
|
| 64 |
-
"
|
| 65 |
-
"
|
| 66 |
-
"
|
| 67 |
-
"
|
| 68 |
-
"
|
| 69 |
-
|
| 70 |
-
"
|
| 71 |
-
"
|
| 72 |
-
"
|
| 73 |
-
"
|
| 74 |
-
"
|
| 75 |
-
"
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
"
|
| 79 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
}
|
| 81 |
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
return df
|
| 85 |
-
|
| 86 |
except Exception as e:
|
| 87 |
return pd.DataFrame([{"Erreur": str(e)}])
|
| 88 |
|
| 89 |
-
#
|
| 90 |
-
with gr.Blocks(title="FreeSound Audio
|
| 91 |
-
gr.Markdown("# 🎧 FreeSound Audio
|
| 92 |
-
gr.Markdown("
|
| 93 |
-
|
| 94 |
-
url_input = gr.Textbox(
|
| 95 |
-
label="URL du son FreeSound",
|
| 96 |
-
placeholder="https://freesound.org/people/..."
|
| 97 |
-
)
|
| 98 |
|
| 99 |
-
|
| 100 |
-
|
|
|
|
| 101 |
|
| 102 |
-
|
| 103 |
|
| 104 |
demo.launch()
|
| 105 |
-
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
import freesound
|
| 4 |
|
| 5 |
+
# ========= CONFIG =========
|
| 6 |
API_TOKEN = "zE9NjEOgUMzH9K7mjiGBaPJiNwJLjSM53LevarRK"
|
| 7 |
|
|
|
|
| 8 |
client = freesound.FreesoundClient()
|
| 9 |
client.set_token(API_TOKEN, "token")
|
| 10 |
|
| 11 |
+
# ========= UTILS =========
|
| 12 |
+
def to_str(v):
|
| 13 |
+
if v is None:
|
| 14 |
return ""
|
| 15 |
+
if isinstance(v, (list, dict)):
|
| 16 |
+
return str(v)
|
| 17 |
+
return v
|
| 18 |
|
| 19 |
+
# ========= MAIN FUNCTION =========
|
| 20 |
+
def extract_features(url):
|
|
|
|
|
|
|
|
|
|
| 21 |
try:
|
|
|
|
| 22 |
sound_id = int(url.rstrip("/").split("/")[-1])
|
| 23 |
sound = client.get_sound(sound_id)
|
| 24 |
+
|
| 25 |
+
# 🔥 C'EST ICI QUE TOUT SE PASSE
|
| 26 |
+
analysis = sound.get_analysis()
|
| 27 |
+
|
| 28 |
sound_data = {
|
| 29 |
+
# ===== META =====
|
| 30 |
"id": sound.id,
|
| 31 |
+
"num_downloads": sound.num_downloads,
|
| 32 |
+
"avg_rating": sound.avg_rating,
|
| 33 |
+
|
| 34 |
+
# ===== RHYTHM =====
|
| 35 |
+
"amplitude_peak_ratio": analysis.lowlevel.get("amplitude_peak_ratio"),
|
| 36 |
+
"beat_count": analysis.rhythm.get("beats_count"),
|
| 37 |
+
"beat_loudness": list_to_str(analysis.rhythm.get("beats_loudness")),
|
| 38 |
+
"beat_times": list_to_str(analysis.rhythm.get("beats_position")),
|
| 39 |
+
"bpm": analysis.rhythm.get("bpm"),
|
| 40 |
+
"bpm_confidence": analysis.rhythm.get("bpm_confidence"),
|
| 41 |
+
|
| 42 |
+
# ===== LOWLEVEL =====
|
| 43 |
+
"boominess": analysis.lowlevel.get("boominess"),
|
| 44 |
+
"brightness": analysis.lowlevel.get("brightness"),
|
| 45 |
+
"decay_strength": analysis.lowlevel.get("decay_strength"),
|
| 46 |
+
"inharmonicity": analysis.lowlevel.get("inharmonicity"),
|
| 47 |
+
"mfcc": list_to_str(analysis.lowlevel.get("mfcc")),
|
| 48 |
+
"onset_count": analysis.lowlevel.get("onsets_count"),
|
| 49 |
+
"onset_times": list_to_str(analysis.lowlevel.get("onsets_position")),
|
| 50 |
+
"pitch": analysis.lowlevel.get("pitch"),
|
| 51 |
+
"pitch_max": analysis.lowlevel.get("pitch_max"),
|
| 52 |
+
"pitch_min": analysis.lowlevel.get("pitch_min"),
|
| 53 |
+
"pitch_salience": analysis.lowlevel.get("pitch_salience"),
|
| 54 |
+
"pitch_var": analysis.lowlevel.get("pitch_var"),
|
| 55 |
+
"silence_rate": analysis.lowlevel.get("silence_rate"),
|
| 56 |
+
|
| 57 |
+
"spectral_centroid": analysis.lowlevel.get("spectral_centroid"),
|
| 58 |
+
"spectral_complexity": analysis.lowlevel.get("spectral_complexity"),
|
| 59 |
+
"spectral_crest": analysis.lowlevel.get("spectral_crest"),
|
| 60 |
+
"spectral_energy": analysis.lowlevel.get("spectral_energy"),
|
| 61 |
+
"spectral_entropy": analysis.lowlevel.get("spectral_entropy"),
|
| 62 |
+
"spectral_flatness": analysis.lowlevel.get("spectral_flatness"),
|
| 63 |
+
"spectral_rolloff": analysis.lowlevel.get("spectral_rolloff"),
|
| 64 |
+
"spectral_skewness": analysis.lowlevel.get("spectral_skewness"),
|
| 65 |
+
"spectral_spread": analysis.lowlevel.get("spectral_spread"),
|
| 66 |
+
"start_time": analysis.lowlevel.get("start_time"),
|
| 67 |
+
|
| 68 |
+
"temporal_centroid": analysis.lowlevel.get("temporal_centroid"),
|
| 69 |
+
"temporal_centroid_ratio": analysis.lowlevel.get("temporal_centroid_ratio"),
|
| 70 |
+
"temporal_decrease": analysis.lowlevel.get("temporal_decrease"),
|
| 71 |
+
"temporal_skewness": analysis.lowlevel.get("temporal_skewness"),
|
| 72 |
+
"temporal_spread": analysis.lowlevel.get("temporal_spread"),
|
| 73 |
+
"zero_crossing_rate": analysis.lowlevel.get("zerocrossingrate"),
|
| 74 |
+
|
| 75 |
+
# ===== TONAL =====
|
| 76 |
+
"chord_count": analysis.tonal.get("chords_number_rate"),
|
| 77 |
+
"chord_progression": list_to_str(analysis.tonal.get("chords_progression")),
|
| 78 |
+
"hpcp": list_to_str(analysis.tonal.get("hpcp")),
|
| 79 |
+
"hpcp_crest": analysis.tonal.get("hpcp_crest"),
|
| 80 |
+
"hpcp_entropy": analysis.tonal.get("hpcp_entropy"),
|
| 81 |
+
"note_confidence": list_to_str(analysis.tonal.get("notes_confidence")),
|
| 82 |
+
"note_midi": list_to_str(analysis.tonal.get("notes_midi")),
|
| 83 |
+
"note_name": list_to_str(analysis.tonal.get("notes_names")),
|
| 84 |
+
"tristimulus": analysis.tonal.get("tristimulus"),
|
| 85 |
+
"warmth": analysis.lowlevel.get("warmth"),
|
| 86 |
}
|
| 87 |
|
| 88 |
+
return pd.DataFrame([data])
|
| 89 |
+
|
|
|
|
|
|
|
| 90 |
except Exception as e:
|
| 91 |
return pd.DataFrame([{"Erreur": str(e)}])
|
| 92 |
|
| 93 |
+
# ========= GRADIO UI =========
|
| 94 |
+
with gr.Blocks(title="FreeSound Audio Feature Extractor") as demo:
|
| 95 |
+
gr.Markdown("## 🎧 FreeSound – Audio Feature Extractor (REAL)")
|
| 96 |
+
gr.Markdown("Colle une URL FreeSound → toutes les features audio s'affichent")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
+
url = gr.Textbox(label="URL du son", placeholder="https://freesound.org/s/123456/")
|
| 99 |
+
btn = gr.Button("🎼 Extraire features")
|
| 100 |
+
out = gr.Dataframe()
|
| 101 |
|
| 102 |
+
btn.click(extract_features, url, out)
|
| 103 |
|
| 104 |
demo.launch()
|
|
|