Download bots/test.py from jam134/adad: direct link, hf CLI and curl.
- Browser
- Download file 3.27 kB
-
https://huggingface.co/spaces/jam134/adad/resolve/main/bots/test.py
- Command line
-
hf download hf://spaces/jam134/adad/bots/test.py
-
curl -L -o test.py https://huggingface.co/spaces/jam134/adad/resolve/main/bots/test.py
3.27 kB
| import discord | |
| import sys | |
| import soundfile as sf | |
| import numpy as np | |
| from tools.i18n.i18n import I18nAuto | |
| from GPT_SoVITS.test_webui import change_gpt_weights, change_sovits_weights, get_tts_wav | |
| import os | |
| from discord.ext import commands | |
| import asyncio | |
| # 創建 intents 實例 | |
| intents = discord.Intents.default() | |
| intents.messages = True | |
| # 初始化 Bot,並傳入 intents | |
| client = commands.Bot(command_prefix='!', intents=intents) | |
| i18n = I18nAuto() | |
| def remove_silence(audio_data, threshold=0.01): | |
| audio_data = np.array(audio_data) | |
| non_silent_indices = np.where(np.abs(audio_data) > threshold)[0] | |
| if non_silent_indices.size > 0: | |
| start_index = non_silent_indices[0] | |
| end_index = non_silent_indices[-1] + 1 | |
| return audio_data[start_index:end_index] | |
| return audio_data | |
| async def synthesize_audio(gpt_model_path, sovits_model_path, ref_audio_path, ref_text, target_text, output_path, language_combobox, language_combobox_02): | |
| await asyncio.to_thread(change_gpt_weights, gpt_path=gpt_model_path) | |
| await asyncio.to_thread(change_sovits_weights, sovits_path=sovits_model_path) | |
| synthesis_result = await asyncio.to_thread(get_tts_wav, ref_wav_path=ref_audio_path, | |
| prompt_text=ref_text, | |
| prompt_language=language_combobox, | |
| text=target_text, | |
| text_language=language_combobox_02) | |
| result_list = list(synthesis_result) | |
| if result_list: | |
| last_sampling_rate, last_audio_data = result_list[-1] | |
| last_audio_data = remove_silence(last_audio_data) | |
| if not os.path.exists(output_path): | |
| os.makedirs(output_path) | |
| output_wav_path = os.path.join(output_path, "output.wav") | |
| sf.write(output_wav_path, last_audio_data, last_sampling_rate) | |
| return output_wav_path | |
| return None | |
| async def synthesize(ctx, *, target_text: str): | |
| ref_text = "幽魂盯上你了,進入射擊位置" | |
| output_path = 'GPT_SoVITS/output' | |
| gpt_model_path = 'GPT_SoVITS/pretrained_models/s1bert25hz-2kh-longer-epoch=68e-step=50232.ckpt' | |
| sovits_model_path = 'GPT_SoVITS/pretrained_models/s2G488k.pth' | |
| ref_audio_path = 'GPT_SoVITS/pretrained_models/Ana.wav' | |
| language_combobox = i18n("中文") | |
| language_combobox_02 = i18n("多语种混合") | |
| output_wav_path = await synthesize_audio(gpt_model_path, sovits_model_path, ref_audio_path, ref_text, target_text, output_path, language_combobox, language_combobox_02) | |
| if output_wav_path: | |
| if ctx.author.voice: | |
| channel = ctx.author.voice.channel | |
| voice_client = await channel.connect() | |
| voice_client.play(discord.FFmpegPCMAudio(output_wav_path), after=lambda e: print('播放完成', e)) | |
| await ctx.send("正在播放生成的音頻...") | |
| # 等待音頻播放完成後斷開連接 | |
| while voice_client.is_playing(): | |
| await asyncio.sleep(1) | |
| await voice_client.disconnect() | |
| else: | |
| await ctx.send("請先進入語音頻道!") | |
| else: | |
| await ctx.send("合成失敗!") | |