File size: 1,587 Bytes
0f36b7e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from transformers import pipeline
import gradio as gr

model_id = "artyomboyko/distilhubert-finetuned-gtzan"
pipe = pipeline("audio-classification", model=model_id)

def classify_audio(filepath):
    preds = pipe(filepath)
    outputs = {}
    for p in preds:
        outputs[p["label"]] = p["score"]
    return outputs


demo = gr.Blocks()

title = "Classification of music"
description = """
This demo is designed to test the music classification. It is important to remember that music classification depends very much on the quality of the recording, for example, when classifying a recording from a 
microphone with poor high frequencies, the song "Evanescence - bring me to life" may not be correctly defined as classical music. But if we transfer a recording of the same song as a file, it is correctly 
identified as metal music. In addition, a real problem is caused by compositions in which there is a mixture of different styles of music, or modern styles of examples of which were not in the training dataset.
"""

mic_classify_audio = gr.Interface(
    fn=classify_audio,
    inputs=gr.Audio(source="microphone", type="filepath"),
    outputs=gr.outputs.Label(),
    title=title,
    description=description,
)

file_classify_audio = gr.Interface(
    fn=classify_audio,
    inputs=gr.Audio(source="upload", type="filepath"),
    outputs=gr.outputs.Label(),
    #examples=[["./example.wav"]],
    title=title,
    description=description,
)

with demo:
    gr.TabbedInterface([mic_classify_audio, file_classify_audio], ["Microphone", "Audio File"])

demo.launch(debug=True)