qqwjq1981 commited on
Commit
a582ed6
·
verified ·
1 Parent(s): edf0e1c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -19
app.py CHANGED
@@ -26,7 +26,6 @@ import logging
26
  from textblob import TextBlob
27
  import whisper
28
  import time
29
- import sqlite3
30
 
31
  def silence(duration, fps=44100):
32
  """
@@ -71,6 +70,7 @@ css = """
71
  }
72
  """
73
 
 
74
  # Function to save feedback or provide access to the database file
75
  def handle_feedback(feedback):
76
  feedback = feedback.strip() # Clean up leading/trailing whitespace
@@ -153,30 +153,36 @@ def get_translation_model(source_language, target_language):
153
  # Return the model using string concatenation
154
  return f"Helsinki-NLP/opus-mt-{source_language}-{target_language}"
155
 
 
 
 
 
 
 
 
 
 
 
156
  def translate_text(transcription_json, source_language, target_language):
157
  # Load the translation model for the specified target language
158
  translation_model_id = get_translation_model(source_language, target_language)
159
  logger.debug(f"Translation model: {translation_model_id}")
160
  translator = pipeline("translation", model=translation_model_id)
161
 
162
- # Prepare output structure
163
- translated_json = []
164
-
165
- # Translate each sentence and store it with its start time
166
- for entry in transcription_json:
167
- original_text = entry["text"]
168
- translated_text = translator(original_text)[0]['translation_text']
169
- translated_json.append({
170
- "start": entry["start"],
171
- "original": original_text,
172
- "translated": translated_text,
173
- "end": entry["end"]
174
- })
175
- # Log the components being added to translated_json
176
- logger.debug("Adding to translated_json: start=%s, original=%s, translated=%s, end=%s",
177
- entry["start"], original_text, translated_text, entry["end"])
178
-
179
- # Return the translated timestamps as a JSON string
180
  return translated_json
181
 
182
  def update_translations(file, edited_table, mode):
 
26
  from textblob import TextBlob
27
  import whisper
28
  import time
 
29
 
30
  def silence(duration, fps=44100):
31
  """
 
70
  }
71
  """
72
 
73
+
74
  # Function to save feedback or provide access to the database file
75
  def handle_feedback(feedback):
76
  feedback = feedback.strip() # Clean up leading/trailing whitespace
 
153
  # Return the model using string concatenation
154
  return f"Helsinki-NLP/opus-mt-{source_language}-{target_language}"
155
 
156
+ def translate_single_entry(entry, translator):
157
+ original_text = entry["text"]
158
+ translated_text = translator(original_text)[0]['translation_text']
159
+ return {
160
+ "start": entry["start"],
161
+ "original": original_text,
162
+ "translated": translated_text,
163
+ "end": entry["end"]
164
+ }
165
+
166
  def translate_text(transcription_json, source_language, target_language):
167
  # Load the translation model for the specified target language
168
  translation_model_id = get_translation_model(source_language, target_language)
169
  logger.debug(f"Translation model: {translation_model_id}")
170
  translator = pipeline("translation", model=translation_model_id)
171
 
172
+ # Use ThreadPoolExecutor to parallelize translations
173
+ with concurrent.futures.ThreadPoolExecutor() as executor:
174
+ # Submit all translation tasks and collect results
175
+ translate_func = lambda entry: translate_single_entry(entry, translator)
176
+ translated_json = list(executor.map(translate_func, transcription_json))
177
+
178
+ # Sort the translated_json by start time
179
+ translated_json.sort(key=lambda x: x["start"])
180
+
181
+ # Log the components being added to translated_json
182
+ for entry in translated_json:
183
+ logger.debug("Added to translated_json: start=%s, original=%s, translated=%s, end=%s",
184
+ entry["start"], entry["original"], entry["translated"], entry["end"])
185
+
 
 
 
 
186
  return translated_json
187
 
188
  def update_translations(file, edited_table, mode):