File size: 10,244 Bytes
f21b8d3
 
13cb21a
f21b8d3
 
 
 
 
 
 
 
dc4be7f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f21b8d3
 
 
 
 
 
1edbd85
f21b8d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dc4be7f
f21b8d3
dc4be7f
f21b8d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dc4be7f
f21b8d3
 
 
 
 
dc4be7f
f21b8d3
dc4be7f
f21b8d3
 
 
 
 
 
 
 
 
 
dc4be7f
f21b8d3
dc4be7f
f21b8d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dc4be7f
f21b8d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bb6ab4b
594ec7a
 
e142fc2
dc4be7f
f21b8d3
 
 
dce8538
f21b8d3
bb6ab4b
e142fc2
594ec7a
e142fc2
f21b8d3
 
 
 
dce8538
bb6ab4b
e142fc2
594ec7a
e142fc2
dc4be7f
f21b8d3
 
ff4d91e
f21b8d3
 
ff4d91e
fa8d7c8
f21b8d3
 
 
 
dc4be7f
f21b8d3
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
import openai


ai_role_dict = {
"music_director": "You are an Experienced Music Director who has 15+ Years experience in the industry",
"lyricist": "You are an Experienced Lyricist, who has written hit songs in several languages",
"freelance_lyricist": "You are an Experienced Freelance Lyricist, who has helped writing songs in several languages",
"music_composer": "You are an Experienced Music Composer, who has composed songs of several genre and arrangements over the years",
"sound_engineer": "You are an Experienced Sound Engineer, who can provide expert feedback on the arrangement being used."
}

languages = [
"Afrikaans",
"Albanian",
"Amharic",
"Arabic",
"Armenian",
"Assamese",
"Aymara",
"Azerbaijani",
"Bhojpuri",
"Basque",
"Belarusian",
"Bengali",
"Bambara",
"Bosnian",
"Bulgarian",
"Burmese (Myanmar)",
"Catalan",
"Cebuano",
"Chewa (Chichewa)",
"Chinese (Simplified)",
"Chinese (Traditional)",
"Corsican",
"Croatian",
"Czech",
"Danish",
"Dogri",
"Dutch",
"English",
"Esperanto",
"Estonian",
"Ewe",
"Finnish",
"French",
"Galician",
"Georgian",
"German",
"Greek",
"Guarani",
"Gujarati",
"Haitian Creole",
"Hausa",
"Hawaiian",
"Hebrew",
"Hindi",
"Hmong",
"Hungarian",
"Icelandic",
"Igbo",
"Ilocano",
"Indonesian",
"Irish",
"Italian",
"Japanese",
"Javanese",
"Kannada",
"Kazakh",
"Khmer",
"Kinyarwanda",
"Konkani",
"Korean",
"Krio",
"Kurdish (Kurmanji)",
"Kurdish (Sorani)",
"Kyrgyz",
"Lao",
"Latin",
"Latvian",
"Lingala",
"Lithuanian",
"Luganda",
"Luxembourgish",
"Macedonian",
"Maithili",
"Malagasy",
"Malay",
"Malayalam",
"Maldivian (Dhivehi)",
"Maltese",
"Māori (Maori)",
"Marathi",
"Meitei (Manipuri, Meiteilon)",
"Mizo",
"Mongolian",
"Nepali",
"Northern Sotho (Sepedi)",
"Norwegian (Bokmål)",
"Odia (Oriya)",
"Oromo",
"Pashto",
"Persian",
"Polish",
"Portuguese",
"Punjabi (Gurmukhi)",
"Quechua",
"Romanian",
"Russian",
"Samoan",
"Sanskrit",
"Scottish Gaelic (Scots Gaelic)",
"Serbian",
"Shona",
"Sindhi",
"Sinhala",
"Slovak",
"Slovenian",
"Somali",
"Sotho (Sesotho)",
"Spanish",
"Sundanese",
"Swahili",
"Swedish",
"Tagalog (Filipino)",
"Tajik",
"Tamil",
"Tatar",
"Telugu",
"Thai",
"Tigrinya",
"Tsonga",
"Turkish",
"Turkmen",
"Twi",
"Ukrainian",
"Urdu",
"Uyghur",
"Uzbek",
"Vietnamese",
"Welsh",
"West Frisian (Frisian)",
"Xhosa",
"Yiddish",
"Yoruba",
"Zulu"
]

from tenacity import (
    retry,
    stop_after_attempt,
    wait_random_exponential,
)  # for exponential backoff

@retry(wait=wait_random_exponential(min=1, max=100), stop=stop_after_attempt(8))
def get_response(ai_role, query, model):

    response = openai.ChatCompletion.create(
    model=model,
    messages=[
            {"role": "system", "content": "{}".format(ai_role)},
            {"role": "user", "content": "{}".format(query)},
        ]
    )

    return response['choices'][0]['message']['content']

def write_intermediate_outputs(filename, text):
    with open(filename, 'w') as fw:
        fw.write(text)

    sample_file_path = f'./{filename}'

    return sample_file_path

def write_and_compose(model, api_key, language, genre, keywords, emotion):
    openai.api_key = api_key
    initial_lyrics = get_response(ai_role_dict['freelance_lyricist'], "Write structured lyrics of a {} {} song with the following keywords - {}, and use the following emotion - {}".format(language, genre, keywords, emotion), model)

    query_feedback = '''The Freelance Lyricist submitted these lyrics:

{}

Provide suitable feedback (in bullet-points)
'''

    feedback1 = get_response(ai_role_dict['music_director'], query_feedback.format(initial_lyrics), model)
    feedback2 = get_response(ai_role_dict['lyricist'], query_feedback.format(initial_lyrics), model)

    # Workflow: Step 3

    feedback = '''After seeing the lyrics you initially submitted -

{}

the music director provided the following feedback -
{}

the lyricist provided the following feedback as well -
{}

Incorporate this feedback, and make suggested changes to the lyrics based on the feedback only
'''

    final_lyrics = get_response(ai_role_dict['freelance_lyricist'], feedback.format(initial_lyrics, feedback1, feedback2), model)    

    # Workflow: Step 4

    query_composer = '''Given the lyrics of the {} {} song on {} in the emotion - {} -

{}

write a suitable chord progression (for each line of the same lyrics), followed by the suitable arrangement required to sing and record the song (in bullet points)'''

    composition_1 = get_response(ai_role_dict['music_composer'], query_composer.format(language, genre, keywords, emotion, final_lyrics), model)

    query_sound_engineer = '''Given the lyrics of the {} {} song on {} in the emotion - {} -

{}

with a Chord Progression and Arrangement (suggested by the Music Composer) - 

{}

could you write improvements that could be made to the Arrangement (in bullet points)? If the current arrangement is upto the mark, write "No change in the arrangement required"
'''

    composition_2 = get_response(ai_role_dict['sound_engineer'], query_sound_engineer.format(language, genre, keywords, emotion, final_lyrics, composition_1), model)    

    final_query = '''Given the lyrics of the {} {} song on {} in the emotion - {} -

{}

with a Chord Progression and Arrangement (suggested by the Music Composer) - 

{}

and further improvements on the Arrangement (suggested by the Sound Engineer)

{}

- suggest any further improvements that could be made to the (a) Chord Progression (b) Arrangement. 
- After that, Write 10 "="s in the next line
- After that, Write the final Chord Progression and Arrangement
- Also, write a suitable title for the song

'''

    final_response = get_response(ai_role_dict['music_director'], final_query.format(language, genre, keywords, emotion, final_lyrics, composition_1, composition_2), model)    

    final_improvements = final_response.split('==========')[0]

    final_chord_prog_and_composition = final_response.split('==========')[-1]

    # return initial_lyrics, feedback1, feedback2, final_lyrics, composition_1, composition_2, final_improvements, final_chord_prog_and_composition

    output_file_list = []
    output_file_list.append(write_intermediate_outputs('step_2.txt', initial_lyrics))
    output_file_list.append(write_intermediate_outputs('step_3A.txt', feedback1))
    output_file_list.append(write_intermediate_outputs('step_3B.txt', feedback2))
    output_file_list.append(write_intermediate_outputs('step_5.txt', composition_1))
    output_file_list.append(write_intermediate_outputs('step_6.txt', composition_2))
    output_file_list.append(write_intermediate_outputs('step_7.txt', final_improvements))

    return final_lyrics, final_chord_prog_and_composition, output_file_list

import gradio as gr

description = '''<span style="font-family:Papyrus; font-size:1.5em;">

# Objective -

Given specific Language, Genre, Keywords, and Emotion of your choice, make a Brand New Song without lifting a finger!

1. Get lyrics of a new song
2. Get a suitable chord progression
3. Get a suitable musical arrangement for singing and recording the song
4. Cherry on the top - Get a suitable song title!


# AI Music Team is composed of several GPT agents with the following "personas" -

1. Experienced Music Director who has 15+ Years experience in the industry
2. Experienced Lyricist, who has written hit songs in several languages
3. Experienced Freelance Lyricist, who has helped writing songs in several languages
4. Experienced Music Composer, who has composed songs of several genre and arrangements over the years
5. Experienced Sound Engineer, who can provide expert feedback on the arrangement being used


# Workflow (Intermediate outputs/results are output as downloadable files) -

1. Get Inputs from user (OpenAI API Endpoint, API Key, language, keywords, genre, emotion for the song). Check out [this link](https://platform.openai.com/account/api-keys) to get your API Key
2. Experienced Freelance Lyricist writes a lyrics draft (**see `step_2.txt`**) 
3. Experienced Music Director and Experienced Lyricist provide feedback (**see `step_3A.txt` & `step_3B.txt` respectively**)
4. Experienced Freelance Lyricist incorporates the feedback, **Lyrics is finalized here**
5. Experienced Music Composer will provide a chord progression, and an arrangement of instruments (**see `step_5.txt`**)
6. Experienced Sound Engineer will provide ways to improve on the existing arrangement (**see `step_6.txt`**)
7. Finally, Music Director will provide improvements (**see `step_7.txt`**), resulting in the **final Chord Progression, Arrangement, and Song Title**
</span>
'''

demo = gr.Interface(title = 'Write and Compose brand new Songs using an Elite *AI Music Team*', description = description,
    fn=write_and_compose,
    inputs=[gr.Radio(["gpt-3.5-turbo", "gpt-4"], value="gpt-3.5-turbo", label = "Choose the OpenAI API Endpoint"), gr.Textbox(label="API Key (Check out [this link](https://platform.openai.com/account/api-keys) to get your API Key)"), gr.Dropdown(choices=languages, value='English', label="Language of the lyrics"), gr.Textbox(label="Genre"), gr.Textbox(label="Keywords (separated by comma)"), gr.Textbox(label="Emotion")], # model, api_key, language, genre, keywords, emotion
    # outputs=[gr.Textbox(label="Lyrics after Step #2"), gr.Textbox(label="Feedback provided by Music Director in Step #3"), gr.Textbox(label="Feedback provided by Lyricist in Step #3"), gr.Textbox(label="Final Lyrics of the song after Step #4"), gr.Textbox(label="Chord Progression and Arrangement suggested by Music Composer in Step #5"), gr.Textbox(label="Arrangement improvements suggested by Sound Engineer in Step #6"), gr.Textbox(label="Chord and Arrangement improvements suggested by Music Director in Step #7"), gr.Textbox(label="Final Chord Progression, Arrangment, and Song Title")], # initial_lyrics, feedback1, feedback2, final_lyrics, composition_1, composition_2, final_improvements, final_chord_prog_and_composition
    outputs=[gr.Textbox(label="Final Lyrics of the song after Step #4"), gr.Textbox(label="Final Chord Progression, Arrangement, and Song Title"), gr.File(label='Intermediate Outputs')], # initial_lyrics, feedback1, feedback2, final_lyrics, composition_1, composition_2, final_improvements, final_chord_prog_and_composition
)
demo.launch()