Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -20,6 +20,70 @@ logging.basicConfig(
|
|
20 |
]
|
21 |
)
|
22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
# 언어 감지 및 모델 선택 함수
|
24 |
def detect_and_select_model(text):
|
25 |
if re.search(r'[\u3131-\u318E\uAC00-\uD7A3]', text): # 한글
|
@@ -33,26 +97,33 @@ def detect_and_select_model(text):
|
|
33 |
|
34 |
def optimize_model_selection(lyrics, genre):
|
35 |
model_path = detect_and_select_model(lyrics)
|
|
|
36 |
|
37 |
model_config = {
|
38 |
"m-a-p/YuE-s1-7B-anneal-en-cot": {
|
39 |
-
"max_tokens":
|
40 |
"temperature": 0.8,
|
41 |
-
"batch_size": 8
|
|
|
|
|
42 |
},
|
43 |
"m-a-p/YuE-s1-7B-anneal-jp-kr-cot": {
|
44 |
-
"max_tokens":
|
45 |
"temperature": 0.7,
|
46 |
-
"batch_size": 8
|
|
|
|
|
47 |
},
|
48 |
"m-a-p/YuE-s1-7B-anneal-zh-cot": {
|
49 |
-
"max_tokens":
|
50 |
"temperature": 0.7,
|
51 |
-
"batch_size": 8
|
|
|
|
|
52 |
}
|
53 |
}
|
54 |
|
55 |
-
return model_path, model_config[model_path]
|
56 |
|
57 |
# GPU 설정 최적화
|
58 |
def optimize_gpu_settings():
|
@@ -138,11 +209,22 @@ def get_last_mp3_file(output_dir):
|
|
138 |
mp3_files_with_path.sort(key=os.path.getmtime, reverse=True)
|
139 |
return mp3_files_with_path[0]
|
140 |
|
|
|
|
|
|
|
|
|
141 |
def infer(genre_txt_content, lyrics_txt_content, num_segments, max_new_tokens):
|
142 |
try:
|
143 |
# 모델 선택 및 설정
|
144 |
-
model_path, config = optimize_model_selection(lyrics_txt_content, genre_txt_content)
|
145 |
logging.info(f"Selected model: {model_path}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
146 |
|
147 |
# 임시 파일 생성
|
148 |
genre_txt_path = create_temp_file(genre_txt_content, prefix="genre_")
|
@@ -159,15 +241,16 @@ def infer(genre_txt_content, lyrics_txt_content, num_segments, max_new_tokens):
|
|
159 |
"--stage2_model", "m-a-p/YuE-s2-1B-general",
|
160 |
"--genre_txt", genre_txt_path,
|
161 |
"--lyrics_txt", lyrics_txt_path,
|
162 |
-
"--run_n_segments", str(
|
163 |
"--stage2_batch_size", str(config['batch_size']),
|
164 |
"--output_dir", output_dir,
|
165 |
"--cuda_idx", "0",
|
166 |
-
"--max_new_tokens", str(
|
167 |
"--temperature", str(config['temperature']),
|
168 |
"--disable_offload_model",
|
169 |
"--use_flash_attention_2",
|
170 |
-
"--bf16"
|
|
|
171 |
]
|
172 |
|
173 |
# CUDA 환경 변수 설정
|
@@ -228,31 +311,31 @@ with gr.Blocks() as demo:
|
|
228 |
)
|
229 |
lyrics_txt = gr.Textbox(
|
230 |
label="Lyrics (Supports English, Korean, Japanese, Chinese)",
|
231 |
-
placeholder="Enter song lyrics...",
|
232 |
lines=10
|
233 |
)
|
234 |
|
235 |
with gr.Column():
|
236 |
num_segments = gr.Number(
|
237 |
-
label="Number of Song Segments",
|
238 |
value=2,
|
239 |
minimum=1,
|
240 |
maximum=4,
|
241 |
step=1,
|
242 |
-
interactive=
|
243 |
)
|
244 |
max_new_tokens = gr.Slider(
|
245 |
-
label="Max New Tokens",
|
246 |
minimum=500,
|
247 |
maximum=32000,
|
248 |
step=500,
|
249 |
value=4000,
|
250 |
-
interactive=
|
251 |
)
|
252 |
submit_btn = gr.Button("Generate Music", variant="primary")
|
253 |
music_out = gr.Audio(label="Generated Audio")
|
254 |
|
255 |
-
# 다국어 예제
|
256 |
gr.Examples(
|
257 |
examples=[
|
258 |
# 영어 예제
|
@@ -264,6 +347,18 @@ Whispers of the night wind echo through the hall
|
|
264 |
Lost within the silence, I hear your gentle voice
|
265 |
Guiding me back homeward, making my heart rejoice
|
266 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
267 |
[chorus]
|
268 |
Don't let this moment fade, hold me close tonight
|
269 |
With you here beside me, everything's alright
|
@@ -285,38 +380,18 @@ Stay with me forever, let our love just flow
|
|
285 |
두려움은 없어 너와 함께라면
|
286 |
영원히 계속될 우리의 노래
|
287 |
이 순간을 기억해 forever
|
288 |
-
"""
|
289 |
-
],
|
290 |
-
# 일본어 예제
|
291 |
-
[
|
292 |
-
"J-pop melodic soft piano emotional",
|
293 |
-
"""[verse]
|
294 |
-
春の風に乗って
|
295 |
-
思い出が流れる
|
296 |
-
あの日の約束を
|
297 |
-
今でも覚えてる
|
298 |
|
299 |
-
[
|
300 |
-
|
301 |
-
|
302 |
-
|
303 |
-
|
304 |
-
"""
|
305 |
-
],
|
306 |
-
# 중국어 예제
|
307 |
-
[
|
308 |
-
"Chinese pop traditional fusion modern",
|
309 |
-
"""[verse]
|
310 |
-
晨光照亮天际
|
311 |
-
新的一天开始
|
312 |
-
追逐着梦想前进
|
313 |
-
不停歇的脚步
|
314 |
|
315 |
[chorus]
|
316 |
-
|
317 |
-
|
318 |
-
|
319 |
-
|
320 |
"""
|
321 |
]
|
322 |
],
|
@@ -341,4 +416,4 @@ demo.queue(concurrency_count=2).launch(
|
|
341 |
enable_queue=True,
|
342 |
show_api=True,
|
343 |
show_error=True
|
344 |
-
)
|
|
|
20 |
]
|
21 |
)
|
22 |
|
23 |
+
# 가사 분석 함수
|
24 |
+
def analyze_lyrics(lyrics):
|
25 |
+
# 줄 단위로 분리
|
26 |
+
lines = [line.strip() for line in lyrics.split('\n') if line.strip()]
|
27 |
+
|
28 |
+
# 섹션 카운트
|
29 |
+
sections = {
|
30 |
+
'verse': 0,
|
31 |
+
'chorus': 0,
|
32 |
+
'bridge': 0,
|
33 |
+
'total_lines': len(lines)
|
34 |
+
}
|
35 |
+
|
36 |
+
current_section = None
|
37 |
+
section_lines = {
|
38 |
+
'verse': 0,
|
39 |
+
'chorus': 0,
|
40 |
+
'bridge': 0
|
41 |
+
}
|
42 |
+
|
43 |
+
for line in lines:
|
44 |
+
lower_line = line.lower()
|
45 |
+
if '[verse]' in lower_line:
|
46 |
+
current_section = 'verse'
|
47 |
+
sections['verse'] += 1
|
48 |
+
elif '[chorus]' in lower_line:
|
49 |
+
current_section = 'chorus'
|
50 |
+
sections['chorus'] += 1
|
51 |
+
elif '[bridge]' in lower_line:
|
52 |
+
current_section = 'bridge'
|
53 |
+
sections['bridge'] += 1
|
54 |
+
elif current_section and line.strip():
|
55 |
+
section_lines[current_section] += 1
|
56 |
+
|
57 |
+
# 총 섹션 수 계산
|
58 |
+
total_sections = sections['verse'] + sections['chorus'] + sections['bridge']
|
59 |
+
|
60 |
+
return sections, total_sections, len(lines), section_lines
|
61 |
+
|
62 |
+
def calculate_generation_params(lyrics):
|
63 |
+
sections, total_sections, total_lines, section_lines = analyze_lyrics(lyrics)
|
64 |
+
|
65 |
+
# 기본 토큰 수 계산
|
66 |
+
base_tokens_per_line = 200
|
67 |
+
verse_tokens = section_lines['verse'] * base_tokens_per_line
|
68 |
+
chorus_tokens = section_lines['chorus'] * (base_tokens_per_line * 1.5) # 코러스는 50% 더 많은 토큰
|
69 |
+
bridge_tokens = section_lines['bridge'] * base_tokens_per_line
|
70 |
+
|
71 |
+
# 총 토큰 수 계산
|
72 |
+
total_tokens = int(verse_tokens + chorus_tokens + bridge_tokens)
|
73 |
+
|
74 |
+
# 섹션 기반 세그먼트 수 계산
|
75 |
+
num_segments = max(2, min(4, total_sections))
|
76 |
+
|
77 |
+
# 토큰 수 제한
|
78 |
+
max_tokens = min(32000, max(3000, total_tokens))
|
79 |
+
|
80 |
+
return {
|
81 |
+
'max_tokens': max_tokens,
|
82 |
+
'num_segments': num_segments,
|
83 |
+
'sections': sections,
|
84 |
+
'section_lines': section_lines
|
85 |
+
}
|
86 |
+
|
87 |
# 언어 감지 및 모델 선택 함수
|
88 |
def detect_and_select_model(text):
|
89 |
if re.search(r'[\u3131-\u318E\uAC00-\uD7A3]', text): # 한글
|
|
|
97 |
|
98 |
def optimize_model_selection(lyrics, genre):
|
99 |
model_path = detect_and_select_model(lyrics)
|
100 |
+
params = calculate_generation_params(lyrics)
|
101 |
|
102 |
model_config = {
|
103 |
"m-a-p/YuE-s1-7B-anneal-en-cot": {
|
104 |
+
"max_tokens": params['max_tokens'],
|
105 |
"temperature": 0.8,
|
106 |
+
"batch_size": 8,
|
107 |
+
"num_segments": params['num_segments'],
|
108 |
+
"chorus_strength": 1.2 if params['sections']['chorus'] > 0 else 1.0
|
109 |
},
|
110 |
"m-a-p/YuE-s1-7B-anneal-jp-kr-cot": {
|
111 |
+
"max_tokens": params['max_tokens'],
|
112 |
"temperature": 0.7,
|
113 |
+
"batch_size": 8,
|
114 |
+
"num_segments": params['num_segments'],
|
115 |
+
"chorus_strength": 1.2 if params['sections']['chorus'] > 0 else 1.0
|
116 |
},
|
117 |
"m-a-p/YuE-s1-7B-anneal-zh-cot": {
|
118 |
+
"max_tokens": params['max_tokens'],
|
119 |
"temperature": 0.7,
|
120 |
+
"batch_size": 8,
|
121 |
+
"num_segments": params['num_segments'],
|
122 |
+
"chorus_strength": 1.2 if params['sections']['chorus'] > 0 else 1.0
|
123 |
}
|
124 |
}
|
125 |
|
126 |
+
return model_path, model_config[model_path], params
|
127 |
|
128 |
# GPU 설정 최적화
|
129 |
def optimize_gpu_settings():
|
|
|
209 |
mp3_files_with_path.sort(key=os.path.getmtime, reverse=True)
|
210 |
return mp3_files_with_path[0]
|
211 |
|
212 |
+
|
213 |
+
|
214 |
+
|
215 |
+
|
216 |
def infer(genre_txt_content, lyrics_txt_content, num_segments, max_new_tokens):
|
217 |
try:
|
218 |
# 모델 선택 및 설정
|
219 |
+
model_path, config, params = optimize_model_selection(lyrics_txt_content, genre_txt_content)
|
220 |
logging.info(f"Selected model: {model_path}")
|
221 |
+
logging.info(f"Lyrics analysis: {params}")
|
222 |
+
|
223 |
+
# 실제 사용할 파라미터
|
224 |
+
actual_num_segments = config['num_segments']
|
225 |
+
actual_max_tokens = config['max_tokens']
|
226 |
+
|
227 |
+
logging.info(f"Using segments: {actual_num_segments}, tokens: {actual_max_tokens}")
|
228 |
|
229 |
# 임시 파일 생성
|
230 |
genre_txt_path = create_temp_file(genre_txt_content, prefix="genre_")
|
|
|
241 |
"--stage2_model", "m-a-p/YuE-s2-1B-general",
|
242 |
"--genre_txt", genre_txt_path,
|
243 |
"--lyrics_txt", lyrics_txt_path,
|
244 |
+
"--run_n_segments", str(actual_num_segments),
|
245 |
"--stage2_batch_size", str(config['batch_size']),
|
246 |
"--output_dir", output_dir,
|
247 |
"--cuda_idx", "0",
|
248 |
+
"--max_new_tokens", str(actual_max_tokens),
|
249 |
"--temperature", str(config['temperature']),
|
250 |
"--disable_offload_model",
|
251 |
"--use_flash_attention_2",
|
252 |
+
"--bf16",
|
253 |
+
"--chorus_strength", str(config['chorus_strength'])
|
254 |
]
|
255 |
|
256 |
# CUDA 환경 변수 설정
|
|
|
311 |
)
|
312 |
lyrics_txt = gr.Textbox(
|
313 |
label="Lyrics (Supports English, Korean, Japanese, Chinese)",
|
314 |
+
placeholder="Enter song lyrics with [verse], [chorus], [bridge] tags...",
|
315 |
lines=10
|
316 |
)
|
317 |
|
318 |
with gr.Column():
|
319 |
num_segments = gr.Number(
|
320 |
+
label="Number of Song Segments (Auto-adjusted based on lyrics)",
|
321 |
value=2,
|
322 |
minimum=1,
|
323 |
maximum=4,
|
324 |
step=1,
|
325 |
+
interactive=False
|
326 |
)
|
327 |
max_new_tokens = gr.Slider(
|
328 |
+
label="Max New Tokens (Auto-adjusted based on lyrics)",
|
329 |
minimum=500,
|
330 |
maximum=32000,
|
331 |
step=500,
|
332 |
value=4000,
|
333 |
+
interactive=False
|
334 |
)
|
335 |
submit_btn = gr.Button("Generate Music", variant="primary")
|
336 |
music_out = gr.Audio(label="Generated Audio")
|
337 |
|
338 |
+
# 다국어 예제
|
339 |
gr.Examples(
|
340 |
examples=[
|
341 |
# 영어 예제
|
|
|
347 |
Lost within the silence, I hear your gentle voice
|
348 |
Guiding me back homeward, making my heart rejoice
|
349 |
|
350 |
+
[chorus]
|
351 |
+
Don't let this moment fade, hold me close tonight
|
352 |
+
With you here beside me, everything's alright
|
353 |
+
Can't imagine life alone, don't want to let you go
|
354 |
+
Stay with me forever, let our love just flow
|
355 |
+
|
356 |
+
[verse]
|
357 |
+
Morning light is breaking, through the window pane
|
358 |
+
Memories of yesterday, like soft summer rain
|
359 |
+
In your arms I'm finding, all I'm dreaming of
|
360 |
+
Every day beside you, fills my heart with love
|
361 |
+
|
362 |
[chorus]
|
363 |
Don't let this moment fade, hold me close tonight
|
364 |
With you here beside me, everything's alright
|
|
|
380 |
두려움은 없어 너와 함께라면
|
381 |
영원히 계속될 우리의 노래
|
382 |
이 순간을 기억해 forever
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
383 |
|
384 |
+
[verse]
|
385 |
+
새로운 내일을 향해 나아가
|
386 |
+
우리만의 길을 만들어가
|
387 |
+
믿음으로 가득한 우리의 맘
|
388 |
+
절대 멈추지 않아 계속해서
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
389 |
|
390 |
[chorus]
|
391 |
+
달려가자 더 높이 더 멀리
|
392 |
+
두려움은 없어 너와 함께라면
|
393 |
+
영원히 계속될 우리의 노래
|
394 |
+
이 순간을 기억해 forever
|
395 |
"""
|
396 |
]
|
397 |
],
|
|
|
416 |
enable_queue=True,
|
417 |
show_api=True,
|
418 |
show_error=True
|
419 |
+
)
|