Spaces:
Build error
Build error
Commit
·
a9505b1
1
Parent(s):
7f6a2f3
Create parseinput.py
Browse files- parseinput.py +128 -0
parseinput.py
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|