Spaces:
Running
Running
| import os | |
| import gradio as gr | |
| from glm_huggingface import run_glm | |
| from open_ai import run_openai | |
| from prompt_loader import get_prompt | |
| from dotenv import load_dotenv | |
| load_dotenv() | |
| if os.getenv("OPENAI_API_KEY",False): | |
| status = "using OPENAI_API_KEY" | |
| run_llm=run_openai | |
| else: | |
| status = "using glm from huggingface" | |
| run_llm=run_glm | |
| PROMPT_STYLE = get_prompt("PROMPT_STYLE") | |
| PROMPT_TEXT = get_prompt("PROMPT_TEXT") | |
| PROMPT_TEXT2 = get_prompt("PROMPT_TEXT2") | |
| SYSPROMPT_STYLE = get_prompt("SYSPROMPT_STYLE") | |
| SYSPROMPT_TEXT = get_prompt("SYSPROMPT_TEXT") | |
| PROMT_GEN_LYRICS = get_prompt("PROMT_GEN_LYRICS") | |
| SYSPROMT_GEN_LYRICS = get_prompt("SYSPROMT_GEN_LYRICS") | |
| def get_style_lyrics(style, lyrics, gen_lyrics: bool =True): | |
| try: | |
| style_gen = run_llm(f"Describe {style}", sys_prompt=SYSPROMPT_STYLE) | |
| if not style_gen: | |
| style_gen = '' | |
| except Exception as e: | |
| print(f"ERROR infer llm: {e}") | |
| style_gen = '' | |
| if gen_lyrics: | |
| try: | |
| gen_lyrics = run_llm(f"{PROMT_GEN_LYRICS}\n<THEME>{lyrics}</THEME>", sys_prompt=SYSPROMT_GEN_LYRICS) | |
| print (gen_lyrics) | |
| if gen_lyrics: | |
| lyrics=gen_lyrics | |
| else: | |
| lyrics='' | |
| except Exception as e: | |
| print(f"ERROR infer llm: {e}") | |
| lyrics='' | |
| try: | |
| text_glm = run_llm(f"{PROMPT_TEXT}\n{style_gen}\n#Text song:\n{lyrics}\n{PROMPT_TEXT2}", sys_prompt=SYSPROMPT_TEXT) | |
| if text_glm: | |
| text = text_glm | |
| else: | |
| text = '' | |
| except Exception as e: | |
| print(f"ERROR infer llm: {e}") | |
| text = '' | |
| return style_gen, text | |
| isGenarateLyrics=False | |
| # Define translatable strings | |
| english_strings = { | |
| "title": "SUNO style generator and lyrics format", | |
| "performer_label": "Preferred Music Style:", | |
| "performer_placeholder": "Describe the desired music style in detail: you can blend different genres and influences, mention specific artists, instruments, rhythm, and overall mood. Specify the vocal type — male, female, high, raspy, smoky, operatic, or any other. Feel free to experiment — mix styles, evoke atmosphere, and express the emotions you want the music to convey.", | |
| "song_text_label": "Lyrics:", | |
| "song_text_placeholder": "Enter your song lyrics to format", | |
| "song_gen_label": "Idea for lyrics:", | |
| "song_gen_placeholder": "Share your inspiration — describe what you dream of singing about. Sketch out your song idea: emotions, events, places, characters — everything that matters. Tell the story you want to turn into music.", | |
| "language_english": "English", | |
| "language_russian": "Russian", | |
| "generate_button": "Generate", | |
| "style_label": "AI-Generated Style:", | |
| "song_result_label": "Formated song text:", | |
| "isGenerate": "generate lyrics (experemental)" | |
| } | |
| russian_strings = { | |
| "title": "SUNO генератор стиля музыки и форматирования структуры песни", | |
| "performer_label": "Желаемый музыкальный стиль:", | |
| "performer_placeholder": "Опишите стиль музыки: можно сочетать разные жанры и направления, конкретных исполнителей, инструменты, ритм и настроение, тип вокала — мужской, женский, высокий, надрывный, прокуренный, оперный или любой другой. Передавайте атмосферу и эмоции, которые хотите услышать.", | |
| "song_text_label": "Текст песни:", | |
| "song_text_placeholder": "Введите текст песни для форматирования", | |
| "song_gen_label": "Опишите идею для текста песни:", | |
| "song_gen_placeholder": "Поделитесь вдохновением — опишите, о чём вы мечтаете спеть. Набросайте идею для песни: эмоции, события, место, герои — всё, что важно. Расскажите историю, которую хотите превратить в музыку.", | |
| "language_english": "Английский: ", | |
| "language_russian": "Русский: ", | |
| "generate_button": "Сгенерировать", | |
| "style_label": "AI-сгенерированный музыкальный стиль: ", | |
| "song_result_label": "Отформатированный текст песни: ", | |
| "isGenerate": "придумать текст песни (экспериментальная)" | |
| } | |
| lang = "EN" | |
| if lang=="EN": | |
| TS=english_strings | |
| else: | |
| TS=russian_strings | |
| def process_lang(selected_lang, mode): | |
| lang=selected_lang | |
| if selected_lang == "RU": | |
| TS=russian_strings | |
| message="Вы выбрали русский" | |
| isVisible=True | |
| elif selected_lang == "EN": | |
| TS=english_strings | |
| message="You selected English" | |
| else: | |
| message="" | |
| isVisible=False | |
| ret = [gr.update(label=TS["performer_label"],placeholder=TS["performer_placeholder"]), | |
| gr.update(label=TS["song_text_label" if not mode else "song_gen_label"], | |
| placeholder=TS["song_text_placeholder" if not mode else "song_gen_placeholder"]), | |
| gr.update(label=TS["style_label"]), | |
| gr.update(label=TS["song_result_label"]), | |
| gr.update(value=TS["generate_button"]), | |
| gr.update(label=TS["isGenerate"]) | |
| ] | |
| return message, *ret | |
| with gr.Blocks(title="Suno PromptGen", fill_height=True, | |
| theme=gr.themes.Default(primary_hue=gr.themes.colors.sky, secondary_hue=gr.themes.colors.indigo), | |
| analytics_enabled=False, css="footer{display:none !important}") as demo: | |
| with gr.Row(elem_id="header", equal_height=True): | |
| with gr.Column(scale=0, min_width=100): | |
| gr.Image(label="header AiCave", | |
| value="splash.webp", | |
| width=100, | |
| height=100, | |
| show_label=False, | |
| interactive=False, | |
| show_download_button=False, | |
| show_fullscreen_button=False, | |
| show_share_button=False, | |
| elem_id="logo" | |
| ) | |
| with gr.Column(scale=80,): | |
| gr.Markdown(f"# {TS['title']}\n## by <a href='https://boosty.to/aicave/donate'>AiCave</a>") | |
| with gr.Column(scale=10): | |
| radio_lang = gr.Radio(choices = ["RU", "EN"], show_label = False, container = False, type = "value", value=lang) | |
| with gr.Column(scale=10): | |
| generate_button = gr.Button(TS["generate_button"], variant="primary", size="lg") | |
| with gr.Row(): | |
| with gr.Column(): | |
| name_input = gr.Textbox(label=TS["performer_label"],lines=5, max_lines=10, | |
| placeholder=TS["performer_placeholder"], autofocus=True) | |
| text_input = gr.Textbox(label=TS["song_text_label"], lines=25, max_lines=25, | |
| placeholder=TS["song_text_placeholder"]) | |
| isGenerate = gr.Checkbox(value=False, label=TS["isGenerate"], show_label=True) | |
| with gr.Column(show_progress=False): | |
| style_output = gr.Textbox(label=TS["style_label"],lines=5, max_lines=10, | |
| show_copy_button=True,) | |
| song_output = gr.Textbox(label=TS["song_result_label"], lines=28, max_lines=28,show_copy_button=True) | |
| with gr.Row(variant="default"): | |
| log_text = gr.Textbox(value=status, container=False, lines=1, max_lines=1) | |
| generate_button.click( | |
| fn=get_style_lyrics, | |
| inputs=[name_input, text_input, isGenerate], | |
| outputs=[style_output, song_output], | |
| api_name="GenSuno", | |
| show_api=True | |
| ) | |
| radio_lang.change(process_lang, inputs=[radio_lang, isGenerate], | |
| outputs=[log_text,name_input,text_input,style_output,song_output,generate_button,isGenerate], | |
| api_name=False, show_api=False) | |
| isGenerate.change(process_lang, inputs=[radio_lang, isGenerate], | |
| outputs=[log_text,name_input,text_input,style_output,song_output,generate_button,isGenerate], | |
| api_name=False, show_api=False) | |
| demo.css = """ | |
| #header { | |
| align-items: center; | |
| padding: 0px 0px; | |
| border-bottom: None; | |
| } | |
| """ | |
| if __name__ == "__main__": | |
| demo.launch(quiet = False) |