Bachstelze commited on
Commit
297995b
1 Parent(s): 000f2c6

add algo to generate one song

Browse files
Files changed (1) hide show
  1. README.md +101 -0
README.md CHANGED
@@ -39,4 +39,105 @@ Ich hab' Jungs in der Trap, ich hab' Jungs an der Uni (Ahh)
39
  [Post-Hook_nullsechsroy_Goodies]
40
  Ja, ich weiß, sie findet niemals ein'n wie mich (Ahh)
41
  ...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  ```
 
39
  [Post-Hook_nullsechsroy_Goodies]
40
  Ja, ich weiß, sie findet niemals ein'n wie mich (Ahh)
41
  ...
42
+ ```
43
+
44
+ # Source code to create a song
45
+
46
+ ```
47
+ from transformers import pipeline, AutoTokenizer,AutoModelForCausalLM
48
+
49
+ # load the model from huggingface
50
+ rap_model = AutoModelForCausalLM.from_pretrained("Bachstelze/poetryRapGPT")
51
+ tokenizer = AutoTokenizer.from_pretrained("Anjoe/german-poetry-gpt2")
52
+ rap_pipe = pipeline('text-generation',
53
+ model=rap_model,
54
+ tokenizer=german_gpt_model,
55
+ pad_token_id=tokenizer.eos_token_id,
56
+ max_length=250)
57
+
58
+ # set the artist
59
+ song_artist = "nullsechsroy" # "nullsechsroy Deluxe"
60
+ # add a title idea or leave it blank
61
+ title = "" # "Kristall" "Fit"
62
+
63
+ # definition of the song structure
64
+ type_with_linenumbers = [("Intro",4),
65
+ ("Hook",4),
66
+ ("Part 1",6),
67
+ ("Part 2",6),
68
+ ("Outro",4)]
69
+
70
+
71
+ def set_title(song_parts):
72
+ """
73
+ we create a title if it isn't set already
74
+ and add the title to the songs parts dictionary
75
+ """
76
+ if len(title) > 0:
77
+ song_parts["Title"] = "\n[Title_" + song_artist + "_" + title + "]\n"
78
+ song_parts["artist_with_title"] = song_artist + "_" + title
79
+ else:
80
+ title_input = "\n[Title_" + song_artist + "_"
81
+ title_lines = rap_pipe(title_input)[0]['generated_text']
82
+ index_title_end = title_lines.index("]\n")
83
+ artist_with_title = title_lines[8:index_title_end]
84
+ song_parts["Title"] = title_lines[:index_title_end+1]
85
+ song_parts["artist_with_title"] = artist_with_title
86
+
87
+ def create_song_by_parts():
88
+ """
89
+ we iterate over the song structure
90
+ and return the dictionary with the song parts
91
+ """
92
+ song_parts = {}
93
+ set_title(song_parts)
94
+
95
+ for (part_type, line_number) in type_with_linenumbers:
96
+ new_song_part = create_song_part(part_type, song_parts["artist_with_title"], line_number)
97
+ song_parts[part_type] = new_song_part
98
+
99
+ return song_parts
100
+
101
+ def get_line(pipe_input, line_number):
102
+ """
103
+ We generate a new song line.
104
+ This function could be scaled to more lines.
105
+ """
106
+ new_lines = rap_pipe(pipe_input)[0]['generated_text'].split("\n")
107
+ if len(new_lines) > line_number + 3:
108
+ new_line = new_lines[line_number+3] + "\n"
109
+ return new_line
110
+ else: #retry
111
+ return get_line(pipe_input, line_number)
112
+
113
+ def create_song_part(part_type, artist_with_title, lines_number):
114
+ """
115
+ we generate one song part
116
+ """
117
+ start_type = "\n["+part_type+"_"+artist_with_title+"]\n"
118
+ song_part = start_type # + preset start line
119
+ lines = [""]
120
+
121
+ for line_number in range(lines_number):
122
+ pipe_input = start_type + lines[-1]
123
+ new_line = get_line(pipe_input, line_number)
124
+ lines.append(new_line)
125
+ song_part += new_line
126
+ return song_part
127
+
128
+ def print_song(song_parts):
129
+ """
130
+ Let's print the generated song
131
+ """
132
+ print(song_parts["Title"])
133
+ print(song_parts["Intro"])
134
+ print(song_parts["Part 1"])
135
+ print(song_parts["Hook"])
136
+ print(song_parts["Part 2"])
137
+ print(song_parts["Hook"])
138
+ print(song_parts["Outro"])
139
+
140
+ # start the generation of one song
141
+ song_parts = create_song_by_parts()
142
+ print_song(song_parts)
143
  ```