CoolFace
Apppublic

Armak/SED

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
app.py65 linesDownload Raw Back to root
1import warnings2 3import gradio as gr4import torch5import torchaudio6from app_utils import initialize_sed, load_config, load_model, make_prediction_on_audio7 8warnings.filterwarnings("ignore")9 10 11def gradio_inference(audio_file: str) -> str:12    """Perform sound event detection on an audio file and return the event plot.13 14    Args:15        audio_file (str): Path to the audio file.16 17    Returns:18        str: Path to the event plot.19    """20    config = load_config()21    sed, encoder = initialize_sed(config)22    sed = load_model(sed, model_path="chalky-protagonist.ckpt")23 24    audio_tensor, sr = torchaudio.load(audio_file)25 26    if audio_tensor.shape[0] == 2:27        audio_tensor = torch.mean(audio_tensor, dim=0)28 29    audio_tensor = audio_tensor.squeeze(0)30 31    if sr != config["data"]["fs"]:32        resampler = torchaudio.transforms.Resample(33            orig_freq=sr, new_freq=config["data"]["fs"]34        )35        audio_tensor = resampler(audio_tensor)36 37    vis, audio_container = make_prediction_on_audio(38        sed,39        audio_tensor,40        config,41        encoder,42        filename=audio_file,43        median_filter=24,44    )45 46    return_filename = "event_plot.png"47 48    vis.save(filename=return_filename)49 50    return return_filename51 52 53iface = gr.Interface(54    fn=gradio_inference,55    inputs=gr.Audio(type="filepath", label="Upload Audio File"),56    outputs=[57        gr.Image(type="filepath", label="Event Plot"),58    ],59    title="Sound Event Detection Inference",60    description="Upload an audio file to perform sound event detection.",61)62 63if __name__ == "__main__":64    iface.launch(server_name="0.0.0.0", server_port=7860)65