Spaces:
Runtime error
Runtime error
import torch | |
from transformers import pipeline | |
from transformers.pipelines.audio_utils import ffmpeg_read | |
import gradio as gr | |
MODEL_NAME = "JackismyShephard/whisper-medium.en-finetuned-gtzan" | |
device = 0 if torch.cuda.is_available() else "cpu" | |
pipe = pipeline( | |
task="audio-classification", | |
model=MODEL_NAME, | |
device=device, | |
) | |
def classify_audio(filepath): | |
preds = pipe(filepath) | |
outputs = {} | |
for p in preds: | |
outputs[p["label"]] = p["score"] | |
return outputs | |
demo = gr.Blocks() | |
file_classify = gr.Interface( | |
fn=classify_audio, | |
inputs= gr.Audio(sources=["upload"], label="Audio file", type="filepath"), | |
outputs=gr.Label(), | |
title="Classify Genre of Music", | |
description=( | |
"Classify long-form audio or microphone inputs with the click of a button! Demo uses the" | |
f" checkpoint [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and 🤗 Transformers to classify audio files" | |
" of arbitrary length." | |
), | |
examples="./examples", | |
cache_examples=True, | |
allow_flagging="never", | |
) | |
mic_classify = gr.Interface( | |
fn=classify_audio, | |
inputs= gr.Audio(sources=["microphone"], type="filepath"), | |
outputs= gr.Label(), | |
title="Classify Genre of Music", | |
description=( | |
"Classify long-form audio or microphone inputs with the click of a button! Demo uses the" | |
f" checkpoint [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) and 🤗 Transformers to classify audio files" | |
" of arbitrary length." | |
), | |
allow_flagging="never", | |
) | |
with demo: | |
gr.TabbedInterface([file_classify, mic_classify], ["Classify Audio File", "classify Microphone Input"]) | |
demo.launch() |