Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -162,6 +162,44 @@ def calculate_generation_params(lyrics):
|
|
162 |
'has_chorus': sections['chorus'] > 0
|
163 |
}
|
164 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
165 |
def detect_and_select_model(text):
|
166 |
if re.search(r'[\u3131-\u318E\uAC00-\uD7A3]', text):
|
167 |
return "m-a-p/YuE-s1-7B-anneal-jp-kr-cot"
|
@@ -238,42 +276,7 @@ def initialize_system():
|
|
238 |
def get_cached_file_path(content_hash, prefix):
|
239 |
return create_temp_file(content_hash, prefix)
|
240 |
|
241 |
-
def empty_output_folder(output_dir):
|
242 |
-
try:
|
243 |
-
shutil.rmtree(output_dir)
|
244 |
-
os.makedirs(output_dir)
|
245 |
-
logging.info(f"Output folder cleaned: {output_dir}")
|
246 |
-
except Exception as e:
|
247 |
-
logging.error(f"Error cleaning output folder: {e}")
|
248 |
-
raise
|
249 |
|
250 |
-
def create_temp_file(content, prefix, suffix=".txt"):
|
251 |
-
temp_file = tempfile.NamedTemporaryFile(delete=False, mode="w", prefix=prefix, suffix=suffix)
|
252 |
-
content = content.strip() + "\n\n"
|
253 |
-
content = content.replace("\r\n", "\n").replace("\r", "\n")
|
254 |
-
temp_file.write(content)
|
255 |
-
temp_file.close()
|
256 |
-
logging.debug(f"Temporary file created: {temp_file.name}")
|
257 |
-
return temp_file.name
|
258 |
-
|
259 |
-
def get_last_mp3_file(output_dir):
|
260 |
-
mp3_files = [f for f in os.listdir(output_dir) if f.endswith('.mp3')]
|
261 |
-
if not mp3_files:
|
262 |
-
logging.warning("No MP3 files found")
|
263 |
-
return None
|
264 |
-
|
265 |
-
mp3_files_with_path = [os.path.join(output_dir, f) for f in mp3_files]
|
266 |
-
mp3_files_with_path.sort(key=os.path.getmtime, reverse=True)
|
267 |
-
return mp3_files_with_path[0]
|
268 |
-
|
269 |
-
def get_audio_duration(file_path):
|
270 |
-
try:
|
271 |
-
import librosa
|
272 |
-
duration = librosa.get_duration(path=file_path)
|
273 |
-
return duration
|
274 |
-
except Exception as e:
|
275 |
-
logging.error(f"Failed to get audio duration: {e}")
|
276 |
-
return None
|
277 |
|
278 |
def optimize_model_selection(lyrics, genre):
|
279 |
model_path = detect_and_select_model(lyrics)
|
@@ -316,7 +319,15 @@ def infer(genre_txt_content, lyrics_txt_content, num_segments, max_new_tokens):
|
|
316 |
lyrics_txt_path = None
|
317 |
|
318 |
try:
|
319 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
320 |
logging.info(f"Selected model: {model_path}")
|
321 |
logging.info(f"Lyrics analysis: {params}")
|
322 |
|
@@ -336,7 +347,8 @@ def infer(genre_txt_content, lyrics_txt_content, num_segments, max_new_tokens):
|
|
336 |
logging.info(f"Using segments: {actual_num_segments}, tokens: {actual_max_tokens}")
|
337 |
|
338 |
genre_txt_path = create_temp_file(genre_txt_content, prefix="genre_")
|
339 |
-
|
|
|
340 |
|
341 |
output_dir = "./output"
|
342 |
os.makedirs(output_dir, exist_ok=True)
|
|
|
162 |
'has_chorus': sections['chorus'] > 0
|
163 |
}
|
164 |
|
165 |
+
def create_temp_file(content, prefix, suffix=".txt"):
|
166 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, mode="w", prefix=prefix, suffix=suffix)
|
167 |
+
content = content.strip() + "\n\n"
|
168 |
+
content = content.replace("\r\n", "\n").replace("\r", "\n")
|
169 |
+
temp_file.write(content)
|
170 |
+
temp_file.close()
|
171 |
+
logging.debug(f"Temporary file created: {temp_file.name}")
|
172 |
+
return temp_file.name
|
173 |
+
|
174 |
+
def empty_output_folder(output_dir):
|
175 |
+
try:
|
176 |
+
shutil.rmtree(output_dir)
|
177 |
+
os.makedirs(output_dir)
|
178 |
+
logging.info(f"Output folder cleaned: {output_dir}")
|
179 |
+
except Exception as e:
|
180 |
+
logging.error(f"Error cleaning output folder: {e}")
|
181 |
+
raise
|
182 |
+
|
183 |
+
def get_last_mp3_file(output_dir):
|
184 |
+
mp3_files = [f for f in os.listdir(output_dir) if f.endswith('.mp3')]
|
185 |
+
if not mp3_files:
|
186 |
+
logging.warning("No MP3 files found")
|
187 |
+
return None
|
188 |
+
|
189 |
+
mp3_files_with_path = [os.path.join(output_dir, f) for f in mp3_files]
|
190 |
+
mp3_files_with_path.sort(key=os.path.getmtime, reverse=True)
|
191 |
+
return mp3_files_with_path[0]
|
192 |
+
|
193 |
+
def get_audio_duration(file_path):
|
194 |
+
try:
|
195 |
+
import librosa
|
196 |
+
duration = librosa.get_duration(path=file_path)
|
197 |
+
return duration
|
198 |
+
except Exception as e:
|
199 |
+
logging.error(f"Failed to get audio duration: {e}")
|
200 |
+
return None
|
201 |
+
|
202 |
+
|
203 |
def detect_and_select_model(text):
|
204 |
if re.search(r'[\u3131-\u318E\uAC00-\uD7A3]', text):
|
205 |
return "m-a-p/YuE-s1-7B-anneal-jp-kr-cot"
|
|
|
276 |
def get_cached_file_path(content_hash, prefix):
|
277 |
return create_temp_file(content_hash, prefix)
|
278 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
279 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
280 |
|
281 |
def optimize_model_selection(lyrics, genre):
|
282 |
model_path = detect_and_select_model(lyrics)
|
|
|
319 |
lyrics_txt_path = None
|
320 |
|
321 |
try:
|
322 |
+
# ---- (1) ํ๋ฉด์๋ ๋ณด์ด์ง ์์ง๋ง, ๋ง์ง๋ง์ [chorus] bye ์ฝ์
----
|
323 |
+
forced_line = "[chorus] bye"
|
324 |
+
tmp_lyrics = lyrics_txt_content.strip()
|
325 |
+
# ์ด๋ฏธ 'bye'๊ฐ ๋ค์ด์๋์ง ํ์ธ (์ํ๋ค๋ฉด ์กฐ๊ฑด ์ถ๊ฐ/์ญ์ ๊ฐ๋ฅ)
|
326 |
+
if forced_line.lower() not in tmp_lyrics.lower():
|
327 |
+
tmp_lyrics += "\n" + forced_line
|
328 |
+
|
329 |
+
# ---- (2) ๊ฐ์ ์ฝ์
๋ tmp_lyrics๋ฅผ ํตํด ๋ชจ๋ธ ์ต์ ํ/์ค์ ----
|
330 |
+
model_path, config, params = optimize_model_selection(tmp_lyrics, genre_txt_content)
|
331 |
logging.info(f"Selected model: {model_path}")
|
332 |
logging.info(f"Lyrics analysis: {params}")
|
333 |
|
|
|
347 |
logging.info(f"Using segments: {actual_num_segments}, tokens: {actual_max_tokens}")
|
348 |
|
349 |
genre_txt_path = create_temp_file(genre_txt_content, prefix="genre_")
|
350 |
+
# tmp_lyrics(๊ฐ์ ์ถ๊ฐ๋ ๋ฌธ์์ด)์ ์์ ํ์ผ๋ก ์ ์ฅ
|
351 |
+
lyrics_txt_path = create_temp_file(tmp_lyrics, prefix="lyrics_")
|
352 |
|
353 |
output_dir = "./output"
|
354 |
os.makedirs(output_dir, exist_ok=True)
|