Spaces:
Build error
Build error
File size: 2,310 Bytes
0d6426a aa314ec 0d6426a |
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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# -*- coding: UTF-8 -*-
import gradio as gr
import torch, torchaudio
from timeit import default_timer as timer
from torchaudio.transforms import Resample
from models.model import HarmonicCNN
device = "cuda" if torch.cuda.is_available() else "cpu"
SAMPLE_RATE = 16000
AUDIO_LEN = 2.90
model = HarmonicCNN()
S = torch.load('models/best_model.pth', map_location=torch.device('cpu'))
model.load_state_dict(S)
LABELS = [
"alternative",
"ambient",
"atmospheric",
"chillout",
"classical",
"dance",
"downtempo",
"easylistening",
"electronic",
"experimental",
"folk",
"funk",
"hiphop",
"house",
"indie",
"instrumentalpop",
"jazz",
"lounge",
"metal",
"newage",
"orchestral",
"pop",
"popfolk",
"poprock",
"reggae",
"rock",
"soundtrack",
"techno",
"trance",
"triphop",
"world",
"acousticguitar",
"bass",
"computer",
"drummachine",
"drums",
"electricguitar",
"electricpiano",
"guitar",
"keyboard",
"piano",
"strings",
"synthesizer",
"violin",
"voice",
"emotional",
"energetic",
"film",
"happy",
"relaxing"
]
example_list = [
"samples/guitar_acoustic.wav",
"samples/guitar_electric.wav",
"samples/piano.wav",
"samples/violin.wav",
"samples/flute.wav"
]
def predict(audio_path):
start_time = timer()
wav, sample_rate = torchaudio.load(audio_path)
if sample_rate > SAMPLE_RATE:
resampler = Resample(sample_rate, SAMPLE_RATE)
wav = resampler(wav)
if wav.shape[0] >= 2:
wav = torch.mean(wav, dim=0)
wav = wav.unsqueeze(0)
model.eval()
with torch.inference_mode():
pred_probs = model(wav)
pred_labels_and_probs = {LABELS[i]: float(pred_probs[0][i]) for i in range(len(LABELS))}
pred_time = round(timer() - start_time, 5)
return pred_labels_and_probs, pred_time
title = "Music Tagging"
demo = gr.Interface(fn=predict,
inputs=gr.Audio(type="filepath"),
outputs=[gr.Label(num_top_classes=10, label="Predictions"),
gr.Number(label="Prediction time (s)")],
examples=example_list,
title=title)
demo.launch(debug=False) |