KawtarTL commited on
Commit
aea812d
·
verified ·
1 Parent(s): 5d191ed

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -80
app.py CHANGED
@@ -2,104 +2,103 @@ import gradio as gr
2
  import pandas as pd
3
  import freesound
4
 
5
- # -------- Configuration --------
6
  API_TOKEN = "zE9NjEOgUMzH9K7mjiGBaPJiNwJLjSM53LevarRK"
7
 
8
- # Initialiser le client FreeSound
9
  client = freesound.FreesoundClient()
10
  client.set_token(API_TOKEN, "token")
11
 
12
- # -------- Fonction utilitaire pour convertir listes en chaînes --------
13
- def list_to_str(val):
14
- if val is None:
15
  return ""
16
- if isinstance(val, list):
17
- return ",".join(map(str, val))
18
- return str(val)
19
 
20
- # -------- Fonction Gradio --------
21
- def predict_with_audio(url):
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
- # Construire le dict (sans télécharger la preview)
 
 
31
  sound_data = {
 
32
  "id": sound.id,
33
- "num_downloads": getattr(sound, "num_downloads", ""),
34
- "avg_rating": getattr(sound, "avg_rating", ""),
35
- "amplitude_peak_ratio": list_to_str(getattr(sound, "amplitude_peak_ratio", "")),
36
- "beat_count": list_to_str(getattr(sound, "beat_count", "")),
37
- "beat_loudness": list_to_str(getattr(sound, "beat_loudness", "")),
38
- "beat_times": list_to_str(getattr(sound, "beat_times", "")),
39
- "boominess": getattr(sound, "boominess", ""),
40
- "bpm": getattr(sound, "bpm", ""),
41
- "bpm_confidence": getattr(sound, "bpm_confidence", ""),
42
- "brightness": getattr(sound, "brightness", ""),
43
- "chord_count": getattr(sound, "chord_count", ""),
44
- "chord_progression": list_to_str(getattr(sound, "chord_progression", "")),
45
- "decay_strength": getattr(sound, "decay_strength", ""),
46
- "hpcp": list_to_str(getattr(sound, "hpcp", "")),
47
- "hpcp_crest": getattr(sound, "hpcp_crest", ""),
48
- "hpcp_entropy": getattr(sound, "hpcp_entropy", ""),
49
- "inharmonicity": getattr(sound, "inharmonicity", ""),
50
- "mfcc": list_to_str(getattr(sound, "mfcc", "")),
51
- "note_confidence": list_to_str(getattr(sound, "note_confidence", "")),
52
- "note_midi": list_to_str(getattr(sound, "note_midi", "")),
53
- "note_name": list_to_str(getattr(sound, "note_name", "")),
54
- "onset_count": getattr(sound, "onset_count", ""),
55
- "onset_times": list_to_str(getattr(sound, "onset_times", "")),
56
- "pitch": getattr(sound, "pitch", ""),
57
- "pitch_max": getattr(sound, "pitch_max", ""),
58
- "pitch_min": getattr(sound, "pitch_min", ""),
59
- "pitch_salience": getattr(sound, "pitch_salience", ""),
60
- "pitch_var": getattr(sound, "pitch_var", ""),
61
- "silence_rate": getattr(sound, "silence_rate", ""),
62
- "spectral_centroid": getattr(sound, "spectral_centroid", ""),
63
- "spectral_complexity": getattr(sound, "spectral_complexity", ""),
64
- "spectral_crest": getattr(sound, "spectral_crest", ""),
65
- "spectral_energy": getattr(sound, "spectral_energy", ""),
66
- "spectral_entropy": getattr(sound, "spectral_entropy", ""),
67
- "spectral_flatness": getattr(sound, "spectral_flatness", ""),
68
- "spectral_rolloff": getattr(sound, "spectral_rolloff", ""),
69
- "spectral_skewness": getattr(sound, "spectral_skewness", ""),
70
- "spectral_spread": getattr(sound, "spectral_spread", ""),
71
- "start_time": getattr(sound, "start_time", ""),
72
- "temporal_centroid": getattr(sound, "temporal_centroid", ""),
73
- "temporal_centroid_ratio": getattr(sound, "temporal_centroid_ratio", ""),
74
- "temporal_decrease": getattr(sound, "temporal_decrease", ""),
75
- "temporal_skewness": getattr(sound, "temporal_skewness", ""),
76
- "temporal_spread": getattr(sound, "temporal_spread", ""),
77
- "tristimulus": getattr(sound, "tristimulus", ""),
78
- "warmth": getattr(sound, "warmth", ""),
79
- "zero_crossing_rate": getattr(sound, "zero_crossing_rate", "")
 
 
 
 
 
 
 
 
80
  }
81
 
82
- # Convertir en DataFrame pour Gradio
83
- df = pd.DataFrame([sound_data])
84
- return df
85
-
86
  except Exception as e:
87
  return pd.DataFrame([{"Erreur": str(e)}])
88
 
89
- # -------- Interface Gradio --------
90
- with gr.Blocks(title="FreeSound Audio Features") as demo:
91
- gr.Markdown("# 🎧 FreeSound Audio Features Extractor")
92
- gr.Markdown("Collez l'URL d'un son FreeSound pour récupérer ses features audio internes.")
93
-
94
- url_input = gr.Textbox(
95
- label="URL du son FreeSound",
96
- placeholder="https://freesound.org/people/..."
97
- )
98
 
99
- btn_audio = gr.Button("🎼 Extraire features audio")
100
- output = gr.Dataframe(headers=None) # accepte automatiquement toutes les colonnes du DataFrame
 
101
 
102
- btn_audio.click(fn=predict_with_audio, inputs=url_input, outputs=output)
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()