Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -22,19 +22,33 @@ LANG_CODES = {
|
|
| 22 |
"Cebuano": "ceb_Latn"
|
| 23 |
}
|
| 24 |
|
| 25 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
def universal_translator(text, audio_path, source_lang_name, target_lang_name):
|
| 27 |
working_text = text
|
| 28 |
|
| 29 |
# If an audio file payload is sent (via UI or API), transcribe it first
|
| 30 |
if audio_path is not None:
|
| 31 |
-
print("Audio file detected. Transcribing via Whisper...")
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
working_text = whisper_result["text"].strip()
|
| 34 |
print(f"Transcription result: {working_text}")
|
| 35 |
|
| 36 |
if not working_text or not working_text.strip():
|
| 37 |
-
return "No text or audio input provided."
|
| 38 |
|
| 39 |
# Map friendly names to NLLB language codes
|
| 40 |
src_code = LANG_CODES.get(source_lang_name, "eng_Latn")
|
|
@@ -55,8 +69,10 @@ def universal_translator(text, audio_path, source_lang_name, target_lang_name):
|
|
| 55 |
temperature=0.1
|
| 56 |
)
|
| 57 |
|
| 58 |
-
translation = tokenizer.batch_decode(output_tokens, skip_special_tokens=True)[0]
|
| 59 |
-
|
|
|
|
|
|
|
| 60 |
|
| 61 |
# 3. Gradio Interface Layout
|
| 62 |
with gr.Blocks(theme=gr.themes.Base()) as demo:
|
|
@@ -72,13 +88,15 @@ with gr.Blocks(theme=gr.themes.Base()) as demo:
|
|
| 72 |
submit_btn = gr.Button("Process Translation", variant="primary")
|
| 73 |
|
| 74 |
with gr.Column():
|
|
|
|
|
|
|
| 75 |
output_text = gr.Textbox(label="Translation Result", lines=4, interactive=False)
|
| 76 |
|
| 77 |
-
#
|
| 78 |
submit_btn.click(
|
| 79 |
fn=universal_translator,
|
| 80 |
inputs=[text_in, audio_in, src_lang, tgt_lang],
|
| 81 |
-
outputs=output_text,
|
| 82 |
api_name="translate"
|
| 83 |
)
|
| 84 |
|
|
|
|
| 22 |
"Cebuano": "ceb_Latn"
|
| 23 |
}
|
| 24 |
|
| 25 |
+
# Whisper language ISO mapping to enforce clean audio extraction accuracy
|
| 26 |
+
WHISPER_LANG_MAPPING = {
|
| 27 |
+
"English": "en",
|
| 28 |
+
"Tagalog": "tl",
|
| 29 |
+
"Cebuano": "ceb"
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
# 2. Unified Processing Function
|
| 33 |
def universal_translator(text, audio_path, source_lang_name, target_lang_name):
|
| 34 |
working_text = text
|
| 35 |
|
| 36 |
# If an audio file payload is sent (via UI or API), transcribe it first
|
| 37 |
if audio_path is not None:
|
| 38 |
+
print(f"Audio file detected. Transcribing via Whisper ({source_lang_name})...")
|
| 39 |
+
whisper_lang = WHISPER_LANG_MAPPING.get(source_lang_name, None)
|
| 40 |
+
|
| 41 |
+
# Guide Whisper with the selected language code for better accuracy
|
| 42 |
+
if whisper_lang:
|
| 43 |
+
whisper_result = whisper_model.transcribe(audio_path, language=whisper_lang)
|
| 44 |
+
else:
|
| 45 |
+
whisper_result = whisper_model.transcribe(audio_path)
|
| 46 |
+
|
| 47 |
working_text = whisper_result["text"].strip()
|
| 48 |
print(f"Transcription result: {working_text}")
|
| 49 |
|
| 50 |
if not working_text or not working_text.strip():
|
| 51 |
+
return "No text or audio input provided.", "No text or audio input provided."
|
| 52 |
|
| 53 |
# Map friendly names to NLLB language codes
|
| 54 |
src_code = LANG_CODES.get(source_lang_name, "eng_Latn")
|
|
|
|
| 69 |
temperature=0.1
|
| 70 |
)
|
| 71 |
|
| 72 |
+
translation = tokenizer.batch_decode(output_tokens, skip_special_tokens=True)[0].strip()
|
| 73 |
+
|
| 74 |
+
# MODIFICATION: Return both the transcription text and translation text
|
| 75 |
+
return working_text, translation
|
| 76 |
|
| 77 |
# 3. Gradio Interface Layout
|
| 78 |
with gr.Blocks(theme=gr.themes.Base()) as demo:
|
|
|
|
| 88 |
submit_btn = gr.Button("Process Translation", variant="primary")
|
| 89 |
|
| 90 |
with gr.Column():
|
| 91 |
+
# MODIFICATION: Added text block interface container to capture the output transcript
|
| 92 |
+
transcription_out = gr.Textbox(label="Transcribed/Source Text", lines=3, interactive=False)
|
| 93 |
output_text = gr.Textbox(label="Translation Result", lines=4, interactive=False)
|
| 94 |
|
| 95 |
+
# MODIFICATION: Changed outputs array to map data cleanly to BOTH UI textboxes
|
| 96 |
submit_btn.click(
|
| 97 |
fn=universal_translator,
|
| 98 |
inputs=[text_in, audio_in, src_lang, tgt_lang],
|
| 99 |
+
outputs=[transcription_out, output_text],
|
| 100 |
api_name="translate"
|
| 101 |
)
|
| 102 |
|