Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,17 +2,25 @@
|
|
| 2 |
import gradio as gr
|
| 3 |
from transformers import pipeline
|
| 4 |
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
def classify_audio(audio_file):
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
demo = gr.Interface(
|
| 11 |
fn=classify_audio,
|
| 12 |
-
inputs=gr.Audio(type="filepath"),
|
| 13 |
-
outputs=gr.Label(
|
| 14 |
-
title="Audio Classification",
|
| 15 |
-
description="Upload or record audio
|
| 16 |
)
|
| 17 |
|
| 18 |
if __name__ == "__main__":
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from transformers import pipeline
|
| 4 |
|
| 5 |
+
# Pretrained model for audio classification
|
| 6 |
+
MODEL = "superb/wav2vec2-base-superb-ks" # keyword spotting (yes, no, up, down...)
|
| 7 |
+
|
| 8 |
+
classifier = pipeline("audio-classification", model=MODEL)
|
| 9 |
|
| 10 |
def classify_audio(audio_file):
|
| 11 |
+
# audio_file is a tuple: (sample_rate, numpy_array) if "numpy", or path if "filepath"
|
| 12 |
+
if isinstance(audio_file, str): # filepath
|
| 13 |
+
return classifier(audio_file)
|
| 14 |
+
else: # (sr, data)
|
| 15 |
+
sr, data = audio_file
|
| 16 |
+
return classifier({"array": data, "sampling_rate": sr})
|
| 17 |
|
| 18 |
demo = gr.Interface(
|
| 19 |
fn=classify_audio,
|
| 20 |
+
inputs=gr.Audio(sources=["microphone", "upload"], type="filepath"),
|
| 21 |
+
outputs=gr.Label(),
|
| 22 |
+
title="🎵 Audio Classification",
|
| 23 |
+
description="Upload or record audio. Model: wav2vec2-base-superb-ks"
|
| 24 |
)
|
| 25 |
|
| 26 |
if __name__ == "__main__":
|