Akariieieie commited on
Commit
101fa34
·
verified ·
1 Parent(s): fa5dd79

update to the latest code

Browse files
Files changed (1) hide show
  1. app.py +10 -27
app.py CHANGED
@@ -31,10 +31,6 @@ WHISPER_LANG_MAPPING = {
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
  # If an audio file payload is sent (via UI or API), transcribe it first
@@ -42,11 +38,11 @@ def universal_translator(text, audio_path, source_lang_name, target_lang_name):
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
- # Guide Whisper with task="transcribe" to prevent auto-translation to English
46
  if whisper_lang:
47
- whisper_result = whisper_model.transcribe(audio_path, language=whisper_lang, task="transcribe")
48
  else:
49
- whisper_result = whisper_model.transcribe(audio_path, task="transcribe")
50
 
51
  working_text = whisper_result["text"].strip()
52
  print(f"Transcription result: {working_text}")
@@ -58,22 +54,19 @@ def universal_translator(text, audio_path, source_lang_name, target_lang_name):
58
  src_code = LANG_CODES.get(source_lang_name, "eng_Latn")
59
  tgt_code = LANG_CODES.get(target_lang_name, "tgl_Latn")
60
 
61
- # Set source and target language context explicitly on tokenizer
62
  tokenizer.src_lang = src_code
63
- tokenizer.tgt_lang = tgt_code
64
- inputs = tokenizer(working_text, return_tensors="pt", src_lang=src_code).to(device)
65
 
66
  # Get target language token ID
67
- tgt_lang_id = tokenizer.convert_tokens_to_ids(tgt_code)
68
 
69
  with torch.no_grad():
70
  output_tokens = model.generate(
71
  **inputs,
72
- forced_bos_token_id=tgt_lang_id,
73
- decoder_start_token_id=tgt_lang_id,
74
  max_new_tokens=256,
75
- num_beams=4,
76
- early_stopping=True
77
  )
78
 
79
  translation = tokenizer.batch_decode(output_tokens, skip_special_tokens=True)[0].strip()
@@ -89,18 +82,8 @@ with gr.Blocks(theme=gr.themes.Base()) as demo:
89
  with gr.Column():
90
  audio_in = gr.Audio(label="Audio Input (File or Mic)", type="filepath")
91
  text_in = gr.Textbox(label="Text Input (Ignored if Audio is provided)", lines=3)
92
- src_lang = gr.Dropdown(
93
- choices=["English", "Tagalog", "Cebuano"],
94
- value=None,
95
- label="From",
96
- info="Select Source Language"
97
- )
98
- tgt_lang = gr.Dropdown(
99
- choices=["English", "Tagalog", "Cebuano"],
100
- value=None,
101
- label="To",
102
- info="Select Target Language"
103
- )
104
  submit_btn = gr.Button("Process Translation", variant="primary")
105
 
106
  with gr.Column():
 
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
 
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}")
 
54
  src_code = LANG_CODES.get(source_lang_name, "eng_Latn")
55
  tgt_code = LANG_CODES.get(target_lang_name, "tgl_Latn")
56
 
57
+ # Set source language context
58
  tokenizer.src_lang = src_code
59
+ inputs = tokenizer(working_text, return_tensors="pt").to(device)
 
60
 
61
  # Get target language token ID
62
+ forced_bos_token_id = tokenizer.convert_tokens_to_ids(tgt_code)
63
 
64
  with torch.no_grad():
65
  output_tokens = model.generate(
66
  **inputs,
67
+ forced_bos_token_id=forced_bos_token_id,
 
68
  max_new_tokens=256,
69
+ temperature=0.1
 
70
  )
71
 
72
  translation = tokenizer.batch_decode(output_tokens, skip_special_tokens=True)[0].strip()
 
82
  with gr.Column():
83
  audio_in = gr.Audio(label="Audio Input (File or Mic)", type="filepath")
84
  text_in = gr.Textbox(label="Text Input (Ignored if Audio is provided)", lines=3)
85
+ src_lang = gr.Dropdown(choices=["English", "Tagalog", "Cebuano"], value="English", label="From")
86
+ tgt_lang = gr.Dropdown(choices=["English", "Tagalog", "Cebuano"], value="Tagalog", label="To")
 
 
 
 
 
 
 
 
 
 
87
  submit_btn = gr.Button("Process Translation", variant="primary")
88
 
89
  with gr.Column():