Spaces:
Runtime error
Runtime error
import gradio as gr | |
import torch | |
from transformers import pipeline | |
username = "AescF" ## Complete your username | |
model_id = "AescF/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): | |
""" | |
Goes from | |
[{'score': 0.8339303731918335, 'label': 'country'}, | |
{'score': 0.11914275586605072, 'label': 'rock'},] | |
to | |
{"country": 0.8339303731918335, "rock":0.11914275586605072} | |
""" | |
preds = pipe(filepath) | |
# preds = predict_trunc(filepath) | |
outputs = {} | |
for p in preds: | |
outputs[p["label"]] = p["score"] | |
return outputs | |
title = "π΅ Music Genre Classifier" | |
description = """ | |
Demo for a music genre classifier trained on [GTZAN](https://huggingface.co/datasets/marsyas/gtzan) | |
For more info checkout [GITHUB](https://github.com/AEscF) | |
""" | |
filenames = ['blues.00098.wav', "disco.00020.wav", "classical.00075.wav","keyboard-153960.mp3"] | |
filenames = [[f"./{f}"] for f in filenames] | |
demo = gr.Interface( | |
fn=classify_audio, | |
inputs=gr.Audio(type="filepath"), | |
outputs=gr.outputs.Label(), | |
title=title, | |
description=description, | |
examples=filenames, | |
) | |
demo.launch() |