File size: 1,068 Bytes
8c72189
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e515109
970b919
e515109
c1ce43e
d1d5581
8c72189
 
 
 
 
c1ce43e
8c72189
 
 
 
c1ce43e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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.Interface(
    fn=classify_audio,
    inputs= gr.Audio(sources=["upload", "microphone"], label="Audio file", type="filepath"),
    outputs=gr.Label(),
    title="Music Genre Classification",
    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",
)

demo.launch()