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()