Abdul-Ib commited on
Commit
cb53d09
1 Parent(s): 5bb0863

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -17
app.py CHANGED
@@ -20,28 +20,26 @@ def print_results(results):
20
  result_string += pprint.pformat(hit, indent=4) + "\n"
21
  return result_string.strip()
22
 
23
- async def translate_bulk_parallel(bulk: list) -> list:
24
  """
25
- Translate the given text to English in parallel and return the translated text.
26
 
27
  Args:
28
- - bulk (list): The list of texts to translate.
29
 
30
  Returns:
31
- - list: The translated texts.
32
  """
33
- translated_bulk = []
34
- with concurrent.futures.ThreadPoolExecutor() as executor:
35
- # Translate each text in parallel
36
- futures = [executor.submit(translator.translate, text, dest="en") for text in bulk]
37
- for future, original_text in zip(futures, bulk):
38
- try:
39
- translated_text = await future
40
- translated_text = translated_text.text.lower().strip()
41
- translated_bulk.append(translated_text)
42
- except Exception as e:
43
- print(f"Translation failed: {e}")
44
- translated_bulk.append(original_text.lower().strip()) # Add placeholder for failed translation
45
  return translated_bulk
46
 
47
  async def encode_document(document: str):
@@ -103,7 +101,7 @@ async def predict(query):
103
 
104
  try:
105
  translation_start_time = time.time()
106
- representation_list = await translate_bulk_parallel(tasks)
107
  except Exception as e:
108
  representation_list = tasks
109
  print(f"An error occurred while translating: {e}")
 
20
  result_string += pprint.pformat(hit, indent=4) + "\n"
21
  return result_string.strip()
22
 
23
+ async def translate_bulk(bulk: list) -> list:
24
  """
25
+ Translate the given text to English and return the translated text.
26
 
27
  Args:
28
+ - text (str): The text to translate.
29
 
30
  Returns:
31
+ - str: The translated text.
32
  """
33
+ try:
34
+ translated_bulk = await translator.translate(bulk, dest="en")
35
+ translated_bulk = [
36
+ translated_text.text.lower().strip() for translated_text in translated_bulk
37
+ ]
38
+ except Exception as e:
39
+ print(f"Bulk Translation failed: {e}")
40
+ translated_bulk = [
41
+ text.lower().strip() for text in bulk
42
+ ] # Use original text if translation fails
 
 
43
  return translated_bulk
44
 
45
  async def encode_document(document: str):
 
101
 
102
  try:
103
  translation_start_time = time.time()
104
+ representation_list = await translate_bulk(tasks)
105
  except Exception as e:
106
  representation_list = tasks
107
  print(f"An error occurred while translating: {e}")