tonic commited on
Commit
0b2fb36
1 Parent(s): 971bee9

adding interface logic and audio returns

Browse files
__pycache__/lang_list.cpython-311.pyc ADDED
Binary file (5.61 kB). View file
 
app.py CHANGED
@@ -9,7 +9,7 @@ from lang_list import TEXT_SOURCE_LANGUAGE_NAMES , LANGUAGE_NAME_TO_CODE , text_
9
  from gradio_client import Client
10
  from dotenv import load_dotenv
11
  import requests
12
- from io import BytesIO
13
  import cohere
14
  import os
15
  import re
@@ -56,9 +56,7 @@ def get_language_code(language_name):
56
 
57
  def translate_text(text, instructions=translatetextinst):
58
  """
59
- - text (str): The initial text.
60
- Returns:
61
- - str: The translated text response.
62
  """
63
  prompt = f"{text}{instructions}"
64
  response = co.generate(
@@ -117,12 +115,8 @@ class TaggedPhraseExtractor:
117
  for color, pattern in self.patterns.items():
118
  found_phrases = re.findall(pattern, self.text)
119
  sorted_phrases = sorted(found_phrases, key=len, reverse=True)
120
- matches[color] = {
121
- 'all_phrases': found_phrases,
122
- 'top_three_longest': sorted_phrases[:3]
123
- }
124
- three_matches = sorted_phrases[:3]
125
- return matches , three_matches
126
 
127
  def print_phrases(self):
128
  """Extract phrases and print them, including the three longest phrases."""
@@ -236,21 +230,26 @@ def process_input(image=None, file=None, audio=None, text="", translateto = "Eng
236
  processed_text = response.generations[0].text
237
 
238
  audio_output = process_text_to_audio(processed_text, translateto, translateto)
 
 
 
 
 
239
 
240
- extractor = TaggedPhraseExtractor(processed_text)
241
- longest_phrases = extractor.get_longest_phrases()
 
242
 
243
- # Translate the longest phrases back into the native language
244
- translated_phrases = [translate_text(phrase, translateto, translatefrom) for phrase in longest_phrases]
245
-
246
- # Convert the original and translated phrases to audio
247
- audio_samples = {
248
- "target_language": [text_to_audio(phrase, translateto) for phrase in longest_phrases],
249
- "native_language": [text_to_audio(phrase, translatefrom) for phrase in translated_phrases]
250
- }
251
-
252
- return audio_output, processed_text, audio_samples, longest_phrases, translated_phrases
253
 
 
254
 
255
  def main():
256
  with gr.Blocks() as demo:
@@ -271,7 +270,7 @@ def main():
271
 
272
  process_button = gr.Button("🌟AyaTonic")
273
 
274
- processed_text_output = RichTextbox(label="Processed Text")
275
  longest_phrases_1 = gr.Textbox(label="Focus")
276
  translated_phrases_output_1 = gr.Textbox(label="Translated Phrases")
277
  audio_output_native_phrase_1 = gr.Audio(label="Audio Output (Native Language)")
@@ -286,14 +285,42 @@ def main():
286
  audio_output_target_phrase_3 = gr.Audio(label="Audio Output (Target Language)")
287
 
288
  def update_outputs(image, file, audio, text, input_language, target_language):
289
- processed_text, audio_samples, longest_phrases, translated_phrases = process_input(
290
- image, file, audio, text, input_language, target_language)
291
- return processed_text, audio_samples['native_language'], audio_samples['target_language'], "\n".join(longest_phrases), "\n".join(translated_phrases) # Fix this
292
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
293
  process_button.click(
294
  fn=update_outputs,
295
  inputs=[image_input, file_input, audio_input, text_input, input_language, target_language],
296
- outputs=[processed_text_output, audio_output_native_phrase_1, audio_output_target_phrase_1, longest_phrases_1, translated_phrases_output_1, audio_output_native_phrase_2, audio_output_target_phrase_2, longest_phrases_2, translated_phrases_output_2, audio_output_native_phrase_3, audio_output_target_phrase_3, longest_phrases_3, translated_phrases_output_3] #add education output
 
 
 
 
 
 
 
 
297
  )
298
 
299
  if __name__ == "__main__":
 
9
  from gradio_client import Client
10
  from dotenv import load_dotenv
11
  import requests
12
+ from io import BytesIO
13
  import cohere
14
  import os
15
  import re
 
56
 
57
  def translate_text(text, instructions=translatetextinst):
58
  """
59
+ translates text.
 
 
60
  """
61
  prompt = f"{text}{instructions}"
62
  response = co.generate(
 
115
  for color, pattern in self.patterns.items():
116
  found_phrases = re.findall(pattern, self.text)
117
  sorted_phrases = sorted(found_phrases, key=len, reverse=True)
118
+ matches[color] = sorted_phrases[:3]
119
+ return matches
 
 
 
 
120
 
121
  def print_phrases(self):
122
  """Extract phrases and print them, including the three longest phrases."""
 
230
  processed_text = response.generations[0].text
231
 
232
  audio_output = process_text_to_audio(processed_text, translateto, translateto)
233
+ extractor = TaggedPhraseExtractor(final_text)
234
+ extractor.add_pattern("red", patterns["red"])
235
+ extractor.add_pattern("blue", patterns["blue"])
236
+ extractor.add_pattern("green", patterns["green"])
237
+ matches = extractor.extract_phrases()
238
 
239
+ top_phrases = []
240
+ for color, phrases in matches.items():
241
+ top_phrases.extend(phrases)
242
 
243
+ audio_outputs = []
244
+ translations = []
245
+ for phrase in top_phrases:
246
+ translated_phrase = translate_text(phrase, translatefrom=translatefrom, translateto=translateto)
247
+ translations.append(translated_phrase)
248
+ target_audio = process_text_to_audio(phrase, translatefrom=translateto, translateto=translateto)
249
+ native_audio = process_text_to_audio(translated_phrase, translatefrom=translatefrom, translateto=translatefrom)
250
+ audio_outputs.append((target_audio, native_audio))
 
 
251
 
252
+ return final_text, audio_output, top_phrases, translations, audio_outputs
253
 
254
  def main():
255
  with gr.Blocks() as demo:
 
270
 
271
  process_button = gr.Button("🌟AyaTonic")
272
 
273
+ processed_text_output = gr.RichTextbox(label="Processed Text")
274
  longest_phrases_1 = gr.Textbox(label="Focus")
275
  translated_phrases_output_1 = gr.Textbox(label="Translated Phrases")
276
  audio_output_native_phrase_1 = gr.Audio(label="Audio Output (Native Language)")
 
285
  audio_output_target_phrase_3 = gr.Audio(label="Audio Output (Target Language)")
286
 
287
  def update_outputs(image, file, audio, text, input_language, target_language):
288
+ final_text, top_phrases, translations, audio_outputs = process_input(
289
+ image=image, file=file, audio=audio, text=text,
290
+ translatefrom=input_language, translateto=target_language
291
+ )
292
+
293
+ # Prepare outputs for Gradio
294
+ processed_text_output = final_text
295
+ audio_output_native_phrases = [native for _, native in audio_outputs]
296
+ audio_output_target_phrases = [target for target, _ in audio_outputs]
297
+
298
+ # Assuming there are exactly 3 top phrases for simplicity
299
+ longest_phrases_outputs = top_phrases[:3]
300
+ translated_phrases_outputs = translations[:3]
301
+ audio_outputs_native = audio_output_native_phrases[:3]
302
+ audio_outputs_target = audio_output_target_phrases[:3]
303
+
304
+ return (
305
+ processed_text_output,
306
+ *audio_outputs_native,
307
+ *audio_outputs_target,
308
+ *longest_phrases_outputs,
309
+ *translated_phrases_outputs
310
+ )
311
+
312
  process_button.click(
313
  fn=update_outputs,
314
  inputs=[image_input, file_input, audio_input, text_input, input_language, target_language],
315
+ outputs=[
316
+ processed_text_output,
317
+ audio_output_native_phrase_1, audio_output_target_phrase_1,
318
+ audio_output_native_phrase_2, audio_output_target_phrase_2,
319
+ audio_output_native_phrase_3, audio_output_target_phrase_3,
320
+ longest_phrases_1, translated_phrases_output_1,
321
+ longest_phrases_2, translated_phrases_output_2,
322
+ longest_phrases_3, translated_phrases_output_3
323
+ ]
324
  )
325
 
326
  if __name__ == "__main__":
ayatonic.env DELETED
@@ -1,2 +0,0 @@
1
- CO_API_KEY=KQBPf0H0ENZESIC5nuUJ4i4jjg34xMPAkYK7s31W
2
- SEAMLESSM4T=https://facebook-seamless-m4t-v2-large.hf.space/--replicas/v4gsf/
 
 
 
requirements.txt CHANGED
@@ -1,11 +1,11 @@
1
  gradio
2
  gradio_rich_textbox
3
  gradio-client
4
- cohere
5
- surya-ocr
6
- pillow
7
  torchvision
8
  torch
9
  python-dotenv
10
  pandas
11
- pydub
 
 
 
 
1
  gradio
2
  gradio_rich_textbox
3
  gradio-client
 
 
 
4
  torchvision
5
  torch
6
  python-dotenv
7
  pandas
8
+ pydub
9
+ cohere
10
+ surya-ocr
11
+ pillow