Update app.py
Browse files
app.py
CHANGED
|
@@ -18,20 +18,26 @@ else:
|
|
| 18 |
# Initialize the audio classification pipeline with the MIT model
|
| 19 |
pipe = pipeline("audio-classification", model="MIT/ast-finetuned-audioset-10-10-0.4593")
|
| 20 |
|
| 21 |
-
# Define the function to classify an audio file
|
| 22 |
def classify_audio(audio):
|
| 23 |
result = pipe(audio)
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
# Set up the Gradio interface
|
|
|
|
|
|
|
| 27 |
app = gr.Interface(
|
| 28 |
fn=classify_audio, # Function to classify audio
|
| 29 |
inputs=gr.Audio(type="filepath"), # Input for uploading an audio file
|
| 30 |
-
outputs=gr.Label(
|
| 31 |
title="Audio Classification", # App title
|
| 32 |
description="Upload an audio file to classify it using MIT's fine-tuned AudioSet model."
|
| 33 |
)
|
| 34 |
|
| 35 |
# Launch the app
|
| 36 |
if __name__ == "__main__":
|
| 37 |
-
app.launch()
|
|
|
|
| 18 |
# Initialize the audio classification pipeline with the MIT model
|
| 19 |
pipe = pipeline("audio-classification", model="MIT/ast-finetuned-audioset-10-10-0.4593")
|
| 20 |
|
| 21 |
+
# Define the function to classify an audio file and return the top 3 results
|
| 22 |
def classify_audio(audio):
|
| 23 |
result = pipe(audio)
|
| 24 |
+
# The pipeline returns a list of dicts sorted by score.
|
| 25 |
+
# We select the top 3 results here.
|
| 26 |
+
top_3_results = result[:3]
|
| 27 |
+
# Convert the list of dicts to a single dictionary for the Label component
|
| 28 |
+
return {label['label']: label['score'] for label in top_3_results}
|
| 29 |
|
| 30 |
# Set up the Gradio interface
|
| 31 |
+
# We removed `num_top_classes=3` from `gr.Label` and instead handle the
|
| 32 |
+
# top-3 logic inside the `classify_audio` function. This avoids the bug.
|
| 33 |
app = gr.Interface(
|
| 34 |
fn=classify_audio, # Function to classify audio
|
| 35 |
inputs=gr.Audio(type="filepath"), # Input for uploading an audio file
|
| 36 |
+
outputs=gr.Label(), # Output Label will display the dictionary from the function
|
| 37 |
title="Audio Classification", # App title
|
| 38 |
description="Upload an audio file to classify it using MIT's fine-tuned AudioSet model."
|
| 39 |
)
|
| 40 |
|
| 41 |
# Launch the app
|
| 42 |
if __name__ == "__main__":
|
| 43 |
+
app.launch()
|