RASMUS commited on
Commit
2acad1e
1 Parent(s): a3d9404

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -28
app.py CHANGED
@@ -94,8 +94,7 @@ transcribe_options = dict(beam_size=3, best_of=3, without_timestamps=False)
94
 
95
 
96
  source_language_list = [key[0] for key in source_languages.items()]
97
- source_language_list_2 = [key[0] for key in DeepL_language_codes_for_translation.items()]
98
- translation_models_list = [key[0] for key in translation_models.items()]
99
 
100
 
101
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
@@ -193,7 +192,7 @@ def speech_to_text(video_file_path, selected_source_lang, whisper_model):
193
  raise RuntimeError("Error Running inference with local model", e)
194
 
195
 
196
- def translate_transcriptions(df, selected_translation_lang_2, selected_source_lang_2):
197
  if selected_translation_lang_2 is None:
198
  selected_translation_lang_2 = 'English'
199
  df.reset_index(inplace=True)
@@ -201,27 +200,27 @@ def translate_transcriptions(df, selected_translation_lang_2, selected_source_la
201
  print("start_translation")
202
  translations = []
203
 
204
- if selected_translation_lang_2 != selected_source_lang_2:
205
-
206
- text_combined = ""
207
- for i, sentence in enumerate(df['text']):
208
- if i == 0:
209
- text_combined = sentence
210
- else:
211
- text_combined = text_combined + '\n' + sentence
212
-
213
- data = {'text': text_combined,
214
- 'tag_spitting': 'xml',
215
- 'target_lang': DeepL_language_codes.get(selected_source_lang_2)
216
- }
217
- response = requests.post('https://api-free.deepl.com/v2/translate', headers=headers, data=data)
218
-
219
- # Print the response from the server
220
- translated_sentences = json.loads(response.text)
221
- translated_sentences = translated_sentences['translations'][0]['text'].split('\n')
222
- df['translation'] = translated_sentences
223
- else:
224
- df['translation'] = df['text']
225
  print("translations done")
226
 
227
  return (df)
@@ -289,10 +288,8 @@ video_out = gr.Video(label="Video Out", mirror_webcam=False)
289
 
290
  df_init = pd.DataFrame(columns=['start','end','text'])
291
  df_init_2 = pd.DataFrame(columns=['start','end','text','translation'])
292
- selected_translation_lang = gr.Dropdown(choices=translation_models_list, type="value", value="English", label="In which language you want the transcriptions?", interactive=True)
293
 
294
  selected_source_lang = gr.Dropdown(choices=source_language_list, type="value", value="Let the model analyze", label="Spoken language in video", interactive=True)
295
- selected_source_lang_2 = gr.Dropdown(choices=source_language_list_2, type="value", value="English", label="Spoken language in video", interactive=True)
296
  selected_translation_lang_2 = gr.Dropdown(choices=translation_models_list, type="value", value="English", label="In which language you want the transcriptions?", interactive=True)
297
  selected_whisper_model = gr.Dropdown(choices=whisper_models, type="value", value="base", label="Selected Whisper model", interactive=True)
298
 
@@ -366,10 +363,9 @@ with demo:
366
  ##### Here you will get translated transcriptions.
367
  ##### Please remember to select Spoken Language and wanted translation language
368
  ##### ''')
369
- selected_source_lang_2.render()
370
  selected_translation_lang_2.render()
371
  translate_transcriptions_button = gr.Button("Step 3. Translate transcription")
372
- translate_transcriptions_button.click(translate_transcriptions, [transcription_df, selected_translation_lang_2, selected_source_lang_2], transcription_and_translation_df)
373
  transcription_and_translation_df.render()
374
 
375
  with gr.Row():
 
94
 
95
 
96
  source_language_list = [key[0] for key in source_languages.items()]
97
+ translation_models_list = [key[0] for key in DeepL_language_codes_for_translation.items()]
 
98
 
99
 
100
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
 
192
  raise RuntimeError("Error Running inference with local model", e)
193
 
194
 
195
+ def translate_transcriptions(df, selected_translation_lang_2):
196
  if selected_translation_lang_2 is None:
197
  selected_translation_lang_2 = 'English'
198
  df.reset_index(inplace=True)
 
200
  print("start_translation")
201
  translations = []
202
 
203
+
204
+
205
+ text_combined = ""
206
+ for i, sentence in enumerate(df['text']):
207
+ if i == 0:
208
+ text_combined = sentence
209
+ else:
210
+ text_combined = text_combined + '\n' + sentence
211
+
212
+ data = {'text': text_combined,
213
+ 'tag_spitting': 'xml',
214
+ 'target_lang': DeepL_language_codes.get(selected_source_lang_2)
215
+ }
216
+ response = requests.post('https://api-free.deepl.com/v2/translate', headers=headers, data=data)
217
+
218
+ # Print the response from the server
219
+ translated_sentences = json.loads(response.text)
220
+ translated_sentences = translated_sentences['translations'][0]['text'].split('\n')
221
+ df['translation'] = translated_sentences
222
+
223
+
224
  print("translations done")
225
 
226
  return (df)
 
288
 
289
  df_init = pd.DataFrame(columns=['start','end','text'])
290
  df_init_2 = pd.DataFrame(columns=['start','end','text','translation'])
 
291
 
292
  selected_source_lang = gr.Dropdown(choices=source_language_list, type="value", value="Let the model analyze", label="Spoken language in video", interactive=True)
 
293
  selected_translation_lang_2 = gr.Dropdown(choices=translation_models_list, type="value", value="English", label="In which language you want the transcriptions?", interactive=True)
294
  selected_whisper_model = gr.Dropdown(choices=whisper_models, type="value", value="base", label="Selected Whisper model", interactive=True)
295
 
 
363
  ##### Here you will get translated transcriptions.
364
  ##### Please remember to select Spoken Language and wanted translation language
365
  ##### ''')
 
366
  selected_translation_lang_2.render()
367
  translate_transcriptions_button = gr.Button("Step 3. Translate transcription")
368
+ translate_transcriptions_button.click(translate_transcriptions, [transcription_df, selected_translation_lang_2], transcription_and_translation_df)
369
  transcription_and_translation_df.render()
370
 
371
  with gr.Row():