Spaces:
Runtime error
Runtime error
tonic
commited on
Commit
·
6150c59
1
Parent(s):
3f36ef5
bug fix extractor, results
Browse files
app.py
CHANGED
@@ -106,7 +106,7 @@ class LongAudioProcessor:
|
|
106 |
class TaggedPhraseExtractor:
|
107 |
def __init__(self, text=''):
|
108 |
self.text = text
|
109 |
-
self.patterns =
|
110 |
|
111 |
def set_text(self, text):
|
112 |
"""Set the text to search within."""
|
@@ -119,7 +119,6 @@ class TaggedPhraseExtractor:
|
|
119 |
def extract_phrases(self):
|
120 |
"""Extract phrases for all colors and patterns added, including the three longest phrases."""
|
121 |
matches = {}
|
122 |
-
three_matches = {}
|
123 |
for color, pattern in self.patterns.items():
|
124 |
found_phrases = re.findall(pattern, self.text)
|
125 |
sorted_phrases = sorted(found_phrases, key=len, reverse=True)
|
@@ -244,27 +243,32 @@ def process_input(image=None, file=None, audio=None, text="", translateto = "Eng
|
|
244 |
|
245 |
audio_output = process_text_to_audio(processed_text, translateto, translateto)
|
246 |
extractor = TaggedPhraseExtractor(final_text)
|
247 |
-
extractor.add_pattern("red", patterns["red"])
|
248 |
-
extractor.add_pattern("blue", patterns["blue"])
|
249 |
-
extractor.add_pattern("green", patterns["green"])
|
250 |
matches = extractor.extract_phrases()
|
251 |
|
252 |
top_phrases = []
|
253 |
for color, phrases in matches.items():
|
254 |
top_phrases.extend(phrases)
|
255 |
|
|
|
|
|
|
|
256 |
audio_outputs = []
|
257 |
translations = []
|
258 |
for phrase in top_phrases:
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
|
|
|
|
|
|
|
|
264 |
|
265 |
return final_text, audio_output, top_phrases, translations, audio_outputs
|
266 |
|
267 |
|
|
|
268 |
# Define the inputs and outputs for the Gradio Interface
|
269 |
inputs = [
|
270 |
|
@@ -292,28 +296,20 @@ outputs = [
|
|
292 |
gr.Audio(label="Audio Output (Native Language) 3"),
|
293 |
gr.Audio(label="Audio Output (Target Language) 3")
|
294 |
]
|
|
|
295 |
def update_outputs(inputlanguage, target_language, audio, image, text, file):
|
296 |
-
|
297 |
image=image, file=file, audio=audio, text=text,
|
298 |
translateto=target_language, translatefrom=inputlanguage
|
299 |
)
|
300 |
-
|
301 |
-
|
302 |
-
|
303 |
-
|
304 |
-
|
305 |
-
|
306 |
-
|
307 |
-
|
308 |
-
|
309 |
-
return (
|
310 |
-
processed_text_output,
|
311 |
-
audio_output,
|
312 |
-
*audio_outputs_native,
|
313 |
-
*audio_outputs_target,
|
314 |
-
*longest_phrases_outputs,
|
315 |
-
*translated_phrases_outputs
|
316 |
-
)
|
317 |
|
318 |
def interface_func(inputlanguage, target_language, audio, image, text, file):
|
319 |
return update_outputs(inputlanguage, target_language, audio, image, text, file)
|
|
|
106 |
class TaggedPhraseExtractor:
|
107 |
def __init__(self, text=''):
|
108 |
self.text = text
|
109 |
+
self.patterns = patterns
|
110 |
|
111 |
def set_text(self, text):
|
112 |
"""Set the text to search within."""
|
|
|
119 |
def extract_phrases(self):
|
120 |
"""Extract phrases for all colors and patterns added, including the three longest phrases."""
|
121 |
matches = {}
|
|
|
122 |
for color, pattern in self.patterns.items():
|
123 |
found_phrases = re.findall(pattern, self.text)
|
124 |
sorted_phrases = sorted(found_phrases, key=len, reverse=True)
|
|
|
243 |
|
244 |
audio_output = process_text_to_audio(processed_text, translateto, translateto)
|
245 |
extractor = TaggedPhraseExtractor(final_text)
|
|
|
|
|
|
|
246 |
matches = extractor.extract_phrases()
|
247 |
|
248 |
top_phrases = []
|
249 |
for color, phrases in matches.items():
|
250 |
top_phrases.extend(phrases)
|
251 |
|
252 |
+
while len(top_phrases) < 3:
|
253 |
+
top_phrases.append("")
|
254 |
+
|
255 |
audio_outputs = []
|
256 |
translations = []
|
257 |
for phrase in top_phrases:
|
258 |
+
if phrase:
|
259 |
+
translated_phrase = translate_text(phrase, translatefrom=translatefrom, translateto=translateto)
|
260 |
+
translations.append(translated_phrase)
|
261 |
+
target_audio = process_text_to_audio(phrase, translatefrom=translateto, translateto=translateto)
|
262 |
+
native_audio = process_text_to_audio(translated_phrase, translatefrom=translatefrom, translateto=translatefrom)
|
263 |
+
audio_outputs.append((target_audio, native_audio))
|
264 |
+
else:
|
265 |
+
translations.append("")
|
266 |
+
audio_outputs.append(("", ""))
|
267 |
|
268 |
return final_text, audio_output, top_phrases, translations, audio_outputs
|
269 |
|
270 |
|
271 |
+
|
272 |
# Define the inputs and outputs for the Gradio Interface
|
273 |
inputs = [
|
274 |
|
|
|
296 |
gr.Audio(label="Audio Output (Native Language) 3"),
|
297 |
gr.Audio(label="Audio Output (Target Language) 3")
|
298 |
]
|
299 |
+
|
300 |
def update_outputs(inputlanguage, target_language, audio, image, text, file):
|
301 |
+
processed_text, audio_output, top_phrases, translations, audio_outputs = process_input(
|
302 |
image=image, file=file, audio=audio, text=text,
|
303 |
translateto=target_language, translatefrom=inputlanguage
|
304 |
)
|
305 |
+
output_tuple = (processed_text, audio_output)
|
306 |
+
for i in range(len(top_phrases)):
|
307 |
+
output_tuple += (top_phrases[i], translations[i], audio_outputs[i][0], audio_outputs[i][1])
|
308 |
+
|
309 |
+
while len(output_tuple) < 14:
|
310 |
+
output_tuple += ("", "", "", "")
|
311 |
+
|
312 |
+
return output_tuple
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
313 |
|
314 |
def interface_func(inputlanguage, target_language, audio, image, text, file):
|
315 |
return update_outputs(inputlanguage, target_language, audio, image, text, file)
|