Spaces:
Running
on
Zero
Running
on
Zero
Update diffrhythm2/utils.py
Browse files- diffrhythm2/utils.py +18 -3
diffrhythm2/utils.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import torch
|
| 2 |
import torchaudio
|
| 3 |
import os
|
|
|
|
| 4 |
import json
|
| 5 |
import random
|
| 6 |
import io
|
|
@@ -103,17 +104,31 @@ def prepare_model(repo_id, device, dtype):
|
|
| 103 |
|
| 104 |
return diffrhythm2, mulan, lrc_tokenizer, decoder
|
| 105 |
|
|
|
|
|
|
|
| 106 |
def parse_lyrics(lrc_tokenizer, lyrics: str):
|
| 107 |
lyrics_with_time = []
|
| 108 |
lyrics = lyrics.split("\n")
|
|
|
|
| 109 |
for line in lyrics:
|
| 110 |
-
|
| 111 |
-
if
|
| 112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
else:
|
| 114 |
tokens = lrc_tokenizer.encode(line.strip())
|
| 115 |
tokens = tokens + [STRUCT_INFO['[stop]']]
|
| 116 |
lyrics_with_time.append(tokens)
|
|
|
|
|
|
|
| 117 |
return lyrics_with_time
|
| 118 |
|
| 119 |
@torch.no_grad()
|
|
|
|
| 1 |
import torch
|
| 2 |
import torchaudio
|
| 3 |
import os
|
| 4 |
+
import re
|
| 5 |
import json
|
| 6 |
import random
|
| 7 |
import io
|
|
|
|
| 104 |
|
| 105 |
return diffrhythm2, mulan, lrc_tokenizer, decoder
|
| 106 |
|
| 107 |
+
STRUCT_PATTERN = re.compile(r'^\[.*?\]$')
|
| 108 |
+
|
| 109 |
def parse_lyrics(lrc_tokenizer, lyrics: str):
|
| 110 |
lyrics_with_time = []
|
| 111 |
lyrics = lyrics.split("\n")
|
| 112 |
+
get_start = False
|
| 113 |
for line in lyrics:
|
| 114 |
+
line = line.strip()
|
| 115 |
+
if not line:
|
| 116 |
+
continue
|
| 117 |
+
struct_flag = STRUCT_PATTERN.match(line)
|
| 118 |
+
if struct_flag:
|
| 119 |
+
struct_idx = STRUCT_INFO.get(line.lower(), None)
|
| 120 |
+
if struct_idx is not None:
|
| 121 |
+
if struct_idx == STRUCT_INFO['[start]']:
|
| 122 |
+
get_start = True
|
| 123 |
+
lyrics_with_time.append([struct_idx, STRUCT_INFO['[stop]']])
|
| 124 |
+
else:
|
| 125 |
+
continue
|
| 126 |
else:
|
| 127 |
tokens = lrc_tokenizer.encode(line.strip())
|
| 128 |
tokens = tokens + [STRUCT_INFO['[stop]']]
|
| 129 |
lyrics_with_time.append(tokens)
|
| 130 |
+
if len(lyrics_with_time) != 0 and not get_start:
|
| 131 |
+
lyrics_with_time = [STRUCT_INFO['[start]'], STRUCT_INFO['[stop]']] + lyrics_with_time
|
| 132 |
return lyrics_with_time
|
| 133 |
|
| 134 |
@torch.no_grad()
|