Spaces:
Sleeping
Sleeping
File size: 1,271 Bytes
d810b2a d5e6332 d810b2a df2bb17 3ae298e 812ed00 1eada99 d810b2a 1812ed5 d5e6332 e1b519a d810b2a 97df2cd |
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 |
from transformers import pipeline
import gradio
from gradio import Interface, Audio, Label, Number
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)
classification = [{"label": p["label"], "score": p["score"]} for p in preds]
label = classification[0]["label"]
number = classification[0]["score"]
return label, number
example_audio_files = [
"Freedom.mp3",
"In The Forest.mp3",
"Summer Mood.mp3",
]
examples = [{"filepath": path} for path in example_audio_files]
interface_options = {
"title": "Music Genre Classification",
"description": "The audio classifier for those who are the best and only want and require the best",
# Audio from validation file
"allow_flagging": "never"
}
demo = Interface(
fn=classify_audio, inputs= Audio(type="filepath", examples=examples), outputs=[Label(), Number()], **interface_options
)
demo.launch(debug=False) |