代码,大家可以照着这个自己玩。因为写的比较快,
🔒糙极了(又不是不能用)
https://github.com/yihong0618/tg_bot_collections
from telebot import TeleBot
from telebot.types import Message
from . import *
import wave
import numpy as np
from handlers.ChatTTS import Chat
chat = Chat()
chat.load_models()
is_generating = False
def generate_tts_wav(prompt):
global is_generating
texts = [prompt,]
is_generating = True
wavs = chat.infer(texts, use_decoder=True)
output_filename = 'tts.wav'
audio_data = np.array(wavs[0], dtype=np.float32)
sample_rate = 24000
audio_data = (audio_data * 32767).astype(np.int16)
with wave.open(output_filename, 'w') as wf:
wf.setnchannels(1)
wf.setsampwidth(2)
wf.setframerate(sample_rate)
wf.writeframes(audio_data.tobytes())
print(f"Audio has been saved to {output_filename}")
is_generating = False
def tts_handler(message: Message, bot: TeleBot):
"""pretty tts: /tts <address>"""
global is_generating
if is_generating:
bot.reply_to(message, "please wait for the previous ChatTTS to finish")
return
bot.reply_to(
message,
f"Generating ChatTTS may take some time please left"
)
m = message.text.strip()
prompt = m.strip()
if len(prompt) > 100:
bot.reply_to(message, "prompt too long must length < 100")
return
try:
generate_tts_wav(prompt)
with open(f"tts.wav", "rb") as audio:
bot.send_audio(
message.chat.id, audio, reply_to_message_id=message.message_id
)
except Exception as e:
print(e)
bot.reply_to(message, "tts error")
is_generating = False
def register(bot: TeleBot) -> None:
bot.register_message_handler(tts_handler, commands=["tts"], pass_bot=True)
bot.register_message_handler(tts_handler, regexp="^tts:", pass_bot=True)
GitHub
GitHub - yihong0618/tg_bot_collections: collections of yihong0618's telegram bot
collections of yihong0618's telegram bot. Contribute to yihong0618/tg_bot_collections development by creating an account on GitHub.