File size: 1,133 Bytes
6c7b6ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
"""Awesome UI for the Music Genre Classifier"""
import gradio as gr

from transformers import pipeline

classifier = pipeline(
    "audio-classification",
    model="mahimairaja/distilhubert-music-classifier-finetuned-gtzan",
)


def classifiy_music(audio):
    """
    Outputs the label for the given audio
    """
    label = classifier(audio)
    outputs = {}
    for pred_value in label:
        outputs[pred_value["label"]] = pred_value["score"]
    return outputs


TITLE = "Music Genre Classifier"

demo = gr.Blocks()

mic_translate = gr.Interface(
    fn=classifiy_music,
    inputs=gr.Audio(source="microphone", type="filepath"),
    outputs=gr.outputs.Label(),
    title=TITLE,
)

file_translate = gr.Interface(
    fn=classifiy_music,
    inputs=gr.Audio(source="upload", type="filepath"),
    outputs=gr.outputs.Label(),
    examples=[
        "assets/song_01.wav",
        "assets/song_02.wav",
        "assets/song_03.wav",
        "assets/song_04.wav",
    ],
    title=TITLE,
)

with demo:
    gr.TabbedInterface(
        [mic_translate, file_translate],
        ["Microphone", "Audio File"]
        )

demo.launch()