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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import random
|
| 3 |
+
import logging
|
| 4 |
+
from twitchio.ext import commands
|
| 5 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 6 |
+
import torch
|
| 7 |
+
|
| 8 |
+
# Set up logging
|
| 9 |
+
logging.basicConfig(level=logging.INFO)
|
| 10 |
+
logger = logging.getLogger(__name__)
|
| 11 |
+
|
| 12 |
+
# Credentials and settings
|
| 13 |
+
TWITCH_OAUTH_TOKEN = os.getenv('TWITCH_OAUTH_TOKEN')
|
| 14 |
+
TWITCH_CHANNEL_NAME = os.getenv('TWITCH_CHANNEL_NAME')
|
| 15 |
+
TWITCH_BOT_USERNAME = os.getenv('TWITCH_BOT_USERNAME')
|
| 16 |
+
HUGGINGFACE_API_TOKEN = os.getenv('HUGGINGFACE_API_TOKEN')
|
| 17 |
+
MAX_TOKENS = int(os.getenv('MAX_TOKENS', 100))
|
| 18 |
+
TEMPERATURE = float(os.getenv('TEMPERATURE', 0.7))
|
| 19 |
+
|
| 20 |
+
# Validate environment variables
|
| 21 |
+
required_vars = [
|
| 22 |
+
'TWITCH_OAUTH_TOKEN',
|
| 23 |
+
'TWITCH_CHANNEL_NAME',
|
| 24 |
+
'TWITCH_BOT_USERNAME',
|
| 25 |
+
'HUGGINGFACE_API_TOKEN'
|
| 26 |
+
]
|
| 27 |
+
|
| 28 |
+
missing_vars = [var for var in required_vars if not globals().get(var)]
|
| 29 |
+
if missing_vars:
|
| 30 |
+
raise ValueError(f"Missing environment variables: {', '.join(missing_vars)}")
|
| 31 |
+
|
| 32 |
+
# Initialize the Hugging Face tokenizer and model for DialoGPT
|
| 33 |
+
model_name = "microsoft/DialoGPT-small"
|
| 34 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, token=HUGGINGFACE_API_TOKEN)
|
| 35 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, token=HUGGINGFACE_API_TOKEN)
|
| 36 |
+
model.to('cpu')
|
| 37 |
+
|
| 38 |
+
# List of house music hooks to drop randomly
|
| 39 |
+
HOUSE_MUSIC_HOOKS = [
|
| 40 |
+
"Feeling the vibe, let's get hyped!",
|
| 41 |
+
"Rave or die!",
|
| 42 |
+
"Rave to the g,rave!",
|
| 43 |
+
"He just strutts!",
|
| 44 |
+
"Dancing is what to do!",
|
| 45 |
+
"Let's get riggity riggity wrecked!",
|
| 46 |
+
"Sleep is for the week, not the weekend!",
|
| 47 |
+
"It's hard out here for a pimp!",
|
| 48 |
+
"Something for your mind!",
|
| 49 |
+
"Something for your body!",
|
| 50 |
+
"Something for your soul!",
|
| 51 |
+
"Not everyone understands this thing they call house music!",
|
| 52 |
+
"It's a spiritual thing!",
|
| 53 |
+
"Talkin' about that thing they call house music!",
|
| 54 |
+
"Don't mean a thing if it ain't got that swing!",
|
| 55 |
+
"Can't stop. Won't stop!",
|
| 56 |
+
"Shots! Shots! Shots!",
|
| 57 |
+
"Jack to the sound of the underground!",
|
| 58 |
+
"Jack your body!",
|
| 59 |
+
"Asking ya..to BELIEVE..in the MIX!",
|
| 60 |
+
"BeaverBankHouseMafia in the house baby!",
|
| 61 |
+
"Dancing all night long, can't stop the beat!",
|
| 62 |
+
"Got my hands up high, feeling alive!",
|
| 63 |
+
"The music's got me moving, can't resist the groove!",
|
| 64 |
+
"Let's ride this wave, we're on top of the world!",
|
| 65 |
+
"Feeling the energy, let it consume us all!",
|
| 66 |
+
"We're in the zone, lost in the music!",
|
| 67 |
+
"This beat is fire, can't stop dancing!",
|
| 68 |
+
"The rhythm is pumping, let's keep it real!",
|
| 69 |
+
"Get ready to party, we're on a roll!",
|
| 70 |
+
"Let's make some noise, and show them how it's done!",
|
| 71 |
+
"We're living for the moment, feeling alive tonight!",
|
| 72 |
+
"The music's got us hyped, let's keep it real!",
|
| 73 |
+
"We're in the house, and we're here to stay!",
|
| 74 |
+
"Let's rock this place, and make it feel like home!",
|
| 75 |
+
"Got my hands up high, feeling alive tonight!",
|
| 76 |
+
"The beat is strong, and so are we!",
|
| 77 |
+
"We're on the dance floor, living our best lives!",
|
| 78 |
+
"Let's keep the energy high, and the vibes bright!",
|
| 79 |
+
"Feeling the music deep inside my soul!",
|
| 80 |
+
"We're on a roll, can't stop feeling alive!",
|
| 81 |
+
"The music's got us moving, let's keep on dancing!",
|
| 82 |
+
"Feeling the magic, in every single note!",
|
| 83 |
+
"We're lost in the rhythm, and we can't be stopped!",
|
| 84 |
+
"The music's got us swaying, back and forth!",
|
| 85 |
+
"Let's keep dancing all night long!",
|
| 86 |
+
"Feeling the heat, let it consume me whole!",
|
| 87 |
+
"We're in the zone, can't stop grooving!",
|
| 88 |
+
"The music's got us high, and we won't come down!",
|
| 89 |
+
"Let's keep on moving, until the morning light!",
|
| 90 |
+
"We're in this together, let's make it feel right!",
|
| 91 |
+
"The rhythm is strong, and we can't be stopped!",
|
| 92 |
+
"Feeling the beat, deep inside my soul!",
|
| 93 |
+
"Got my heart racing, and my feet are on fire!",
|
| 94 |
+
"The music's got us moving, let's keep it real!",
|
| 95 |
+
"Feeling the energy, and we won't stop until the break of dawn!",
|
| 96 |
+
"Got my hands up high, feeling alive tonight!",
|
| 97 |
+
"The beat is strong, and so are we!",
|
| 98 |
+
"Got my feet tapping, and my heart is pounding!",
|
| 99 |
+
"Feeling the magic, in every single note!",
|
| 100 |
+
"Let's keep on dancing all night long, feeling alive tonight!",
|
| 101 |
+
"We're lost in the rhythm, and can't be stopped!",
|
| 102 |
+
"Feeling the energy, let it consume me whole!",
|
| 103 |
+
"Got my heart racing, and my feet are on fire!",
|
| 104 |
+
"The music's got us moving, can't stop dancing!",
|
| 105 |
+
"Feeling the beat, deep inside my soul!",
|
| 106 |
+
"Let's keep on dancing, until we reach our goal!",
|
| 107 |
+
"We're on a roll, can't stop living our best lives!",
|
| 108 |
+
"The music's got us moving, can't stop swaying!",
|
| 109 |
+
"Feeling the energy, let it consume me whole!",
|
| 110 |
+
"We're in this together, let's make it feel right!",
|
| 111 |
+
"Got my heart racing, and my feet are on fire!",
|
| 112 |
+
"Feeling the magic, in every single note!",
|
| 113 |
+
"We're lost in the rhythm, and can't be stopped!",
|
| 114 |
+
"The music's got us swaying, back and forth!",
|
| 115 |
+
"Feeling the heat, let it consume me whole!",
|
| 116 |
+
"The beat is strong, and so are we!",
|
| 117 |
+
"Got my feet tapping, and my heart is pounding!",
|
| 118 |
+
"Let's keep dancing all night long, feeling alive tonight!",
|
| 119 |
+
"Feeling the energy, let it take control!",
|
| 120 |
+
"We're lost in the music, can't be found!",
|
| 121 |
+
"Got my hands up high, feeling alive tonight!",
|
| 122 |
+
"Feeling the vibe, let's keep it real!",
|
| 123 |
+
"We're on a roll, can't stop living our best lives!",
|
| 124 |
+
"Got my heart racing, and my feet are on fire!",
|
| 125 |
+
"The music's got us moving, can't stop grooving!",
|
| 126 |
+
"Feeling the magic, in every single note!",
|
| 127 |
+
"We're lost in the rhythm, and can't be stopped!",
|
| 128 |
+
"Got my feet tapping, and my heart is pounding!",
|
| 129 |
+
"Feeling the energy, let it take control!",
|
| 130 |
+
"We're on a roll, can't stop living our best lives!",
|
| 131 |
+
"The beat is strong, and so are we!",
|
| 132 |
+
"Feeling the energy, let it consume me whole!",
|
| 133 |
+
"Got my feet tapping, and my heart is pounding!",
|
| 134 |
+
"Feeling the beat, deep inside my soul!",
|
| 135 |
+
"Got my heart racing, and my feet are on fire!",
|
| 136 |
+
"The music's got us moving, can't stop dancing!",
|
| 137 |
+
]
|
| 138 |
+
|
| 139 |
+
# Initialize chat history for users
|
| 140 |
+
chat_histories = {}
|
| 141 |
+
|
| 142 |
+
async def generate_response(user_id, user_message):
|
| 143 |
+
"""Generates a response using the DialoGPT model."""
|
| 144 |
+
try:
|
| 145 |
+
# Retrieve or initialize the chat history for the user
|
| 146 |
+
if user_id in chat_histories:
|
| 147 |
+
chat_history_ids = chat_histories[user_id]
|
| 148 |
+
else:
|
| 149 |
+
chat_history_ids = None
|
| 150 |
+
|
| 151 |
+
# Encode the user message and append the EOS token
|
| 152 |
+
new_user_input_ids = tokenizer.encode(user_message + tokenizer.eos_token, return_tensors='pt').to('cpu')
|
| 153 |
+
|
| 154 |
+
# Concatenate new user input with chat history (if it exists)
|
| 155 |
+
if chat_history_ids is not None:
|
| 156 |
+
bot_input_ids = torch.cat([chat_history_ids, new_user_input_ids], dim=-1)
|
| 157 |
+
else:
|
| 158 |
+
bot_input_ids = new_user_input_ids
|
| 159 |
+
|
| 160 |
+
# Generate a response
|
| 161 |
+
output_ids = model.generate(
|
| 162 |
+
bot_input_ids,
|
| 163 |
+
max_length=bot_input_ids.shape[-1] + MAX_TOKENS,
|
| 164 |
+
temperature=TEMPERATURE,
|
| 165 |
+
do_sample=True,
|
| 166 |
+
top_p=0.95,
|
| 167 |
+
top_k=50,
|
| 168 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 169 |
+
no_repeat_ngram_size=3,
|
| 170 |
+
)
|
| 171 |
+
|
| 172 |
+
# Extract the new response
|
| 173 |
+
response_ids = output_ids[:, bot_input_ids.shape[-1]:]
|
| 174 |
+
response_text = tokenizer.decode(response_ids[0], skip_special_tokens=True)
|
| 175 |
+
|
| 176 |
+
# Update the chat history
|
| 177 |
+
chat_histories[user_id] = output_ids[:, -1000:] # Keep last 1000 tokens to limit history size
|
| 178 |
+
|
| 179 |
+
# Randomly include a house music hook (30% chance)
|
| 180 |
+
if random.random() < 0.3:
|
| 181 |
+
response_text = f"{random.choice(HOUSE_MUSIC_HOOKS)} {response_text}"
|
| 182 |
+
|
| 183 |
+
logger.info(f"Generated response: {response_text}")
|
| 184 |
+
return response_text
|
| 185 |
+
|
| 186 |
+
except Exception as e:
|
| 187 |
+
logger.error(f"Error generating response: {e}")
|
| 188 |
+
return "Sorry, I'm too hyped to respond right now!"
|
| 189 |
+
|
| 190 |
+
# Create a Twitch chatbot using TwitchIO
|
| 191 |
+
class TwitchChatBot(commands.Bot):
|
| 192 |
+
|
| 193 |
+
def __init__(self):
|
| 194 |
+
super().__init__(
|
| 195 |
+
token=TWITCH_OAUTH_TOKEN,
|
| 196 |
+
nick=TWITCH_BOT_USERNAME,
|
| 197 |
+
prefix='!',
|
| 198 |
+
initial_channels=[TWITCH_CHANNEL_NAME]
|
| 199 |
+
)
|
| 200 |
+
|
| 201 |
+
async def event_ready(self):
|
| 202 |
+
"""Event handler when the bot is connected and ready."""
|
| 203 |
+
logger.info(f"Logged in as | {self.nick}")
|
| 204 |
+
logger.info(f"Connected to channel | {TWITCH_CHANNEL_NAME}")
|
| 205 |
+
|
| 206 |
+
async def event_message(self, message):
|
| 207 |
+
"""Event handler when a message is received in chat."""
|
| 208 |
+
# Ignore messages sent by the bot itself
|
| 209 |
+
if message.echo:
|
| 210 |
+
return
|
| 211 |
+
|
| 212 |
+
logger.info(f"Message received from {message.author.name}: {message.content}")
|
| 213 |
+
|
| 214 |
+
# Generate a response
|
| 215 |
+
response = await generate_response(message.author.id, message.content)
|
| 216 |
+
|
| 217 |
+
# Send the response back to the Twitch chat
|
| 218 |
+
await message.channel.send(f"@{message.author.name} {response}")
|
| 219 |
+
|
| 220 |
+
# Initialize and run the bot
|
| 221 |
+
if __name__ == "__main__":
|
| 222 |
+
bot = TwitchChatBot()
|
| 223 |
+
bot.run()
|