Spaces:
Build error
Build error
# -*- coding: UTF-8 -*- | |
import gradio as gr | |
import torch, torchaudio | |
from timeit import default_timer as timer | |
from data_setups import audio_preprocess, resample | |
device = "cuda" if torch.cuda.is_available() else "cpu" | |
SAMPLE_RATE = 44100 | |
AUDIO_LEN = 2.90 | |
model = torch.load("models/torch_efficientnet_fold2_CNN.pth", map_location=torch.device('cpu')) | |
CHINESE_LABELS = [ | |
"大提琴", "單簧管", "長笛", "民謠吉他", "電吉他", "風琴", "鋼琴", "薩克斯風", "喇叭", "小提琴", "人聲" | |
] | |
LABELS = [ | |
"Cello", "Clarinet", "Flute", "Acoustic Guitar", "Electric Guitar", "Organ", "Piano", "Saxophone", "Trumpet", "Violin", "Voice" | |
] | |
example_list = [ | |
"samples/guitar_acoustic.wav", | |
"samples/piano.wav", | |
"samples/violin.wav", | |
"samples/flute.wav" | |
] | |
def predict(audio_path): | |
start_time = timer() | |
wavform, sample_rate = torchaudio.load(audio_path) | |
wav = resample(wavform, sample_rate, SAMPLE_RATE) | |
if len(wav) > int(AUDIO_LEN * SAMPLE_RATE): | |
wav = wav[:int(AUDIO_LEN * SAMPLE_RATE)] | |
else: | |
print(f"input length {len(wav)} too small!, need over {int(AUDIO_LEN * SAMPLE_RATE)}") | |
return | |
# input Preprocessing | |
img = audio_preprocess(wav, SAMPLE_RATE).unsqueeze(0) | |
model.eval() | |
with torch.inference_mode(): | |
pred_probs = torch.softmax(model(img), dim=1) | |
pred_labels_and_probs = {CHINESE_LABELS[i]: float(pred_probs[0][i]) for i in range(len(CHINESE_LABELS))} | |
pred_time = round(timer() - start_time, 5) | |
return pred_labels_and_probs, pred_time | |
title = "樂器辨識🎺🎸🎹🎻" | |
description = "使用IRMAS資料集訓練的深度學習模型,可辨識11種不同樂器,包含「大提琴, 單簧管, 長笛, 民謠吉他, 電吉他, 風琴, 鋼琴, 薩克斯風, 喇叭, 小提琴, 人聲」" | |
article = "" | |
demo = gr.Interface(fn=predict, | |
inputs=gr.Audio(type="filepath"), | |
outputs=[gr.Label(num_top_classes=11, label="Predictions"), | |
gr.Number(label="Prediction time (s)")], | |
examples=example_list, | |
title=title, | |
description=description, | |
article=article) | |
demo.launch(debug=False) |