Update: WillieTimer RNN textgen function and better behavior with generating text
This commit is contained in:
BIN
modules/WillieTimer/WillieBotModel_weights.hdf5
Normal file
BIN
modules/WillieTimer/WillieBotModel_weights.hdf5
Normal file
Binary file not shown.
@@ -6,6 +6,8 @@ from discord.ext import commands
|
||||
from discord.ext.tasks import loop
|
||||
from BotResources import write_config_file
|
||||
|
||||
from phraseGenerator import PhraseGenerator
|
||||
|
||||
|
||||
class WillieTimer(commands.Cog):
|
||||
def __init__(self, bot): #, mention_group='Superadmins', channel_id=757379843792044102):
|
||||
@@ -14,17 +16,31 @@ class WillieTimer(commands.Cog):
|
||||
self.channel_id = int(self.bot.Default_Channel_ID)
|
||||
self.lock = False
|
||||
self.current_phrase = None
|
||||
self.caller_name = None
|
||||
|
||||
self.PG = PhraseGenerator()
|
||||
|
||||
@loop(minutes=1)
|
||||
async def bg_timer(self):
|
||||
# Get variables ready before waiting for the next minute, might take a few seconds
|
||||
await self.bot.wait_until_ready()
|
||||
channel = self.bot.get_channel(id=self.channel_id)
|
||||
output_string = self.get_output_string()
|
||||
|
||||
seconds_until_next_minute = int(60 - int(datetime.datetime.now().strftime('%S')))
|
||||
|
||||
if not seconds_until_next_minute <= 2:
|
||||
await asyncio.sleep(seconds_until_next_minute)
|
||||
if datetime.datetime.now().strftime('%H:%M') in ("04:20", "16:20"):
|
||||
|
||||
# If it is 4:20pm
|
||||
if datetime.datetime.now().strftime('%H:%M') in "16:20":
|
||||
print(f"It's {datetime.datetime.now().strftime('%H:%M:%S')}!")
|
||||
channel = self.bot.get_channel(id=self.channel_id)
|
||||
await channel.send(f"<@&{self.bot_get_role().id}> {self.get_random_phrase()}")
|
||||
await channel.send(output_string['420'])
|
||||
|
||||
# A warm up to 4:20pm
|
||||
if datetime.datetime.now().strftime('%H:%M') in "16:17":
|
||||
print(f"It's {datetime.datetime.now().strftime('%H:%M:%S')}!")
|
||||
await channel.send(output_string['warm up'])
|
||||
|
||||
@commands.command(help="Use this command to start the background task to wait for 4:20",
|
||||
brief="Starts the 4:20 clock")
|
||||
@@ -34,6 +50,7 @@ class WillieTimer(commands.Cog):
|
||||
await ctx.send(f"Thanks {member}, Willie will be in touch soon...")
|
||||
self.lock = True
|
||||
self.bg_timer.start()
|
||||
self.caller_name = member
|
||||
else:
|
||||
await ctx.send(f"I already told Willie {member}, if you want me to tell him to stop, @ me and say 'stop420'")
|
||||
|
||||
@@ -44,6 +61,7 @@ class WillieTimer(commands.Cog):
|
||||
await ctx.send(f"Ok {member}, Willie will go back to writing music...")
|
||||
self.bg_timer.stop()
|
||||
self.lock = False
|
||||
self.caller_name = None
|
||||
else:
|
||||
await ctx.send(f"{member} I haven't told Willie yet. If you want me to, @ me and say 'start420'")
|
||||
|
||||
@@ -83,8 +101,7 @@ class WillieTimer(commands.Cog):
|
||||
@commands.command(help='Test random choice')
|
||||
async def test_rchoice(self, ctx, *, member: discord.Member = None):
|
||||
member = member or ctx.author.display_name
|
||||
await ctx.send(f"Selection:\t{self.get_random_phrase()}", tts=True)
|
||||
|
||||
await ctx.send(f"Selection:\t{str(self.get_output_string())}", tts=True)
|
||||
|
||||
def bot_get_role(self):
|
||||
channel = self.bot.get_channel(id=self.channel_id)
|
||||
@@ -92,7 +109,7 @@ class WillieTimer(commands.Cog):
|
||||
role = discord.utils.get(guild.roles, name=self.mention_group)
|
||||
return role
|
||||
|
||||
def get_random_phrase(self):
|
||||
def get_random_420_phrase(self):
|
||||
selected_phrase = None
|
||||
while not selected_phrase:
|
||||
with open('./modules/WillieTimer/Phrases.txt', 'r') as phrase_file:
|
||||
@@ -107,6 +124,36 @@ class WillieTimer(commands.Cog):
|
||||
selected_phrase = None
|
||||
continue
|
||||
|
||||
def get_random_warm_up_phrase(self):
|
||||
selected_phrase = None
|
||||
while not selected_phrase:
|
||||
selected_phrase = self.PG.pg_generate()
|
||||
|
||||
if selected_phrase is not None:
|
||||
if selected_phrase != self.current_phrase:
|
||||
self.current_phrase = selected_phrase
|
||||
return selected_phrase
|
||||
else:
|
||||
selected_phrase = None
|
||||
continue
|
||||
|
||||
def get_output_string(self):
|
||||
|
||||
willie_time_string = f"<@&{self.caller_name.id}>, Willie wanted me to tell you:\n" \
|
||||
f"\"{self.get_random_420_phrase()}\"\n" \
|
||||
f"<@&{self.bot_get_role().id}>"
|
||||
|
||||
warm_up_string = f"<@&{self.caller_name.id}> & <@&{self.bot_get_role().id}>! Heads up! " \
|
||||
f"Willie's passing an early message along:\n" \
|
||||
f"\"{self.get_random_warm_up_phrase()}\""
|
||||
|
||||
output_dict = {
|
||||
'420': str(willie_time_string),
|
||||
'warm up': str(warm_up_string)
|
||||
}
|
||||
|
||||
return output_dict
|
||||
|
||||
|
||||
def setup(bot: commands.Bot):
|
||||
bot.add_cog(WillieTimer(bot))
|
||||
|
||||
@@ -5,9 +5,9 @@ from textgenrnn import textgenrnn
|
||||
|
||||
class PhraseGenerator(textgenrnn):
|
||||
def __init__(self, input_training_file_path='./lyrics.txt', input_epochs=1, input_temperature=.5,
|
||||
input_model_file_path='./textgenrnn_weights.hdf5'):
|
||||
input_model_file_path='./WillieBotModel_weights.hdf5', logging_level=str(2)):
|
||||
# Set logging for Tensorflow
|
||||
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
|
||||
os.environ['TF_CPP_MIN_LOG_LEVEL'] = str(logging_level)
|
||||
|
||||
# Init vars
|
||||
self.training_file_path = input_training_file_path
|
||||
@@ -24,6 +24,7 @@ class PhraseGenerator(textgenrnn):
|
||||
def pg_generate(self):
|
||||
generated_text = self.generate(1, temperature=self.temperature, return_as_list=True)
|
||||
print(generated_text[0])
|
||||
return str(generated_text[0])
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
@@ -42,7 +43,8 @@ if __name__ == '__main__':
|
||||
|
||||
pg = PhraseGenerator(input_epochs=args['epochs'] if args['epochs'] else 1,
|
||||
input_training_file_path=args['training_file'] if args['training_file'] else './lyrics.txt',
|
||||
input_temperature=args['temp'] if args['temp'] else .5)
|
||||
input_temperature=args['temp'] if args['temp'] else .5,
|
||||
logging_level=str(2) if args['generate'] else str(0))
|
||||
|
||||
if args['train']:
|
||||
pg.pg_train()
|
||||
|
||||
Reference in New Issue
Block a user