Update app.py
Browse files
app.py
CHANGED
|
@@ -20,37 +20,26 @@ pipe = pipeline(
|
|
| 20 |
|
| 21 |
|
| 22 |
# --- Core Logic Function ---
|
| 23 |
-
def classify_audio(
|
| 24 |
"""
|
| 25 |
Classifies the audio, takes the top 3 predictions,
|
| 26 |
and formats them into a single, human-readable string.
|
| 27 |
"""
|
| 28 |
-
if
|
| 29 |
return "Please upload an audio file first."
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
# Format the output as a string instead of a dictionary
|
| 34 |
-
# This is the key change to fix the TypeError
|
| 35 |
-
output_str = ""
|
| 36 |
-
for i, pred in enumerate(preds[:3]):
|
| 37 |
-
label = pred["label"]
|
| 38 |
-
score = pred["score"]
|
| 39 |
-
output_str += f"{i+1}. {label}: {score:.4f}\n"
|
| 40 |
-
|
| 41 |
-
return output_str.strip()
|
| 42 |
-
|
| 43 |
|
| 44 |
# --- Gradio Interface ---
|
| 45 |
# Create the Gradio app interface
|
| 46 |
app = gr.Interface(
|
| 47 |
fn=classify_audio,
|
| 48 |
inputs=gr.Audio(type="filepath", label="Upload Audio File"),
|
| 49 |
-
outputs=gr.Label(
|
| 50 |
title="Audio Classification with MIT/AST",
|
| 51 |
description=(
|
| 52 |
"Upload an audio file to classify it. The model will identify the top 3 most likely sound categories. "
|
| 53 |
-
"This version is corrected to avoid common Gradio backend errors."
|
| 54 |
),
|
| 55 |
cache_examples=False,
|
| 56 |
)
|
|
|
|
| 20 |
|
| 21 |
|
| 22 |
# --- Core Logic Function ---
|
| 23 |
+
def classify_audio(audio):
|
| 24 |
"""
|
| 25 |
Classifies the audio, takes the top 3 predictions,
|
| 26 |
and formats them into a single, human-readable string.
|
| 27 |
"""
|
| 28 |
+
if audio is None:
|
| 29 |
return "Please upload an audio file first."
|
| 30 |
|
| 31 |
+
result = pipe(audio)
|
| 32 |
+
return {label['label']: label['score'] for label in result}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
# --- Gradio Interface ---
|
| 35 |
# Create the Gradio app interface
|
| 36 |
app = gr.Interface(
|
| 37 |
fn=classify_audio,
|
| 38 |
inputs=gr.Audio(type="filepath", label="Upload Audio File"),
|
| 39 |
+
outputs=gr.Label(num_top_classes=3), # This will now receive a simple string
|
| 40 |
title="Audio Classification with MIT/AST",
|
| 41 |
description=(
|
| 42 |
"Upload an audio file to classify it. The model will identify the top 3 most likely sound categories. "
|
|
|
|
| 43 |
),
|
| 44 |
cache_examples=False,
|
| 45 |
)
|