import twitchio
import openai
import torch
import sounddevice as sd
import time
# Set up OpenAI API key and engine
openai.api_key = 'sk-токенD3YioE'
openai_engine = "text-davinci-003"
# Set up TTS parameters
language = 'ru'
model_id = 'ru_v3'
sample_rate = 48000 # 48000
speaker = 'baya' # aidar, baya, kseniya, xenia, random
put_accent = True
put_yo = True
device = torch.device('cpu') # cpu или gpu
# Set up Twitch Bot
bot_nickname = 'idбот_bot'
twitch_oauth_token = 'u7токенkag'
twitch_channel_name = 'idканалoke'
class TwitchBot(twitchio.Client):
def __init__(self):
super().__init__(token=twitch_oauth_token, initial_channels=[twitch_channel_name])
async def event_message(self, message):
# Ignore messages from the bot itself
if message.author.name.lower() == bot_nickname.lower():
return
# Handle new message from users
prompt = f"{message.author.name} написал: {message.content}"
response = openai.Completion.create(
engine=openai_engine,
prompt=prompt,
max_tokens=300,
n=1,
stop=None,
temperature=0.7
)
text_response = response.choices[0].text.strip()
audio_response = torch.hub.load(repo_or_dir='snakers4/silero-models',
model='silero_tts',
language=language,
speaker=model_id).to(device).synthesize(text_response)
print(f"{message.author.name} написал: {message.content} - {text_response}")
sd.play(audio_response, sample_rate)
time.sleep(len(audio_response) / sample_rate)
sd.stop()
async def event_subscribe(self, event):
# Say thank you to new subscribers and followers
if event.sub_plan == 'Prime' or event.sub_plan == '1000':
prompt = f"Thank you for the paid subscription {event.user.name}"
response = openai.Completion.create(
engine=openai_engine,
prompt=prompt,
max_tokens=100,
n=1,
stop=None,
temperature=0.7
)
text_response = response.choices[0].text.strip()
audio_response = torch.hub.load(repo_or_dir='snakers4/silero-models',
model='silero_tts',
language=language,
speaker=model_id).to(device).synthesize(text_response)
print(f"Thank you for the paid subscription {event.user.name}")
sd.play(audio_response, sample_rate)
time.sleep(len(audio_response) / sample_rate)
sd.stop()
else:
prompt = f"Thank you for the follows {event.user.name}"
response = openai.Completion.create(
engine=openai_engine,
prompt=prompt,
max_tokens=100,
n=1,
stop=None,
temperature=0.7
)
text_response = response.choices[0].text.strip()
audio_response = torch.hub.load(repo_or_dir='snakers4/silero-models',
model='silero_tts',
language=language,
speaker=model_id).to(device).synthesize(text_response)
print(f"Thank you for the follows {event.user.name}")
sd.play(audio_response, sample_rate)
time.sleep(len(audio_response) / sample_rate)
sd.stop()
bot = TwitchBot()
bot.run()