Spaces:
Sleeping
Sleeping
Change the app.py to accommodate the use of the audio
Browse files
app.py
CHANGED
|
@@ -1,83 +1,85 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
|
|
|
| 3 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 4 |
|
| 5 |
-
# 1.
|
| 6 |
-
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
print(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
| 10 |
-
model = AutoModelForSeq2SeqLM.from_pretrained(model_path)
|
| 11 |
|
| 12 |
-
# 2. Map display names to NLLB-200 language codes
|
| 13 |
LANG_CODES = {
|
| 14 |
"English": "eng_Latn",
|
| 15 |
"Tagalog": "tgl_Latn",
|
| 16 |
"Cebuano": "ceb_Latn"
|
| 17 |
}
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
# Get the correct NLLB language tokens
|
| 24 |
-
source_code = LANG_CODES[source_lang_name]
|
| 25 |
-
target_code = LANG_CODES[target_lang_name]
|
| 26 |
-
|
| 27 |
-
# Force the tokenizer to use the selected source language prefix
|
| 28 |
-
tokenizer.src_lang = source_code
|
| 29 |
-
inputs = tokenizer(text, return_tensors="pt")
|
| 30 |
-
|
| 31 |
-
# Generate translation targeting the destination language prefix
|
| 32 |
-
translated_tokens = model.generate(
|
| 33 |
-
**inputs,
|
| 34 |
-
forced_bos_token_id=tokenizer.convert_tokens_to_ids(target_code),
|
| 35 |
-
max_length=128
|
| 36 |
-
)
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
with gr.Blocks(theme=gr.themes.Base()) as demo:
|
| 43 |
-
gr.Markdown("# 🇵🇭 DialectGo
|
| 44 |
-
gr.Markdown("
|
| 45 |
|
| 46 |
with gr.Row():
|
| 47 |
with gr.Column():
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
)
|
| 53 |
-
|
| 54 |
-
# Sub-layout to put selectors side-by-side
|
| 55 |
-
with gr.Row():
|
| 56 |
-
source_lang = gr.Dropdown(
|
| 57 |
-
choices=["English", "Tagalog", "Cebuano"],
|
| 58 |
-
value="English",
|
| 59 |
-
label="Translate From"
|
| 60 |
-
)
|
| 61 |
-
target_lang = gr.Dropdown(
|
| 62 |
-
choices=["English", "Tagalog", "Cebuano"],
|
| 63 |
-
value="Tagalog",
|
| 64 |
-
label="Translate To"
|
| 65 |
-
)
|
| 66 |
-
|
| 67 |
-
submit_btn = gr.Button("Translate", variant="primary")
|
| 68 |
|
| 69 |
with gr.Column():
|
| 70 |
-
output_text = gr.Textbox(
|
| 71 |
-
label="Translation Output",
|
| 72 |
-
lines=4,
|
| 73 |
-
interactive=False
|
| 74 |
-
)
|
| 75 |
|
| 76 |
-
#
|
| 77 |
submit_btn.click(
|
| 78 |
-
fn=
|
| 79 |
-
inputs=[
|
| 80 |
-
outputs=output_text
|
|
|
|
| 81 |
)
|
| 82 |
|
| 83 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
+
import whisper
|
| 4 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 5 |
|
| 6 |
+
# 1. Initialization and Model Loading (Optimized for Hugging Face Free CPU tier)
|
| 7 |
+
print("Initializing Hugging Face Space environment...")
|
| 8 |
+
|
| 9 |
+
device = "cpu"
|
| 10 |
|
| 11 |
+
print("Loading Whisper STT Model...")
|
| 12 |
+
whisper_model = whisper.load_model("tiny", device=device)
|
| 13 |
+
|
| 14 |
+
model_path = "Akariieieie/DialectGo-NLLB"
|
| 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)
|
| 18 |
|
|
|
|
| 19 |
LANG_CODES = {
|
| 20 |
"English": "eng_Latn",
|
| 21 |
"Tagalog": "tgl_Latn",
|
| 22 |
"Cebuano": "ceb_Latn"
|
| 23 |
}
|
| 24 |
|
| 25 |
+
# 2. Unified Processing Function (Replaces your Flask route logic)
|
| 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 |
+
whisper_result = whisper_model.transcribe(audio_path)
|
| 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")
|
| 41 |
+
tgt_code = LANG_CODES.get(target_lang_name, "tgl_Latn")
|
| 42 |
+
|
| 43 |
+
# Set source language context
|
| 44 |
+
tokenizer.src_lang = src_code
|
| 45 |
+
inputs = tokenizer(working_text, return_tensors="pt").to(device)
|
| 46 |
+
|
| 47 |
+
# Get target language token ID
|
| 48 |
+
forced_bos_token_id = tokenizer.convert_tokens_to_ids(tgt_code)
|
| 49 |
+
|
| 50 |
+
with torch.no_grad():
|
| 51 |
+
output_tokens = model.generate(
|
| 52 |
+
**inputs,
|
| 53 |
+
forced_bos_token_id=forced_bos_token_id,
|
| 54 |
+
max_new_tokens=256,
|
| 55 |
+
temperature=0.1
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
translation = tokenizer.batch_decode(output_tokens, skip_special_tokens=True)[0]
|
| 59 |
+
return translation.strip()
|
| 60 |
+
|
| 61 |
+
# 3. Gradio Interface Layout
|
| 62 |
with gr.Blocks(theme=gr.themes.Base()) as demo:
|
| 63 |
+
gr.Markdown("# 🇵🇭 DialectGo Unified Translation System")
|
| 64 |
+
gr.Markdown("This space functions as both a web UI and an active API endpoint for multi-way voice/text translation.")
|
| 65 |
|
| 66 |
with gr.Row():
|
| 67 |
with gr.Column():
|
| 68 |
+
audio_in = gr.Audio(label="Audio Input (File or Mic)", type="filepath")
|
| 69 |
+
text_in = gr.Textbox(label="Text Input (Ignored if Audio is provided)", lines=3)
|
| 70 |
+
src_lang = gr.Dropdown(choices=["English", "Tagalog", "Cebuano"], value="English", label="From")
|
| 71 |
+
tgt_lang = gr.Dropdown(choices=["English", "Tagalog", "Cebuano"], value="Tagalog", label="To")
|
| 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 |
+
# Set explicit api_name so your external apps can hit it cleanly
|
| 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 |
|
| 85 |
demo.launch()
|