Spaces:
Runtime error
Runtime error
kevinwang676
commited on
Commit
•
580d62a
1
Parent(s):
134e14e
Upload 4 files
Browse files- util/__init__.py +0 -0
- util/helper.py +35 -0
- util/parseinput.py +129 -0
- util/settings.py +41 -0
util/__init__.py
ADDED
File without changes
|
util/helper.py
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from datetime import datetime
|
3 |
+
from mutagen.wave import WAVE
|
4 |
+
from mutagen.id3._frames import *
|
5 |
+
|
6 |
+
def create_filename(path, seed, name, extension):
|
7 |
+
now = datetime.now()
|
8 |
+
date_str =now.strftime("%m-%d-%Y")
|
9 |
+
outputs_folder = os.path.join(os.getcwd(), path)
|
10 |
+
if not os.path.exists(outputs_folder):
|
11 |
+
os.makedirs(outputs_folder)
|
12 |
+
|
13 |
+
sub_folder = os.path.join(outputs_folder, date_str)
|
14 |
+
if not os.path.exists(sub_folder):
|
15 |
+
os.makedirs(sub_folder)
|
16 |
+
|
17 |
+
time_str = now.strftime("%H-%M-%S")
|
18 |
+
if seed == None:
|
19 |
+
file_name = f"{name}_{time_str}{extension}"
|
20 |
+
else:
|
21 |
+
file_name = f"{name}_{time_str}_s{seed}{extension}"
|
22 |
+
return os.path.join(sub_folder, file_name)
|
23 |
+
|
24 |
+
|
25 |
+
def add_id3_tag(filename, text, speakername, seed):
|
26 |
+
audio = WAVE(filename)
|
27 |
+
if speakername == None:
|
28 |
+
speakername = "Unconditional"
|
29 |
+
|
30 |
+
# write id3 tag with text truncated to 60 chars, as a precaution...
|
31 |
+
audio["TIT2"] = TIT2(encoding=3, text=text[:60])
|
32 |
+
audio["TPE1"] = TPE1(encoding=3, text=f"Voice {speakername} using Seed={seed}")
|
33 |
+
audio["TPUB"] = TPUB(encoding=3, text="Bark by Suno AI")
|
34 |
+
audio["COMMENT"] = COMM(encoding=3, text="Generated with Bark GUI - Text-Prompted Generative Audio Model. Visit https://github.com/C0untFloyd/bark-gui")
|
35 |
+
audio.save()
|
util/parseinput.py
ADDED
@@ -0,0 +1,129 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import re
|
2 |
+
import xml.etree.ElementTree as ET
|
3 |
+
from xml.sax import saxutils
|
4 |
+
#import nltk
|
5 |
+
|
6 |
+
# Chunked generation originally from https://github.com/serp-ai/bark-with-voice-clone
|
7 |
+
def split_and_recombine_text(text, desired_length=100, max_length=150):
|
8 |
+
# return nltk.sent_tokenize(text)
|
9 |
+
|
10 |
+
# from https://github.com/neonbjb/tortoise-tts
|
11 |
+
"""Split text it into chunks of a desired length trying to keep sentences intact."""
|
12 |
+
# normalize text, remove redundant whitespace and convert non-ascii quotes to ascii
|
13 |
+
text = re.sub(r"\n\n+", "\n", text)
|
14 |
+
text = re.sub(r"\s+", " ", text)
|
15 |
+
text = re.sub(r"[“”]", '"', text)
|
16 |
+
|
17 |
+
rv = []
|
18 |
+
in_quote = False
|
19 |
+
current = ""
|
20 |
+
split_pos = []
|
21 |
+
pos = -1
|
22 |
+
end_pos = len(text) - 1
|
23 |
+
|
24 |
+
def seek(delta):
|
25 |
+
nonlocal pos, in_quote, current
|
26 |
+
is_neg = delta < 0
|
27 |
+
for _ in range(abs(delta)):
|
28 |
+
if is_neg:
|
29 |
+
pos -= 1
|
30 |
+
current = current[:-1]
|
31 |
+
else:
|
32 |
+
pos += 1
|
33 |
+
current += text[pos]
|
34 |
+
if text[pos] == '"':
|
35 |
+
in_quote = not in_quote
|
36 |
+
return text[pos]
|
37 |
+
|
38 |
+
def peek(delta):
|
39 |
+
p = pos + delta
|
40 |
+
return text[p] if p < end_pos and p >= 0 else ""
|
41 |
+
|
42 |
+
def commit():
|
43 |
+
nonlocal rv, current, split_pos
|
44 |
+
rv.append(current)
|
45 |
+
current = ""
|
46 |
+
split_pos = []
|
47 |
+
|
48 |
+
while pos < end_pos:
|
49 |
+
c = seek(1)
|
50 |
+
# do we need to force a split?
|
51 |
+
if len(current) >= max_length:
|
52 |
+
if len(split_pos) > 0 and len(current) > (desired_length / 2):
|
53 |
+
# we have at least one sentence and we are over half the desired length, seek back to the last split
|
54 |
+
d = pos - split_pos[-1]
|
55 |
+
seek(-d)
|
56 |
+
else:
|
57 |
+
# no full sentences, seek back until we are not in the middle of a word and split there
|
58 |
+
while c not in "!?.,\n " and pos > 0 and len(current) > desired_length:
|
59 |
+
c = seek(-1)
|
60 |
+
commit()
|
61 |
+
# check for sentence boundaries
|
62 |
+
elif not in_quote and (c in "!?]\n" or (c == "." and peek(1) in "\n ")):
|
63 |
+
# seek forward if we have consecutive boundary markers but still within the max length
|
64 |
+
while (
|
65 |
+
pos < len(text) - 1 and len(current) < max_length and peek(1) in "!?.]"
|
66 |
+
):
|
67 |
+
c = seek(1)
|
68 |
+
split_pos.append(pos)
|
69 |
+
if len(current) >= desired_length:
|
70 |
+
commit()
|
71 |
+
# treat end of quote as a boundary if its followed by a space or newline
|
72 |
+
elif in_quote and peek(1) == '"' and peek(2) in "\n ":
|
73 |
+
seek(2)
|
74 |
+
split_pos.append(pos)
|
75 |
+
rv.append(current)
|
76 |
+
|
77 |
+
# clean up, remove lines with only whitespace or punctuation
|
78 |
+
rv = [s.strip() for s in rv]
|
79 |
+
rv = [s for s in rv if len(s) > 0 and not re.match(r"^[\s\.,;:!?]*$", s)]
|
80 |
+
|
81 |
+
return rv
|
82 |
+
|
83 |
+
def is_ssml(value):
|
84 |
+
try:
|
85 |
+
ET.fromstring(value)
|
86 |
+
except ET.ParseError:
|
87 |
+
return False
|
88 |
+
return True
|
89 |
+
|
90 |
+
def build_ssml(rawtext, selected_voice):
|
91 |
+
texts = rawtext.split("\n")
|
92 |
+
joinedparts = ""
|
93 |
+
for textpart in texts:
|
94 |
+
textpart = textpart.strip()
|
95 |
+
if len(textpart) < 1:
|
96 |
+
continue
|
97 |
+
joinedparts = joinedparts + f"\n<voice name=\"{selected_voice}\">{saxutils.escape(textpart)}</voice>"
|
98 |
+
ssml = f"""<?xml version="1.0"?>
|
99 |
+
<speak version="1.0" xmlns="http://www.w3.org/2001/10/synthesis"
|
100 |
+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
101 |
+
xsi:schemaLocation="http://www.w3.org/2001/10/synthesis
|
102 |
+
http://www.w3.org/TR/speech-synthesis/synthesis.xsd"
|
103 |
+
xml:lang="en-US">
|
104 |
+
{joinedparts}
|
105 |
+
</speak>
|
106 |
+
"""
|
107 |
+
return ssml
|
108 |
+
|
109 |
+
def create_clips_from_ssml(ssmlinput):
|
110 |
+
# Parse the XML
|
111 |
+
tree = ET.ElementTree(ET.fromstring(ssmlinput))
|
112 |
+
root = tree.getroot()
|
113 |
+
|
114 |
+
# Create an empty list
|
115 |
+
voice_list = []
|
116 |
+
|
117 |
+
# Loop through all voice tags
|
118 |
+
for voice in root.iter('{http://www.w3.org/2001/10/synthesis}voice'):
|
119 |
+
# Extract the voice name attribute and the content text
|
120 |
+
voice_name = voice.attrib['name']
|
121 |
+
voice_content = voice.text.strip() if voice.text else ''
|
122 |
+
if(len(voice_content) > 0):
|
123 |
+
parts = split_and_recombine_text(voice_content)
|
124 |
+
for p in parts:
|
125 |
+
if(len(p) > 1):
|
126 |
+
# add to tuple list
|
127 |
+
voice_list.append((voice_name, p))
|
128 |
+
return voice_list
|
129 |
+
|
util/settings.py
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import yaml
|
2 |
+
|
3 |
+
class Settings:
|
4 |
+
def __init__(self, config_file):
|
5 |
+
self.config_file = config_file
|
6 |
+
self.load()
|
7 |
+
|
8 |
+
def load(self):
|
9 |
+
try:
|
10 |
+
with open(self.config_file, 'r') as f:
|
11 |
+
data = yaml.load(f, Loader=yaml.FullLoader)
|
12 |
+
self.selected_theme = data.get('selected_theme', "gstaff/xkcd")
|
13 |
+
self.server_name = data.get('server_name', "")
|
14 |
+
self.server_port = data.get('server_port', 0)
|
15 |
+
self.server_share = data.get('server_share', False)
|
16 |
+
self.input_text_desired_length = data.get('input_text_desired_length', 110)
|
17 |
+
self.input_text_max_length = data.get('input_text_max_length', 170)
|
18 |
+
self.silence_sentence = data.get('silence_between_sentences', 250)
|
19 |
+
self.silence_speakers = data.get('silence_between_speakers', 500)
|
20 |
+
self.output_folder_path = data.get('output_folder_path', 'outputs')
|
21 |
+
|
22 |
+
except:
|
23 |
+
self.selected_theme = "gstaff/xkcd"
|
24 |
+
|
25 |
+
def save(self):
|
26 |
+
data = {
|
27 |
+
'selected_theme': self.selected_theme,
|
28 |
+
'server_name': self.server_name,
|
29 |
+
'server_port': self.server_port,
|
30 |
+
'server_share': self.server_share,
|
31 |
+
'input_text_desired_length' : self.input_text_desired_length,
|
32 |
+
'input_text_max_length' : self.input_text_max_length,
|
33 |
+
'silence_between_sentences': self.silence_sentence,
|
34 |
+
'silence_between_speakers': self.silence_speakers,
|
35 |
+
'output_folder_path': self.output_folder_path
|
36 |
+
}
|
37 |
+
with open(self.config_file, 'w') as f:
|
38 |
+
yaml.dump(data, f)
|
39 |
+
|
40 |
+
|
41 |
+
|