File size: 1,330 Bytes
b868159
7b1bd83
 
b868159
7b1bd83
e25ed3d
7b1bd83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f80e4ee
7b1bd83
 
2858f8a
7b1bd83
 
 
 
f80e4ee
 
7b1bd83
 
f80e4ee
 
 
7b1bd83
 
 
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
import gradio as gr
import torch
from transformers import pipeline

username = "ardneebwar"  ## Complete your username
model_id = f"{username}/wav2vec2-animal-sounds-finetuned-hubert-finetuned-animals"
device = "cuda:0" if torch.cuda.is_available() else "cpu"
pipe = pipeline("audio-classification", model=model_id, device=device)


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 = "🎵 Animal Sound Classifier"
description = """
Animal Sound Classifier model (Fine-tuned "ntu-spml/distilhubert") | Dataset: ESC-50 from Github (only the animal sounds) | Better to use audios 5 seconds long.
"""

filenames = ['cat.wav']
filenames = [f"./{f}" for f in filenames]

demo = gr.Interface(
    fn=classify_audio,
    inputs=gr.Audio(type="filepath", label="Upload your audio file"),
    outputs=[gr.Label(label="Predicted Animal Sound"), gr.Number(label="Prediction time (s)")],
    title=title,
    description=description,
    theme="huggingface",
    examples=[("cat.wav")],
    live=False
)

demo.launch()