CoolFace
Apppublic

zydfx/Audio_Classification

sourceHugging Faceopenrailupdated 3y agoView on Hugging Face
0likes
app.py45 linesDownload Raw Back to root
1import torch2import librosa3import gradio as gr4from model import Conv1DNet5 6# 加载模型7model = torch.load("model.model",map_location=torch.device('cpu'))8model = model.to('cpu')9model.eval()10 11# 定义函数12def predict(audio_file):13    labels=['down', 'go', 'left', 'no', 'off', 'on', 'right', 'stop', 'up', 'yes']14    # 加载音频文件15    samples, sample_rate = librosa.load(audio_file, sr = 8000)16 17    # 预处理音频18    x = torch.tensor(samples)19    x = torch.unsqueeze(x,0)20    y_pred = model(x)21    result = labels[torch.argmax(y_pred)]22    return result23 24# 创建界面25inputs = gr.inputs.Audio(type="filepath")26outputs = gr.outputs.Textbox(label="Result")27interface = gr.Interface(28    fn=predict,29    inputs=inputs,30    outputs=outputs,31    # 设置输入参数示例32    examples=[33        "go.wav",34        "stop.wav"35    ],36    title="Audio Classification",37    description="""This is a simple audio classification demo using Python, Librosa, and PyTorch!38                    Choose an audio file  to classify it as one of following categories:39                      'down', 'go', 'left', 'no', 'off', 'on', 'right', 'stop', 'up', 'yes'40                """,41    layout="horizontal",42    )43 44# 运行界面45interface.launch()