Spaces:
Runtime error
Runtime error
Hendrik Schroeter commited on
Commit ·
b64ea7b
1
Parent(s): 9b336fb
Update to df3, improve cleanup
Browse files2 unmodified lines34567899 unmodified lines1920212223242574 unmodified lines10010110210310410510640 unmodified lines14714814915015115215315415515615715815916016116294 unmodified lines25725825926026126257 unmodified lines3203213223233242 unmodified linesimport osimport tempfileimport timefrom typing import Optional, Tuple, Unionimport gradio as grimport matplotlib.pyplot as plt9 unmodified linesfrom df.io import resampledevice = torch.device("cuda" if torch.cuda.is_available() else "cpu")model, df, _ = init_df("./DeepFilterNet2", config_allow_defaults=True)model = model.to(device=device).eval()fig_noisy: plt.Figure74 unmodified linesreturn audio, metadef demo_fn(speech_upl: str, noise_type: str, snr: int, mic_input: str):if mic_input:speech_upl = mic_inputsr = config("sr", 48000, int, section="df")40 unmodified linesax_enh.clear()noisy_im = spec_im(sample, sr=sr, figure=fig_noisy, ax=ax_noisy)enh_im = spec_im(enhanced, sr=sr, figure=fig_enh, ax=ax_enh)# Cleanup some old wav filesif os.path.exists("/tmp"):days = 1for f in glob.glob("/tmp/*wav"):is_old = (time.time() - os.path.getmtime(f)) / 3600 > 24 * daysif is_old and f not in (enhanced_wav, noisy_wav):try:os.remove(f)except Exception as e:print(f"failed to remove file {f}: {e}")return noisy_wav, noisy_im, enhanced_wav, enh_im94 unmodified linesreturn Image.frombytes("RGB", figure.canvas.get_width_height(), figure.canvas.tostring_rgb())def toggle(choice):if choice == "mic":return gr.update(visible=True, value=None), gr.update(visible=False, value=None)57 unmodified lines),gr.Markdown(open("usage.md").read())demo.launch(enable_queue=True)2 unmodified lines34567899 unmodified lines1920212223242574 unmodified lines10010110210310410510640 unmodified lines14714814915015115215315415515694 unmodified lines25125225325425525625725825926026126226326426526626726826957 unmodified lines3273283293303312 unmodified linesimport osimport tempfileimport timefrom typing import List, Optional, Tuple, Unionimport gradio as grimport matplotlib.pyplot as plt9 unmodified linesfrom df.io import resampledevice = torch.device("cuda" if torch.cuda.is_available() else "cpu")model, df, _ = init_df(config_allow_defaults=True)model = model.to(device=device).eval()fig_noisy: plt.Figure74 unmodified linesreturn audio, metadef demo_fn(speech_upl: str, noise_type: str, snr: int, mic_input: Optional[str] = None):if mic_input:speech_upl = mic_inputsr = config("sr", 48000, int, section="df")40 unmodified linesax_enh.clear()noisy_im = spec_im(sample, sr=sr, figure=fig_noisy, ax=ax_noisy)enh_im = spec_im(enhanced, sr=sr, figure=fig_enh, ax=ax_enh)filter = [speech_upl, noisy_wav, enhanced_wav]if mic_input is not None and mic_input != "":filter.append(mic_input)cleanup_tmp(filter)return noisy_wav, noisy_im, enhanced_wav, enh_im94 unmodified linesreturn Image.frombytes("RGB", figure.canvas.get_width_height(), figure.canvas.tostring_rgb())def cleanup_tmp(filter: List[str] = [], hours_keep=2):# Cleanup some old wav filesif os.path.exists("/tmp"):for f in glob.glob("/tmp/*"):is_old = (time.time() - os.path.getmtime(f)) / 3600 > hours_keepif is_old and f not in filter:try:os.remove(f)logger.info(f"Removed file {f}")except Exception as e:logger.warning(f"failed to remove file {f}: {e}")def toggle(choice):if choice == "mic":return gr.update(visible=True, value=None), gr.update(visible=False, value=None)57 unmodified lines),gr.Markdown(open("usage.md").read())cleanup_tmp()demo.launch(enable_queue=True)
app.py+21-14