Update app.py
Browse files
app.py
CHANGED
|
@@ -19,7 +19,7 @@ def load_class_map():
|
|
| 19 |
|
| 20 |
class_names = load_class_map()
|
| 21 |
|
| 22 |
-
# Classification function
|
| 23 |
def classify_audio(audio, sample_rate):
|
| 24 |
try:
|
| 25 |
# Convert stereo to mono
|
|
@@ -29,7 +29,7 @@ def classify_audio(audio, sample_rate):
|
|
| 29 |
# Normalize
|
| 30 |
audio = audio / np.max(np.abs(audio))
|
| 31 |
|
| 32 |
-
# Resample if needed
|
| 33 |
target_sr = 16000
|
| 34 |
if sample_rate != target_sr:
|
| 35 |
duration = audio.shape[0] / sample_rate
|
|
@@ -47,7 +47,7 @@ def classify_audio(audio, sample_rate):
|
|
| 47 |
|
| 48 |
# Extract predictions
|
| 49 |
top_prediction = class_names[top_5[0]]
|
| 50 |
-
top_scores = {class_names[i]: float(mean_scores[i]) for i in top_5
|
| 51 |
|
| 52 |
# Create waveform plot
|
| 53 |
fig, ax = plt.subplots()
|
|
@@ -62,7 +62,7 @@ def classify_audio(audio, sample_rate):
|
|
| 62 |
except Exception as e:
|
| 63 |
return f"Error: {str(e)}", {}, None
|
| 64 |
|
| 65 |
-
# Gradio Interface (
|
| 66 |
interface = gr.Interface(
|
| 67 |
fn=classify_audio,
|
| 68 |
inputs=gr.Audio(source="upload", type="numpy", label="Upload .wav or .mp3"),
|
|
|
|
| 19 |
|
| 20 |
class_names = load_class_map()
|
| 21 |
|
| 22 |
+
# Classification function
|
| 23 |
def classify_audio(audio, sample_rate):
|
| 24 |
try:
|
| 25 |
# Convert stereo to mono
|
|
|
|
| 29 |
# Normalize
|
| 30 |
audio = audio / np.max(np.abs(audio))
|
| 31 |
|
| 32 |
+
# Resample to 16kHz if needed
|
| 33 |
target_sr = 16000
|
| 34 |
if sample_rate != target_sr:
|
| 35 |
duration = audio.shape[0] / sample_rate
|
|
|
|
| 47 |
|
| 48 |
# Extract predictions
|
| 49 |
top_prediction = class_names[top_5[0]]
|
| 50 |
+
top_scores = {class_names[i]: float(mean_scores[i]) for i in top_5}
|
| 51 |
|
| 52 |
# Create waveform plot
|
| 53 |
fig, ax = plt.subplots()
|
|
|
|
| 62 |
except Exception as e:
|
| 63 |
return f"Error: {str(e)}", {}, None
|
| 64 |
|
| 65 |
+
# Gradio Interface (binary audio compatible for n8n)
|
| 66 |
interface = gr.Interface(
|
| 67 |
fn=classify_audio,
|
| 68 |
inputs=gr.Audio(source="upload", type="numpy", label="Upload .wav or .mp3"),
|