jerrypan7 commited on
Commit
df13cc5
·
verified ·
1 Parent(s): 47a5d48

Update app.py

Browse files

segment the translation

Files changed (1) hide show
  1. app.py +25 -3
app.py CHANGED
@@ -96,6 +96,23 @@ def download_youtube_audio(youtube_url: str, output_dir: Optional[str] = None) -
96
  print("Error:", response.status_code, response.text)
97
  return None # Return None on failure
98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99
  def inference_via_llm_api(input_text, min_new_tokens=2, max_new_tokens=64):
100
  print(input_text)
101
  one_vllm_input = f"<|im_start|>system\nYou are a translation expert.<|im_end|>\n<|im_start|>user\n{input_text}<|im_end|>\n<|im_start|>assistant"
@@ -142,10 +159,15 @@ def transcribe_and_speak(audio, source_lang, target_lang, youtube_url=None, targ
142
  else:
143
  return "ASR failed", None, None
144
 
145
- translation_prompt = f"Translate the following text from {LANGUAGE_MAP[source_lang]} to {LANGUAGE_MAP[target_lang]}: {transcription}"
146
- translated_text = inference_via_llm_api(translation_prompt)
147
- print(f"Translation: {translated_text}")
148
 
 
 
 
 
 
 
 
 
149
  # TTS
150
  tts_params = {
151
  'language': target_lang,
 
96
  print("Error:", response.status_code, response.text)
97
  return None # Return None on failure
98
 
99
+
100
+ punctuation_marks = r'([\.!?!?。])'
101
+
102
+ def split_text_with_punctuation(text):
103
+ # Split the text using the punctuation marks, keeping the punctuation marks
104
+ split_text = re.split(punctuation_marks, text)
105
+ # Combine each punctuation mark with the preceding segment
106
+ combined_segments = []
107
+ for i in range(0, len(split_text) - 1, 2):
108
+ combined_segments.append(split_text[i] + split_text[i + 1])
109
+ # If there's any remaining text after the last punctuation, append it as well
110
+ if len(split_text) % 2 != 0:
111
+ combined_segments.append(split_text[-1])
112
+
113
+ return combined_segments
114
+
115
+
116
  def inference_via_llm_api(input_text, min_new_tokens=2, max_new_tokens=64):
117
  print(input_text)
118
  one_vllm_input = f"<|im_start|>system\nYou are a translation expert.<|im_end|>\n<|im_start|>user\n{input_text}<|im_end|>\n<|im_start|>assistant"
 
159
  else:
160
  return "ASR failed", None, None
161
 
 
 
 
162
 
163
+ split_result = split_text_with_punctuation(transcription)
164
+ translate_segments=[]
165
+ for segment in split_result:
166
+ translation_prompt = f"Translate the following text from {LANGUAGE_MAP[source_lang]} to {LANGUAGE_MAP[target_lang]}: {segment}"
167
+ translated_seg_txt = inference_via_llm_api(translation_prompt)
168
+ translate_segments.append(translated_seg_txt)
169
+ print(f"Translation: {translated_seg_txt}")
170
+ translated_text = " ".join(translate_segments)
171
  # TTS
172
  tts_params = {
173
  'language': target_lang,