File size: 1,035 Bytes
d1f1420
8e765bd
 
d1f1420
 
 
 
 
 
 
 
 
 
 
 
 
 
b68e5c4
d1f1420
 
 
 
b68e5c4
 
d1f1420
 
6144259
4b6701b
d1f1420
 
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
from transformers import pipeline
import gradio as gr
from timeit import default_timer as timer

username = "afern24" ## 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}
  """
    start_time = timer()
    preds = pipe(filepath)
    outputs = {}
    for p in preds:
        outputs[p["label"]] = p["score"]
    pred_time = round(timer() - start_time, 5)
    return outputs, pred_time

demo = gr.Interface(
    fn=classify_audio, inputs=gr.Audio(type="filepath"), outputs=[gr.Label(label="Predictions"), gr.Number(label="Prediction time (s)")], title="Distilhubert-finetuned-gtzan",
    description="Audio classifier based on music genres, for more check out my github ar www.github.com/Ferno22"
)
demo.launch(debug=True)