Spaces:
Runtime error
Runtime error
Update app.py
Browse files1 unmodified line2345678919 unmodified lines29303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282291 unmodified lineimport randomimport loggingfrom twitchio.ext import commandsfrom transformers import AutoTokenizer, AutoModelForCausalLMimport torch# Set up logginglogging.basicConfig(level=logging.INFO)19 unmodified linesif missing_vars:raise ValueError(f"Missing environment variables: {', '.join(missing_vars)}")model_name = "jacksonstrut/tinyllama-1.1B-chat"tokenizer = AutoTokenizer.from_pretrained(model_name, use_auth_token=HUGGINGFACE_API_TOKEN)model = AutoModelForCausalLM.from_pretrained(model_name, use_auth_token=HUGGINGFACE_API_TOKEN)model.to('cpu')# Initialize the Hugging Face tokenizer and model for DialoGPTmodel_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 randomlyHOUSE_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 userschat_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 userif 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 tokennew_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 responseoutput_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 responseresponse_ids = output_ids[:, bot_input_ids.shape[-1]:]response_text = tokenizer.decode(response_ids[0], skip_special_tokens=True)# Update the chat historychat_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_textexcept 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 TwitchIOclass 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 itselfif message.echo:returnlogger.info(f"Message received from {message.author.name}: {message.content}")# Generate a responseresponse = await generate_response(message.author.id, message.content)# Send the response back to the Twitch chatawait message.channel.send(f"@{message.author.name} {response}")# Initialize and run the botif __name__ == "__main__":bot = TwitchChatBot()bot.run()1 unmodified line234567891019 unmodified lines30313233343536373839404142434445461 unmodified lineimport randomimport loggingfrom twitchio.ext import commandsfrom transformers import AutoTokenizer, AutoConfig, AutoModelForCausalLM, PreTrainedModel, PretrainedConfigimport torchimport torch.nn as nn# Set up logginglogging.basicConfig(level=logging.INFO)19 unmodified linesif missing_vars:raise ValueError(f"Missing environment variables: {', '.join(missing_vars)}")# Initialize the Hugging Face tokenizer and modelmodel_name = "your-username/your-model-name" # Replace with your actual model name# Replace use_auth_token with tokentokenizer = AutoTokenizer.from_pretrained(model_name, token=HUGGINGFACE_API_TOKEN)config = AutoConfig.from_pretrained(model_name)model = AutoModelForCausalLM.from_pretrained(model_name, config=config, token=HUGGINGFACE_API_TOKEN)model.to('cpu')# Ensure pad_token is setif tokenizer.pad_token is None:tokenizer.pad_token = tokenizer.eos_token# ... rest of your script ...
app.py+11-194