Spaces:
Paused
Paused
Commit ·
33085cc
1
Parent(s): 5856dbb
minor fixes
Browse filesFiles changed (2)
- app.py+19-7
- separate.py+27-21
106 unmodified lines1071081091101111121131141151161173 unmodified lines12112212312412512612726 unmodified lines15415515615715815912 unmodified lines17217317417517617717817918078 unmodified lines259260261262263264106 unmodified lineslogging.info(f"model_name: {model_name}")logging.info(f"in_filename: {in_filename}")waveform = load_audio(in_filename)waveform = np.transpose(waveform)waveform = np.ascontiguousarray(waveform)duration = waveform.shape[1] / 44100 # in secondssp = load_model(model_name)3 unmodified linesstart = time.time()output = sp.process(sample_rate=44100, samples=waveform)date_time = now.strftime("%Y-%m-%d %H:%M:%S.%f")end = time.time()26 unmodified linestitle = "# Source separation with Next-gen Kaldi"# css style is copied from# https://huggingface.co/spaces/alphacep/asr/blob/main/app.py#L11312 unmodified linesgr.Markdown(title)model_dropdown = gr.Dropdown(choices=model_list[model_list[0]],label="Select a model",value=model_list[model_list[0]],)with gr.Tabs():78 unmodified linesinputs=[model_dropdown, url_textbox],outputs=[url_vocals, url_non_vocals, url_html_info],)if __name__ == "__main__":formatter = "%(asctime)s %(levelname)s [%(filename)s:%(lineno)d] %(message)s"106 unmodified lines1071081091101111121131141151161173 unmodified lines12112212312412512612726 unmodified lines15415515615715815916016116216316416516616716816917012 unmodified lines18318418518618718818919019178 unmodified lines270271272273274275276106 unmodified lineslogging.info(f"model_name: {model_name}")logging.info(f"in_filename: {in_filename}")samples, sample_rate = load_audio(in_filename)samples = np.transpose(samples)samples = np.ascontiguousarray(samples)duration = samples.shape[1] / sample_rate # in secondssp = load_model(model_name)3 unmodified linesstart = time.time()output = sp.process(sample_rate=sample_rate, samples=samples)date_time = now.strftime("%Y-%m-%d %H:%M:%S.%f")end = time.time()26 unmodified linestitle = "# Source separation with Next-gen Kaldi"description = """This space shows how to do source separation with Next-gen Kaldi.It is running on CPU within a docker container provided by Hugging Face.See more information by visiting the following links:- <https://github.com/k2-fsa/sherpa-onnx>Everything is open-sourced."""# css style is copied from# https://huggingface.co/spaces/alphacep/asr/blob/main/app.py#L11312 unmodified linesgr.Markdown(title)model_dropdown = gr.Dropdown(choices=model_list[0],label="Select a model",value=model_list[0],)with gr.Tabs():78 unmodified linesinputs=[model_dropdown, url_textbox],outputs=[url_vocals, url_non_vocals, url_html_info],)gr.Markdown(description)if __name__ == "__main__":formatter = "%(asctime)s %(levelname)s [%(filename)s:%(lineno)d] %(message)s"
123456789101112131415161718192021222324252627282930313233343536373839#!/usr/bin/env python3# Copyright 2023 Xiaomi Corp. (authors: Fangjun Kuang)from functools import lru_cacheimport ffmpegimport numpy as npfrom huggingface_hub import hf_hub_downloadimport sherpa_onnxsample_rate = 44100def load_audio(filename):probe = ffmpeg.probe(filename)if "streams" not in probe or len(probe["streams"]) == 0:raise ValueError("No stream was found with ffprobe")metadata = next(stream for stream in probe["streams"] if stream["codec_type"] == "audio")n_channels = metadata["channels"]process = (ffmpeg.input(filename).output("pipe:", format="f32le", ar=sample_rate).run_async(pipe_stdout=True, pipe_stderr=True))buffer, _ = process.communicate()waveform = np.frombuffer(buffer, dtype="<f4").reshape(-1, n_channels)if n_channels > 2:waveform = waveform[:, :2]return waveform@lru_cache(maxsize=10)123456789101112131415161718192021222324252627282930313233343536373839404142434445#!/usr/bin/env python3# Copyright 2023 Xiaomi Corp. (authors: Fangjun Kuang)import loggingimport osfrom functools import lru_cacheimport numpy as npimport sherpa_onnximport soundfile as sffrom huggingface_hub import hf_hub_downloadimport uuiddef convert_to_wav(in_filename: str) -> str:"""Convert the input audio file to a wave file"""out_filename = str(uuid.uuid4())out_filename = f"{in_filename}.wav"logging.info(f"Converting '{in_filename}' to '{out_filename}'")_ = os.system(f"ffmpeg -hide_banner -loglevel error -i '{in_filename}' -ar 441000 -ac 2 '{out_filename}' -y")return out_filenamedef load_audio(filename):filename = convert_to_wav(filename)samples, sample_rate = sf.read(filename, dtype="float32", always_2d=True)samples = np.transpose(samples)# now samples is of shape (num_channels, num_samples)assert (samples.shape[1] > samples.shape[0]), f"You should use (num_channels, num_samples). {samples.shape}"assert (samples.dtype == np.float32), f"Expect np.float32 as dtype. Given: {samples.dtype}"return samples, sample_rate@lru_cache(maxsize=10)
app.py+19-7
separate.py+27-21