kevinwang676 commited on
Commit
cfad9ad
·
1 Parent(s): eaa2833

Create parseinput.py

Browse files
Files changed (1) hide show
  1. parseinput.py +125 -0
parseinput.py ADDED
@@ -0,0 +1,125 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ joinedparts = joinedparts + f"\n<voice name=\"{selected_voice}\">{saxutils.escape(textpart)}</voice>"
95
+ ssml = f"""<?xml version="1.0"?>
96
+ <speak version="1.0" xmlns="http://www.w3.org/2001/10/synthesis"
97
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
98
+ xsi:schemaLocation="http://www.w3.org/2001/10/synthesis
99
+ http://www.w3.org/TR/speech-synthesis/synthesis.xsd"
100
+ xml:lang="en-US">
101
+ {joinedparts}
102
+ </speak>
103
+ """
104
+ return ssml
105
+
106
+ def create_clips_from_ssml(ssmlinput):
107
+ # Parse the XML
108
+ tree = ET.ElementTree(ET.fromstring(ssmlinput))
109
+ root = tree.getroot()
110
+
111
+ # Create an empty list
112
+ voice_list = []
113
+
114
+ # Loop through all voice tags
115
+ for voice in root.iter('{http://www.w3.org/2001/10/synthesis}voice'):
116
+ # Extract the voice name attribute and the content text
117
+ voice_name = voice.attrib['name']
118
+ voice_content = voice.text.strip() if voice.text else ''
119
+ if(len(voice_content) > 0):
120
+ parts = split_and_recombine_text(voice_content)
121
+ for p in parts:
122
+ # add to tuple list
123
+ voice_list.append((voice_name, p))
124
+
125
+ return voice_list