Spaces:
Runtime error
Runtime error
ggoknar commited on
Commit ·
0db6209
1
Parent(s): 33aef52
use ffmpeg to denoise microphone input
Browse filesFiles changed (3)
- app.py+44-5
- ffmpeg.zipAdded+3
- packages.txtAdded+1
1234567891011121314151617181920212212 unmodified lines353637383940414243444546477 unmodified lines555657585960613 unmodified lines656667686970252 unmodified lines323324325326import sysimport os# By using XTTS you agree to CPML license https://coqui.ai/cpmlos.environ["COQUI_TOS_AGREED"] = "1"import langidimport gradio as grfrom TTS.api import TTSHF_TOKEN = os.environ.get("HF_TOKEN")from huggingface_hub import HfApi# will use api to restart space on a unrecoverable errorapi = HfApi(token=HF_TOKEN)repo_id = "coqui/xtts"tts = TTS("tts_models/multilingual/multi-dataset/xtts_v1")tts.to("cuda")DEVICE_ASSERT_DETECTED=0DEVICE_ASSERT_PROMPT=NoneDEVICE_ASSERT_LANG=None12 unmodified lineslanguage_predicted=langid.classify(prompt)[0].strip() # strip need as there is space at end!if language_predicted == "zh":#we use zh-cnlanguage_predicted = "zh-cn"print(f"Detected language:{language_predicted}, Chosen language:{language}")if len(prompt)>15:#allow any language for short text as some may be commonif language_predicted != language and not no_lang_auto_detect:#Please duplicate and remove this check if you really want this#Or auto-detector fails to identify language (which it can on pretty short text or mixed text)7 unmodified linesif use_mic == True:if mic_file_path is not None:speaker_wav=mic_file_pathelse:gr.Warning("Please record your voice with Microphone, or uncheck Use Microphone to use reference audios")return (3 unmodified lineselse:speaker_wav=audio_file_pthif len(prompt)<2:gr.Warning("Please give a longer prompt text")252 unmodified linesdescription=description,article=article,examples=examples,).queue().launch(debug=True)12345678910111213141516171819202122232425262728293031323334353612 unmodified lines49505152535455565758596061626364657 unmodified lines73747576777879808182838485868788899091929394959697983 unmodified lines102103104105106107108252 unmodified lines361362363364365import sysimport os,statimport subprocessfrom zipfile import ZipFile# By using XTTS you agree to CPML license https://coqui.ai/cpmlos.environ["COQUI_TOS_AGREED"] = "1"# langid is used to detect language for longer text# Most users expect text to be their own language, there is checkbox to disable itimport langidimport gradio as grfrom TTS.api import TTSHF_TOKEN = os.environ.get("HF_TOKEN")from huggingface_hub import HfApi# will use api to restart space on a unrecoverable errorapi = HfApi(token=HF_TOKEN)repo_id = "coqui/xtts"# Use never ffmpeg binary for Ubuntu20 to use denoising for microphone inputprint("Export newer ffmpeg binary for denoise filter")ZipFile("ffmpeg.zip").extractall()print("Make ffmpeg binary executable")st = os.stat('ffmpeg')os.chmod('ffmpeg', st.st_mode | stat.S_IEXEC)# Load TTStts = TTS("tts_models/multilingual/multi-dataset/xtts_v1")tts.to("cuda")# This is for debugging purposes onlyDEVICE_ASSERT_DETECTED=0DEVICE_ASSERT_PROMPT=NoneDEVICE_ASSERT_LANG=None12 unmodified lineslanguage_predicted=langid.classify(prompt)[0].strip() # strip need as there is space at end!# tts expects chinese as zh-cnif language_predicted == "zh":#we use zh-cnlanguage_predicted = "zh-cn"print(f"Detected language:{language_predicted}, Chosen language:{language}")# After text character length 15 trigger language detectionif len(prompt)>15:# allow any language for short text as some may be common# If user unchecks language autodetection it will not trigger# You may remove this completely for own useif language_predicted != language and not no_lang_auto_detect:#Please duplicate and remove this check if you really want this#Or auto-detector fails to identify language (which it can on pretty short text or mixed text)7 unmodified linesif use_mic == True:if mic_file_path is not None:try:# Filtering for microphone input, as it has BG noise, maybe silence in beginning and end# This is fast filtering not perfectlowpass_highpass="lowpass=1000,highpass=200" #too bassfast_denoise="afftdn=nr=12:nf=-25"# better to remove silence in beginning and end for microphonetrim_silence="areverse,atrim=start=0.2,silenceremove=start_periods=1:start_silence=0:start_threshold=0.02,areverse,atrim=start=0.2,silenceremove=start_periods=1:start_silence=0:start_threshold=0.02"out_filename = mic_file_path +".wav" #ffmpeg to know output format#we will use newer ffmpeg as that has afftn denoise filtershell_command = f"./ffmpeg -y -i {mic_file_path} -af {lowpass_highpass}{fast_denoise},{trim_silence},loudnorm {out_filename}".split(" ")command_result = subprocess.run([item for item in shell_command], capture_output=False,text=True, check=True)speaker_wav=out_filenameprint("Filtered microphone input")except subprocess.CalledProcessError:# There was an error - command exited with non-zero codeprint("Error: failed filtering, use original microphone input")speaker_wav=mic_file_pathelse:gr.Warning("Please record your voice with Microphone, or uncheck Use Microphone to use reference audios")return (3 unmodified lineselse:speaker_wav=audio_file_pthif len(prompt)<2:gr.Warning("Please give a longer prompt text")252 unmodified linesdescription=description,article=article,examples=examples,).queue().launch(debug=True)
123version https://git-lfs.github.com/spec/v1oid sha256:6c04aa2958762686cf94a3bd1456b4738fd537d19bb0a9b622fc788a5e4ce723size 292070561unzipapp.py+44-5
ffmpeg.zipAdded+3
packages.txtAdded+1