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