Spaces:
Sleeping
Sleeping
Solve the error on the place holder
Browse files
app.py
CHANGED
|
@@ -3,16 +3,15 @@ import torch
|
|
| 3 |
import whisper
|
| 4 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 5 |
|
| 6 |
-
# 1. Initialization and Model Loading
|
| 7 |
print("Initializing Hugging Face Space environment...")
|
| 8 |
|
| 9 |
-
device = "cpu"
|
| 10 |
|
| 11 |
print("Loading Whisper STT Model...")
|
| 12 |
whisper_model = whisper.load_model("small", device=device)
|
| 13 |
|
| 14 |
-
|
| 15 |
-
model_path = "Akariieieie/fine_tuned_dialectgov4" # <-- REPLACE WITH YOUR ORG & MODEL REPO
|
| 16 |
print(f"Loading custom NLLB model from {model_path}...")
|
| 17 |
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
| 18 |
model = AutoModelForSeq2SeqLM.from_pretrained(model_path).to(device)
|
|
@@ -23,23 +22,26 @@ LANG_CODES = {
|
|
| 23 |
"Cebuano": "ceb_Latn"
|
| 24 |
}
|
| 25 |
|
| 26 |
-
# Whisper
|
| 27 |
WHISPER_LANG_MAPPING = {
|
| 28 |
"English": "en",
|
| 29 |
"Tagalog": "tl",
|
| 30 |
-
"Cebuano":
|
| 31 |
}
|
| 32 |
|
| 33 |
# 2. Unified Processing Function
|
| 34 |
def universal_translator(text, audio_path, source_lang_name, target_lang_name):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
working_text = text
|
| 36 |
|
| 37 |
-
#
|
| 38 |
if audio_path is not None:
|
| 39 |
print(f"Audio file detected. Transcribing via Whisper ({source_lang_name})...")
|
| 40 |
whisper_lang = WHISPER_LANG_MAPPING.get(source_lang_name, None)
|
| 41 |
|
| 42 |
-
# Guide Whisper with the selected language code for better accuracy
|
| 43 |
if whisper_lang:
|
| 44 |
whisper_result = whisper_model.transcribe(audio_path, language=whisper_lang)
|
| 45 |
else:
|
|
@@ -51,13 +53,13 @@ def universal_translator(text, audio_path, source_lang_name, target_lang_name):
|
|
| 51 |
if not working_text or not working_text.strip():
|
| 52 |
return "No text or audio input provided.", "No text or audio input provided."
|
| 53 |
|
| 54 |
-
# Map friendly names to NLLB
|
| 55 |
src_code = LANG_CODES.get(source_lang_name, "eng_Latn")
|
| 56 |
tgt_code = LANG_CODES.get(target_lang_name, "tgl_Latn")
|
| 57 |
|
| 58 |
-
# Set source language
|
| 59 |
tokenizer.src_lang = src_code
|
| 60 |
-
inputs = tokenizer(working_text, return_tensors="pt").to(device)
|
| 61 |
|
| 62 |
# Get target language token ID
|
| 63 |
forced_bos_token_id = tokenizer.convert_tokens_to_ids(tgt_code)
|
|
@@ -67,7 +69,8 @@ def universal_translator(text, audio_path, source_lang_name, target_lang_name):
|
|
| 67 |
**inputs,
|
| 68 |
forced_bos_token_id=forced_bos_token_id,
|
| 69 |
max_new_tokens=256,
|
| 70 |
-
|
|
|
|
| 71 |
)
|
| 72 |
|
| 73 |
translation = tokenizer.batch_decode(output_tokens, skip_special_tokens=True)[0].strip()
|
|
@@ -83,8 +86,18 @@ with gr.Blocks(theme=gr.themes.Base()) as demo:
|
|
| 83 |
with gr.Column():
|
| 84 |
audio_in = gr.Audio(label="Audio Input (File or Mic)", type="filepath")
|
| 85 |
text_in = gr.Textbox(label="Text Input (Ignored if Audio is provided)", lines=3)
|
| 86 |
-
src_lang = gr.Dropdown(
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
submit_btn = gr.Button("Process Translation", variant="primary")
|
| 89 |
|
| 90 |
with gr.Column():
|
|
|
|
| 3 |
import whisper
|
| 4 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 5 |
|
| 6 |
+
# 1. Initialization and Model Loading
|
| 7 |
print("Initializing Hugging Face Space environment...")
|
| 8 |
|
| 9 |
+
device = "cpu"
|
| 10 |
|
| 11 |
print("Loading Whisper STT Model...")
|
| 12 |
whisper_model = whisper.load_model("small", device=device)
|
| 13 |
|
| 14 |
+
model_path = "Akariieieie/fine_tuned_dialectgov4"
|
|
|
|
| 15 |
print(f"Loading custom NLLB model from {model_path}...")
|
| 16 |
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
| 17 |
model = AutoModelForSeq2SeqLM.from_pretrained(model_path).to(device)
|
|
|
|
| 22 |
"Cebuano": "ceb_Latn"
|
| 23 |
}
|
| 24 |
|
| 25 |
+
# Whisper ISO language mapping (Set Cebuano to None to auto-detect audio phonemes)
|
| 26 |
WHISPER_LANG_MAPPING = {
|
| 27 |
"English": "en",
|
| 28 |
"Tagalog": "tl",
|
| 29 |
+
"Cebuano": None
|
| 30 |
}
|
| 31 |
|
| 32 |
# 2. Unified Processing Function
|
| 33 |
def universal_translator(text, audio_path, source_lang_name, target_lang_name):
|
| 34 |
+
# Guard clause: ensure user selects both languages before running
|
| 35 |
+
if not source_lang_name or not target_lang_name:
|
| 36 |
+
return "Please select both source and target languages.", "Please select both source and target languages."
|
| 37 |
+
|
| 38 |
working_text = text
|
| 39 |
|
| 40 |
+
# Transcribe audio file payload if present
|
| 41 |
if audio_path is not None:
|
| 42 |
print(f"Audio file detected. Transcribing via Whisper ({source_lang_name})...")
|
| 43 |
whisper_lang = WHISPER_LANG_MAPPING.get(source_lang_name, None)
|
| 44 |
|
|
|
|
| 45 |
if whisper_lang:
|
| 46 |
whisper_result = whisper_model.transcribe(audio_path, language=whisper_lang)
|
| 47 |
else:
|
|
|
|
| 53 |
if not working_text or not working_text.strip():
|
| 54 |
return "No text or audio input provided.", "No text or audio input provided."
|
| 55 |
|
| 56 |
+
# Map friendly language names to NLLB codes
|
| 57 |
src_code = LANG_CODES.get(source_lang_name, "eng_Latn")
|
| 58 |
tgt_code = LANG_CODES.get(target_lang_name, "tgl_Latn")
|
| 59 |
|
| 60 |
+
# Set source language directly in tokenizer call
|
| 61 |
tokenizer.src_lang = src_code
|
| 62 |
+
inputs = tokenizer(working_text, return_tensors="pt", src_lang=src_code).to(device)
|
| 63 |
|
| 64 |
# Get target language token ID
|
| 65 |
forced_bos_token_id = tokenizer.convert_tokens_to_ids(tgt_code)
|
|
|
|
| 69 |
**inputs,
|
| 70 |
forced_bos_token_id=forced_bos_token_id,
|
| 71 |
max_new_tokens=256,
|
| 72 |
+
num_beams=4,
|
| 73 |
+
early_stopping=True
|
| 74 |
)
|
| 75 |
|
| 76 |
translation = tokenizer.batch_decode(output_tokens, skip_special_tokens=True)[0].strip()
|
|
|
|
| 86 |
with gr.Column():
|
| 87 |
audio_in = gr.Audio(label="Audio Input (File or Mic)", type="filepath")
|
| 88 |
text_in = gr.Textbox(label="Text Input (Ignored if Audio is provided)", lines=3)
|
| 89 |
+
src_lang = gr.Dropdown(
|
| 90 |
+
choices=["English", "Tagalog", "Cebuano"],
|
| 91 |
+
value=None,
|
| 92 |
+
label="From",
|
| 93 |
+
info="Select Source Language"
|
| 94 |
+
)
|
| 95 |
+
tgt_lang = gr.Dropdown(
|
| 96 |
+
choices=["English", "Tagalog", "Cebuano"],
|
| 97 |
+
value=None,
|
| 98 |
+
label="To",
|
| 99 |
+
info="Select Target Language"
|
| 100 |
+
)
|
| 101 |
submit_btn = gr.Button("Process Translation", variant="primary")
|
| 102 |
|
| 103 |
with gr.Column():
|