Akariieieie commited on
Commit
a53aff2
·
verified ·
1 Parent(s): 4c623ef

FIx the code (main code)

Browse files
Files changed (1) hide show
  1. app.py +16 -28
app.py CHANGED
@@ -3,10 +3,10 @@ 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)
@@ -22,26 +22,23 @@ LANG_CODES = {
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,13 +50,13 @@ def universal_translator(text, audio_path, source_lang_name, target_lang_name):
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,41 +66,32 @@ def universal_translator(text, audio_path, source_lang_name, target_lang_name):
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()
77
 
 
78
  return working_text, translation
79
 
80
  # 3. Gradio Interface Layout
81
  with gr.Blocks(theme=gr.themes.Base()) as demo:
82
- gr.Markdown("# 🇵🇭 DialectGo Unified Translation System")
83
- gr.Markdown("This space functions as both a web UI and an active API endpoint for multi-way voice/text translation.")
84
 
85
  with gr.Row():
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():
 
104
  transcription_out = gr.Textbox(label="Transcribed/Source Text", lines=3, interactive=False)
105
  output_text = gr.Textbox(label="Translation Result", lines=4, interactive=False)
106
 
 
107
  submit_btn.click(
108
  fn=universal_translator,
109
  inputs=[text_in, audio_in, src_lang, tgt_lang],
 
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("small", device=device)
 
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": "tl"
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:
 
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")
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)
 
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()
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:
79
+ gr.Markdown("DialectGo Unified Translation System")
 
80
 
81
  with gr.Row():
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():
90
+ # MODIFICATION: Added text block interface container to capture the output transcript
91
  transcription_out = gr.Textbox(label="Transcribed/Source Text", lines=3, interactive=False)
92
  output_text = gr.Textbox(label="Translation Result", lines=4, interactive=False)
93
 
94
+ # MODIFICATION: Changed outputs array to map data cleanly to BOTH UI textboxes
95
  submit_btn.click(
96
  fn=universal_translator,
97
  inputs=[text_in, audio_in, src_lang, tgt_lang],