Spaces:
Runtime error
Runtime error
| 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 | |
| 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() |