File size: 1,154 Bytes
d810b2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1812ed5
 
 
 
 
 
 
 
 
 
d810b2a
1812ed5
d810b2a
 
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
from transformers import pipeline
import gradio as gr

username = 'bvallegc' ## Complete your username
model_id = f"{username}/distilhubert-finetuned-gtzan"
pipe = pipeline("audio-classification", model=model_id)

def classify_audio(filepath):
    """
    Goes from
    [{'score': 0.8339303731918335, 'label': 'country'},
  {'score': 0.11914275586605072, 'label': 'rock'},]

   to
   {"country":  0.8339303731918335, "rock":0.11914275586605072}
  """
    preds = pipe(filepath)
    outputs = {}
    for p in preds:
        outputs[p["label"]] = p["score"]
    return outputs

interface_options = {
    "title": "Music Genre Classification",
    "description": "The audio classifier for those who are the best and only want and require the best",
    "interpretation": "default",
    "layout": "horizontal",
    # Audio from validation file
    "examples": ["000003.ogg", "000032.ogg", "000038.ogg", "000050.ogg", "000103.ogg"],
    "allow_flagging": "never"
}

demo = gr.Interface(
    fn=classify_audio, inputs=gr.Audio(type="filepath"), outputs=[gr.outputs.Label(), gr.Number(label="Prediction time (s)")], **interface_options
)
demo.launch(debug=True)