Spaces:
Running
Running
Upload folder using huggingface_hub
Browse files
app.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from glm_huggingface import run_glm
|
| 4 |
from open_ai import run_openai
|
|
@@ -26,28 +27,70 @@ SYSPROMT_GEN_LYRICS = get_prompt("SYSPROMT_GEN_LYRICS")
|
|
| 26 |
SYSPROMT_GEN_STYLE_FROM_LYRICS = get_prompt("SYSPROMT_GEN_STYLE_FROM_LYRICS")
|
| 27 |
PROMT_GEN_STYLE_FROM_LYRICS = get_prompt("PROMT_GEN_STYLE_FROM_LYRICS")
|
| 28 |
|
| 29 |
-
def get_style_lyrics(style: str, lyrics: str, gen_lyrics: bool, gen_style_from_lyrics: bool
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
if gen_lyrics:
|
|
|
|
|
|
|
|
|
|
| 32 |
try:
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
lyrics=gen_lyrics
|
| 37 |
else:
|
| 38 |
-
lyrics=''
|
| 39 |
except Exception as e:
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
|
|
|
|
|
|
| 43 |
if gen_style_from_lyrics:
|
| 44 |
try:
|
| 45 |
-
style_gen = run_llm(f"{PROMT_GEN_STYLE_FROM_LYRICS}\n<SONG_TEXT><LYRICS>\n{lyrics}\n</LYRICS></SONG_TEXT>",
|
| 46 |
sys_prompt=SYSPROMT_GEN_STYLE_FROM_LYRICS)
|
| 47 |
if not style_gen:
|
| 48 |
style_gen = ''
|
| 49 |
except Exception as e:
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
| 51 |
style_gen = ''
|
| 52 |
else:
|
| 53 |
try:
|
|
@@ -55,10 +98,19 @@ def get_style_lyrics(style: str, lyrics: str, gen_lyrics: bool, gen_style_from_l
|
|
| 55 |
if not style_gen:
|
| 56 |
style_gen = ''
|
| 57 |
except Exception as e:
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
| 59 |
style_gen = ''
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
|
|
|
|
|
|
|
| 62 |
try:
|
| 63 |
text_glm = run_llm(f"{PROMPT_TEXT}\n{style_gen}\n#Text song:\n{lyrics}\n{PROMPT_TEXT2}", sys_prompt=SYSPROMPT_TEXT)
|
| 64 |
if text_glm:
|
|
@@ -66,10 +118,21 @@ def get_style_lyrics(style: str, lyrics: str, gen_lyrics: bool, gen_style_from_l
|
|
| 66 |
else:
|
| 67 |
text = ''
|
| 68 |
except Exception as e:
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
text = ''
|
| 71 |
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
isGenarateLyrics=False
|
| 75 |
|
|
@@ -88,7 +151,26 @@ english_strings = {
|
|
| 88 |
"style_label": "AI-Generated Style:",
|
| 89 |
"song_result_label": "Formated song text:",
|
| 90 |
"isGenerateLyrics": "generate lyrics (experemental)",
|
| 91 |
-
"isGenerateStyle": "generate music style from lyrics"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
}
|
| 93 |
|
| 94 |
russian_strings = {
|
|
@@ -105,7 +187,26 @@ russian_strings = {
|
|
| 105 |
"style_label": "AI-сгенерированный музыкальный стиль: ",
|
| 106 |
"song_result_label": "Отформатированный текст песни: ",
|
| 107 |
"isGenerateLyrics": "придумать текст песни (экспериментальная)",
|
| 108 |
-
"isGenerateStyle": "придумать стиль музыки по тексту песни"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
}
|
| 110 |
|
| 111 |
lang = "EN"
|
|
@@ -116,6 +217,7 @@ else:
|
|
| 116 |
TS=russian_strings
|
| 117 |
|
| 118 |
def process_lang(selected_lang, isGenerateLyrics, isGenerateStyle):
|
|
|
|
| 119 |
lang=selected_lang
|
| 120 |
if selected_lang == "RU":
|
| 121 |
TS=russian_strings
|
|
@@ -134,7 +236,8 @@ def process_lang(selected_lang, isGenerateLyrics, isGenerateStyle):
|
|
| 134 |
gr.update(label=TS["song_result_label"]),
|
| 135 |
gr.update(value=TS["generate_button"]),
|
| 136 |
gr.update(label=TS["isGenerateLyrics"]),
|
| 137 |
-
gr.update(label=TS["isGenerateStyle"])
|
|
|
|
| 138 |
]
|
| 139 |
return message, *ret
|
| 140 |
|
|
@@ -180,28 +283,29 @@ with gr.Blocks(title="Suno PromptGen", fill_height=True,
|
|
| 180 |
song_output = gr.Textbox(label=TS["song_result_label"], lines=25, max_lines=25,show_copy_button=True)
|
| 181 |
|
| 182 |
with gr.Row(variant="default"):
|
| 183 |
-
log_text = gr.Textbox(value=status, container=False, lines=1, max_lines=
|
|
|
|
| 184 |
|
| 185 |
generate_button.click(
|
| 186 |
fn=get_style_lyrics,
|
| 187 |
inputs=[name_input, text_input, isGenerateLyrics, isGenerateStyle],
|
| 188 |
-
outputs=[style_output, song_output],
|
| 189 |
api_name="GenSuno",
|
| 190 |
show_api=True
|
| 191 |
-
)
|
| 192 |
|
| 193 |
radio_lang.change(process_lang, inputs=[radio_lang, isGenerateLyrics, isGenerateStyle],
|
| 194 |
outputs=[log_text,name_input,text_input,style_output,song_output,generate_button,
|
| 195 |
-
isGenerateLyrics, isGenerateStyle],
|
| 196 |
api_name=False, show_api=False)
|
| 197 |
|
| 198 |
isGenerateLyrics.change(process_lang, inputs=[radio_lang, isGenerateLyrics, isGenerateStyle],
|
| 199 |
outputs=[log_text,name_input,text_input,style_output,song_output,generate_button,
|
| 200 |
-
isGenerateLyrics, isGenerateStyle],
|
| 201 |
api_name=False, show_api=False)
|
| 202 |
isGenerateStyle.change(process_lang, inputs=[radio_lang, isGenerateLyrics, isGenerateStyle],
|
| 203 |
outputs=[log_text,name_input,text_input,style_output,song_output,generate_button,
|
| 204 |
-
isGenerateLyrics, isGenerateStyle],
|
| 205 |
api_name=False, show_api=False)
|
| 206 |
|
| 207 |
demo.css = """
|
|
|
|
| 1 |
import os
|
| 2 |
+
import time
|
| 3 |
import gradio as gr
|
| 4 |
from glm_huggingface import run_glm
|
| 5 |
from open_ai import run_openai
|
|
|
|
| 27 |
SYSPROMT_GEN_STYLE_FROM_LYRICS = get_prompt("SYSPROMT_GEN_STYLE_FROM_LYRICS")
|
| 28 |
PROMT_GEN_STYLE_FROM_LYRICS = get_prompt("PROMT_GEN_STYLE_FROM_LYRICS")
|
| 29 |
|
| 30 |
+
def get_style_lyrics(style: str, lyrics: str, gen_lyrics: bool, gen_style_from_lyrics: bool):
|
| 31 |
+
# Detect current UI language from global TS variable
|
| 32 |
+
current_lang = "RU" if TS == russian_strings else "EN"
|
| 33 |
+
|
| 34 |
+
# Initialize timing and logging variables
|
| 35 |
+
start_time = time.time()
|
| 36 |
+
stage_times = {}
|
| 37 |
+
errors = []
|
| 38 |
|
| 39 |
+
|
| 40 |
+
# Helper function to get localized message
|
| 41 |
+
def get_msg(key):
|
| 42 |
+
return TS.get(key, english_strings.get(key, f"Missing key: {key}"))
|
| 43 |
+
|
| 44 |
+
# Helper function to yield progress with timing
|
| 45 |
+
def yield_progress(message_key, status="🔄", **kwargs):
|
| 46 |
+
message = get_msg(message_key).format(**kwargs)
|
| 47 |
+
elapsed = time.time() - start_time
|
| 48 |
+
progress_msg = f"{status} {message} (⏱️ {elapsed:.2f}s)"
|
| 49 |
+
print(f"[PROGRESS] {message}")
|
| 50 |
+
return progress_msg
|
| 51 |
+
|
| 52 |
+
log = ''
|
| 53 |
+
yield ("", "", "🚀" + get_msg("log_starting"))
|
| 54 |
+
|
| 55 |
+
gen_start = time.time()
|
| 56 |
+
# Stage 1: Lyrics Generation (if requested)
|
| 57 |
if gen_lyrics:
|
| 58 |
+
stage_start = time.time()
|
| 59 |
+
yield ("", lyrics, log + " ⏳" + get_msg("log_lyrics_gen_start"))
|
| 60 |
+
|
| 61 |
try:
|
| 62 |
+
gen_lyrics_result = run_llm(f"{PROMT_GEN_LYRICS}\n<THEME>{lyrics}</THEME>", sys_prompt=SYSPROMT_GEN_LYRICS)
|
| 63 |
+
if gen_lyrics_result:
|
| 64 |
+
lyrics = gen_lyrics_result
|
|
|
|
| 65 |
else:
|
| 66 |
+
lyrics = ''
|
| 67 |
except Exception as e:
|
| 68 |
+
stage_time = time.time() - stage_start
|
| 69 |
+
stage_times['lyrics_generation'] = stage_time
|
| 70 |
+
error_msg = f"ERROR infer llm: {e}"
|
| 71 |
+
errors.append(error_msg)
|
| 72 |
+
yield yield_progress("log_lyrics_gen_error", status="❌", error=str(e))
|
| 73 |
+
print(error_msg)
|
| 74 |
+
lyrics = ''
|
| 75 |
+
stage_time = time.time() - stage_start
|
| 76 |
+
stage_times['lyrics_generation'] = stage_time
|
| 77 |
+
log += " "+yield_progress("log_lyrics_gen_complete", status=" 🖌️", time=stage_time)
|
| 78 |
+
yield ("", lyrics, log + " ⏳" + get_msg("log_style_gen_start" if not gen_style_from_lyrics else "log_style_desc_start"))
|
| 79 |
+
|
| 80 |
|
| 81 |
+
# Stage 2: Style Generation
|
| 82 |
+
stage_start = time.time()
|
| 83 |
if gen_style_from_lyrics:
|
| 84 |
try:
|
| 85 |
+
style_gen = run_llm(f"{PROMT_GEN_STYLE_FROM_LYRICS}\n<MUSIC_STYLE>{style}</MISIC_STYLE><SONG_TEXT><LYRICS>\n{lyrics}\n</LYRICS></SONG_TEXT>",
|
| 86 |
sys_prompt=SYSPROMT_GEN_STYLE_FROM_LYRICS)
|
| 87 |
if not style_gen:
|
| 88 |
style_gen = ''
|
| 89 |
except Exception as e:
|
| 90 |
+
error_msg = f"ERROR infer llm: {e}"
|
| 91 |
+
errors.append(error_msg)
|
| 92 |
+
yield yield_progress("log_style_gen_error", status="❌", error=str(e))
|
| 93 |
+
print(error_msg)
|
| 94 |
style_gen = ''
|
| 95 |
else:
|
| 96 |
try:
|
|
|
|
| 98 |
if not style_gen:
|
| 99 |
style_gen = ''
|
| 100 |
except Exception as e:
|
| 101 |
+
error_msg = f"ERROR infer llm: {e}"
|
| 102 |
+
errors.append(error_msg)
|
| 103 |
+
yield yield_progress("log_style_gen_error", status="❌", error=str(e))
|
| 104 |
+
print(error_msg)
|
| 105 |
style_gen = ''
|
| 106 |
+
stage_time = time.time() - stage_start
|
| 107 |
+
stage_times['style_generation'] = stage_time
|
| 108 |
+
log += " "+ yield_progress("log_style_gen_complete", status=" 🎨", time=stage_time)
|
| 109 |
+
yield (style_gen, lyrics, log + " ⏳" + get_msg("log_text_format_start"))
|
| 110 |
|
| 111 |
|
| 112 |
+
# Stage 3: Text Formatting
|
| 113 |
+
stage_start = time.time()
|
| 114 |
try:
|
| 115 |
text_glm = run_llm(f"{PROMPT_TEXT}\n{style_gen}\n#Text song:\n{lyrics}\n{PROMPT_TEXT2}", sys_prompt=SYSPROMPT_TEXT)
|
| 116 |
if text_glm:
|
|
|
|
| 118 |
else:
|
| 119 |
text = ''
|
| 120 |
except Exception as e:
|
| 121 |
+
stage_time = time.time() - stage_start
|
| 122 |
+
stage_times['text_formatting'] = stage_time
|
| 123 |
+
error_msg = f"ERROR infer llm: {e}"
|
| 124 |
+
errors.append(error_msg)
|
| 125 |
+
yield yield_progress("log_text_format_error", status="❌", error=str(e))
|
| 126 |
+
print(error_msg)
|
| 127 |
text = ''
|
| 128 |
|
| 129 |
+
stage_time = time.time() - stage_start
|
| 130 |
+
# Final summary
|
| 131 |
+
total_time = time.time() - gen_start
|
| 132 |
+
log += " " + yield_progress("log_text_format_complete", status=" 🧾", time=stage_time)
|
| 133 |
+
yield (style_gen, text, log)
|
| 134 |
+
|
| 135 |
+
|
| 136 |
|
| 137 |
isGenarateLyrics=False
|
| 138 |
|
|
|
|
| 151 |
"style_label": "AI-Generated Style:",
|
| 152 |
"song_result_label": "Formated song text:",
|
| 153 |
"isGenerateLyrics": "generate lyrics (experemental)",
|
| 154 |
+
"isGenerateStyle": "generate music style from lyrics",
|
| 155 |
+
# Progress logging messages
|
| 156 |
+
"log_starting": "Starting SUNO prompt generation...",
|
| 157 |
+
"log_lyrics_gen_start": "Generating lyrics from idea...",
|
| 158 |
+
"log_lyrics_gen_complete": "Lyrics generation completed in {time:.2f}s",
|
| 159 |
+
"log_lyrics_gen_error": "Error generating lyrics: {error}",
|
| 160 |
+
"log_style_gen_start": "Generating music style from lyrics...",
|
| 161 |
+
"log_style_desc_start": "Generating music style description...",
|
| 162 |
+
"log_style_gen_complete": "Style generation completed in {time:.2f}s",
|
| 163 |
+
"log_style_gen_error": "Error generating style: {error}",
|
| 164 |
+
"log_text_format_start": "Formatting song text...",
|
| 165 |
+
"log_text_format_complete": "Text formatting completed in {time:.2f}s",
|
| 166 |
+
"log_text_format_error": "Error formatting text: {error}",
|
| 167 |
+
"log_summary_success": "✅ Generation completed successfully!",
|
| 168 |
+
"log_summary_error": "❌ Generation completed with errors",
|
| 169 |
+
"log_summary_total": "Total execution time: {time:.2f}s",
|
| 170 |
+
"log_summary_stages": "Stage timings: {stages}",
|
| 171 |
+
"generation_warning_msg": """**⚠️ Note:** the generation model (*GLM*) is not among the strongest ones and was chosen for its availability on *Hugging Face*.
|
| 172 |
+
If the output looks off, simply try generating again — it usually fixes the issue.
|
| 173 |
+
"""
|
| 174 |
}
|
| 175 |
|
| 176 |
russian_strings = {
|
|
|
|
| 187 |
"style_label": "AI-сгенерированный музыкальный стиль: ",
|
| 188 |
"song_result_label": "Отформатированный текст песни: ",
|
| 189 |
"isGenerateLyrics": "придумать текст песни (экспериментальная)",
|
| 190 |
+
"isGenerateStyle": "придумать стиль музыки по тексту песни",
|
| 191 |
+
# Progress logging messages
|
| 192 |
+
"log_starting": "Начинаем генерацию SUNO промптов...",
|
| 193 |
+
"log_lyrics_gen_start": "Генерируем текст песни из идеи...",
|
| 194 |
+
"log_lyrics_gen_complete": "Генерация текста завершена за {time:.2f}с",
|
| 195 |
+
"log_lyrics_gen_error": "Ошибка генерации текста: {error}",
|
| 196 |
+
"log_style_gen_start": "Генерируем музыкальный стиль из текста...",
|
| 197 |
+
"log_style_desc_start": "Генерируем описание музыкального стиля...",
|
| 198 |
+
"log_style_gen_complete": "Генерация стиля завершена за {time:.2f}с",
|
| 199 |
+
"log_style_gen_error": "Ошибка генерации стиля: {error}",
|
| 200 |
+
"log_text_format_start": "Форматируем текст песни...",
|
| 201 |
+
"log_text_format_complete": "Форматирование текста завершено за {time:.2f}с",
|
| 202 |
+
"log_text_format_error": "Ошибка форматирования текста: {error}",
|
| 203 |
+
"log_summary_success": "✅ Генерация успешно завершена!",
|
| 204 |
+
"log_summary_error": "❌ Генерация завершена с ошибками",
|
| 205 |
+
"log_summary_total": "Общее время выполнения: {time:.2f}с",
|
| 206 |
+
"log_summary_stages": "Время этапов: {stages}",
|
| 207 |
+
"generation_warning_msg": """
|
| 208 |
+
**⚠️ Примечание:** используется не самая мощная модель генерации (*GLM*), выбранная из-за доступности на *Hugging Face*.
|
| 209 |
+
Иногда результат может содержать ошибки — чаще всего помогает просто повторная генерация."""
|
| 210 |
}
|
| 211 |
|
| 212 |
lang = "EN"
|
|
|
|
| 217 |
TS=russian_strings
|
| 218 |
|
| 219 |
def process_lang(selected_lang, isGenerateLyrics, isGenerateStyle):
|
| 220 |
+
global TS
|
| 221 |
lang=selected_lang
|
| 222 |
if selected_lang == "RU":
|
| 223 |
TS=russian_strings
|
|
|
|
| 236 |
gr.update(label=TS["song_result_label"]),
|
| 237 |
gr.update(value=TS["generate_button"]),
|
| 238 |
gr.update(label=TS["isGenerateLyrics"]),
|
| 239 |
+
gr.update(label=TS["isGenerateStyle"]),
|
| 240 |
+
gr.update(value=TS["generation_warning_msg"])
|
| 241 |
]
|
| 242 |
return message, *ret
|
| 243 |
|
|
|
|
| 283 |
song_output = gr.Textbox(label=TS["song_result_label"], lines=25, max_lines=25,show_copy_button=True)
|
| 284 |
|
| 285 |
with gr.Row(variant="default"):
|
| 286 |
+
log_text = gr.Textbox(value=status, container=False, lines=1, max_lines=3, label="log")
|
| 287 |
+
warning_msg = gr.Markdown(value=TS["generation_warning_msg"])
|
| 288 |
|
| 289 |
generate_button.click(
|
| 290 |
fn=get_style_lyrics,
|
| 291 |
inputs=[name_input, text_input, isGenerateLyrics, isGenerateStyle],
|
| 292 |
+
outputs=[style_output, song_output, log_text],
|
| 293 |
api_name="GenSuno",
|
| 294 |
show_api=True
|
| 295 |
+
)
|
| 296 |
|
| 297 |
radio_lang.change(process_lang, inputs=[radio_lang, isGenerateLyrics, isGenerateStyle],
|
| 298 |
outputs=[log_text,name_input,text_input,style_output,song_output,generate_button,
|
| 299 |
+
isGenerateLyrics, isGenerateStyle, warning_msg],
|
| 300 |
api_name=False, show_api=False)
|
| 301 |
|
| 302 |
isGenerateLyrics.change(process_lang, inputs=[radio_lang, isGenerateLyrics, isGenerateStyle],
|
| 303 |
outputs=[log_text,name_input,text_input,style_output,song_output,generate_button,
|
| 304 |
+
isGenerateLyrics, isGenerateStyle, warning_msg],
|
| 305 |
api_name=False, show_api=False)
|
| 306 |
isGenerateStyle.change(process_lang, inputs=[radio_lang, isGenerateLyrics, isGenerateStyle],
|
| 307 |
outputs=[log_text,name_input,text_input,style_output,song_output,generate_button,
|
| 308 |
+
isGenerateLyrics, isGenerateStyle, warning_msg],
|
| 309 |
api_name=False, show_api=False)
|
| 310 |
|
| 311 |
demo.css = """
|