jacksonstrut commited on
Commit
73d0c31
·
verified ·
1 Parent(s): 5ab488c

Update app.py

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