File size: 1,206 Bytes
bbb0227
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import librosa
from transformers import pipeline

pipe = pipeline("audio-classification", model="Shamik/whisper-base.en-finetuned-gtzan")

title = """Music Genre Classifier"""
description = """
Next time you think of how Shazam <img src="shzam.png" width=200px> finds the name of the song,
well it might certainly be classifying the genre of the music too. This tool classifies music based
on pre-defined genre from the [GTZAN](https://huggingface.co/datasets/marsyas/gtzan) dataset, 
which contains music from the following genres:
`blues, classical, country, disco, hiphop, jazz, metal, pop, reggae, and rock`.
"""

def classify_audio(filepath):
    audio, sampling_rate = librosa.load(filepath, sr=16_000)
    preds = pipe(audio)
    outputs = {}
    for p in preds:
        outputs[p["label"]] = p["score"]
    return outputs


label = gr.outputs.Label()

demo = gr.Interface(fn=classify_audio,
                     inputs=gr.Audio(type="filepath"),
                       outputs=label,
                       title=title,
                       description=description,
                         examples=[["song1.ogg"], ["song2.ogg"], ["song3.ogg"], ["song4.ogg"]],)
demo.launch()