File size: 1,462 Bytes
4862fc7
 
 
 
 
94a728f
4862fc7
 
 
 
 
 
 
 
 
 
 
 
 
96fc794
 
 
 
4862fc7
96fc794
4862fc7
 
 
96fc794
 
 
 
 
4862fc7
e7b15aa
4862fc7
 
23a184a
4862fc7
 
23a184a
96fc794
e7b15aa
4862fc7
 
 
ce646f6
4862fc7
 
 
23a184a
0b7d49b
ce646f6
4862fc7
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
55
import gradio as gr
import torch
from transformers import pipeline

username = "ardneebwar"  ## Complete your username
model_id = "sanchit-gandhi/distilhubert-finetuned-gtzan"
device = "cuda:0" if torch.cuda.is_available() else "cpu"
pipe = pipeline("audio-classification", model=model_id, device=device)

# def predict_trunc(filepath):
#     preprocessed = pipe.preprocess(filepath)
#     truncated = pipe.feature_extractor.pad(preprocessed,truncation=True, max_length = 16_000*30)
#     model_outputs = pipe.forward(truncated)
#     outputs = pipe.postprocess(model_outputs)

#     return outputs


def classify_audio(filepath):
    import time
    start_time = time.time()
    
    # Assuming `pipe` is your model pipeline for inference
    preds = pipe(filepath)
    
    outputs = {}
    for p in preds:
        outputs[p["label"]] = p["score"]
    
    end_time = time.time()
    prediction_time = end_time - start_time
    
    return outputs, prediction_time


title = "🎵 Music Genre Classifier"
description = """
Music Genre Classifier model (Fine-tuned "ntu-spml/distilhubert") Dataset: [GTZAN](https://huggingface.co/datasets/marsyas/gtzan)
"""

filenames = ['rock-it-21275.mp3']
filenames = [f"./{f}" for f in filenames]

demo = gr.Interface(
    fn=classify_audio,
    inputs=gr.Audio(type="filepath"),
    outputs=[gr.Label(), gr.Number(label="Prediction time (s)")],
    title=title,
    description=description,
)

demo.queue()

demo.launch()