Spaces:
Running on Zero
Running on Zero
Update inference_gradio.py
Browse files109 unmodified lines11011111211311411511613 unmodified lines13013113213313413513613713813914099 unmodified lines24024124224324424524624726 unmodified lines2742752762772782794 unmodified lines28428528628728828929012 unmodified lines303304305306307308148 unmodified lines457458459460461462463109 unmodified linesdenoise_model = UVR5(model_dir=Path(PRETRAINED_ROOT) / "uvr5",code_dir=REPO_ROOT / "uvr5",)def load_wav(audio_info, sr=16000, channel=1):13 unmodified linesdef denoise(audio_info):save_path = "./denoised_audio.wav"denoised_audio, sr = denoise_model.denoise(audio_info)sf.write(save_path, denoised_audio, sr, format='wav', subtype='PCM_24')print("save denoised audio:", save_path)return save_pathdef cancel_denoise(audio_info):return audio_info99 unmodified linesif not os.path.isfile(ckpt_resolved):return None, "Checkpoint not found!", ""if denoise_audio:ref_audio = denoise_audio# Automatically enable prosody encoder when using the prosody checkpointuse_prosody_encoder = True if "prosody" in str(ckpt_resolved) else False26 unmodified lines)except Exception as e:traceback.print_exc()return None, f"Error loading model: {str(e)}", ""print("Model loaded >>", file_checkpoint, use_ema)4 unmodified linestry:with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as f:tts_api.infer(ref_file=ref_audio,ref_text=ref_text.strip(),gen_text=gen_text.strip(),nfe_step=nfe_step,12 unmodified linesexcept Exception as e:traceback.print_exc()return None, f"Inference error: {str(e)}", ""def get_gpu_stats():148 unmodified lineswith gr.Row():denoise_btn = gr.Button(value="Denoise")cancel_btn = gr.Button(value="Cancel Denoise")denoise_audio = gr.Audio(label="Denoised Audio", value=None, type="filepath", interactive=True, show_download_button=True, editable=True)gen_text = gr.Textbox(label="Text to Generate", placeholder="Enter the text you want to generate...")109 unmodified lines11011111211311411513 unmodified lines12913013113213313413513613799 unmodified lines23723823924024124224324424524624724824925025125225325425525625725826 unmodified lines2852862872882892902912922934 unmodified lines29829930030130230330412 unmodified lines317318319320321322323324325326148 unmodified lines475476477478479480481482483484485486487488489109 unmodified linesdenoise_model = UVR5(model_dir=Path(PRETRAINED_ROOT) / "uvr5",)def load_wav(audio_info, sr=16000, channel=1):13 unmodified linesdef denoise(audio_info):# Return a numpy waveform tuple for direct playback in Gradio.denoised_audio, sr = denoise_model.denoise(audio_info)return (sr, denoised_audio)def cancel_denoise(audio_info):return audio_info99 unmodified linesif not os.path.isfile(ckpt_resolved):return None, "Checkpoint not found!", ""# Prepare reference audio:# - `ref_audio` from Gradio is a filepath (original reference)# - `denoise_audio` is an optional (sr, numpy_array) tuple from UVR5.# If provided, dump it to a temporary wav file and use that as ref_file.ref_audio_path = ref_audiotmp_ref_path = Noneif denoise_audio is not None:try:sr_d, wav_d = denoise_audiowith tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as f_ref:sf.write(f_ref.name, wav_d, int(sr_d), format="wav", subtype="PCM_24")tmp_ref_path = f_ref.nameref_audio_path = f_ref.nameexcept Exception as e:traceback.print_exc()return None, f"Error preparing denoised reference audio: {str(e)}", ""# Automatically enable prosody encoder when using the prosody checkpointuse_prosody_encoder = True if "prosody" in str(ckpt_resolved) else False26 unmodified lines)except Exception as e:traceback.print_exc()# Cleanup temp ref file if it was createdif tmp_ref_path is not None and os.path.isfile(tmp_ref_path):os.remove(tmp_ref_path)return None, f"Error loading model: {str(e)}", ""print("Model loaded >>", file_checkpoint, use_ema)4 unmodified linestry:with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as f:tts_api.infer(ref_file=ref_audio_path,ref_text=ref_text.strip(),gen_text=gen_text.strip(),nfe_step=nfe_step,12 unmodified linesexcept Exception as e:traceback.print_exc()return None, f"Inference error: {str(e)}", ""finally:# Remove temporary reference file if createdif tmp_ref_path is not None and os.path.isfile(tmp_ref_path):os.remove(tmp_ref_path)def get_gpu_stats():148 unmodified lineswith gr.Row():denoise_btn = gr.Button(value="Denoise")cancel_btn = gr.Button(value="Cancel Denoise")# Use numpy type here so we can reuse the waveform directly in Python.denoise_audio = gr.Audio(label="Denoised Audio",value=None,type="numpy",interactive=True,show_download_button=True,editable=True,)gen_text = gr.Textbox(label="Text to Generate", placeholder="Enter the text you want to generate...")
inference_gradio.py+35-9