diff --git a/.gitignore b/.gitignore index 0893afc..89abbc6 100644 --- a/.gitignore +++ b/.gitignore @@ -4,7 +4,5 @@ config.ini *.7z *.bat -*.hdf5 -*.json -/modules/*.txt /DSDPlus/ +._.DS_Store diff --git a/modules/WillieTimer/.gitignore b/modules/WillieTimer/.gitignore index 80c7d3c..700d7b9 100644 --- a/modules/WillieTimer/.gitignore +++ b/modules/WillieTimer/.gitignore @@ -1,2 +1,3 @@ -lyrics.py -phraseGenerator.py \ No newline at end of file +/lyrics.txt +*.hdf5 +*.json diff --git a/modules/WillieTimer/lyrics.py b/modules/WillieTimer/lyrics.py new file mode 100644 index 0000000..2f988eb --- /dev/null +++ b/modules/WillieTimer/lyrics.py @@ -0,0 +1,50 @@ +from lyricsgenius import Genius +import json +import re +import os + + +def get_songs(artists=["Notorious B.I.G", "outkast", "nwa"]): + GENIUS_TOKEN = "gMnJyj87FvjyP2W093rQ_mjo5ZwwLw1u2r0AmcVqYcJ8kkjjW6ZbObeGnS726SrH" + session = Genius(GENIUS_TOKEN, retries=2, timeout=20, sleep_time=0.3) + + lyrics = [] + + # get songs + for artist in artists: + songlist = session.search_artist(artist, max_songs=75, sort='title') + songlist.save_lyrics() + +def sanitize_lyrics(input): + notes_re = re.compile('((?:\[[0-9a-zA-Z :()&+-.]+\])(?: \+ \([a-zA-Z -.]+)?(?:\\n)?)') + footer_re = re.compile('((?:EmbedShare)[ ]*(?:URLCopyEmbedCopy))') + multiline_re = re.compile(('(\\n){3,}')) + sanitized_input = notes_re.sub('', input) + sanitized_input = footer_re.sub('', sanitized_input) + sanitized_input = multiline_re.sub('\n\n', sanitized_input) + return sanitized_input + +def get_lyrics_from_json(json_file): + artist_dict = json.load(json_file) + ready_lyrics = [] + print(artist_dict.keys()) + for song in artist_dict['songs']: + sanitized_lyrics = sanitize_lyrics(song['lyrics']) + print(sanitized_lyrics) + ready_lyrics.append(sanitized_lyrics) + return ready_lyrics + +def save_sanitized_lyrics(): + sanitized_lyrics_list = [] + for file in os.listdir("./"): + if file.endswith(".json"): + with open(file, 'r', encoding="utf-8") as read_file: + sanitized_lyrics_list.extend(get_lyrics_from_json(read_file)) + print(sanitized_lyrics_list) + with open('./lyrics.txt', 'w+', encoding="utf-8") as lyrics_file: + for lyrics in sanitized_lyrics_list: + print(lyrics) + lyrics_file.write(f"{lyrics}\n") + + +save_sanitized_lyrics() diff --git a/modules/WillieTimer/phraseGenerator.py b/modules/WillieTimer/phraseGenerator.py new file mode 100644 index 0000000..c183add --- /dev/null +++ b/modules/WillieTimer/phraseGenerator.py @@ -0,0 +1,51 @@ +import os +import argparse +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'): + # Set logging for Tensorflow + os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' + + # Init vars + self.training_file_path = input_training_file_path + self.model_file_path = input_model_file_path + self.epochs = input_epochs + self.temperature = input_temperature + + # Init Textgenrnn + super().__init__(weights_path=self.model_file_path, allow_growth=True, name='WillieBotModel') + + def pg_train(self): + self.train_from_file(self.training_file_path, num_epochs=self.epochs, verbose=0, top_n=5, return_as_list=True) + + def pg_generate(self): + generated_text = self.generate(1, temperature=self.temperature, return_as_list=True) + print(generated_text[0]) + + +if __name__ == '__main__': + parser = argparse.ArgumentParser(description='Description of your program') + parser.add_argument('-t', '--train', action='store_true', help='Train the model', required=False) + parser.add_argument('-g', '--generate', action='store_true', help='Generate text', required=False) + parser.add_argument('-e', '--epochs', action='store', type=int, help='Set amount of epochs (defaults to 5)', + required=False) + parser.add_argument('-p', '--temp', action='store', type=int, + help='Set temperature for generation (defaults to .5)', required=False) + parser.add_argument('-f', '--training_file', action='store', type=str, + help='Set the training file (defaults to \'./lyrics.txt\')', required=False) + args = vars(parser.parse_args()) + print(args) + print('Starting') + + 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) + + if args['train']: + pg.pg_train() + + if args['generate']: + pg.pg_generate()