Updated app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,6 @@ import numpy as np
|
|
| 4 |
import librosa
|
| 5 |
import os
|
| 6 |
import ctranslate2
|
| 7 |
-
|
| 8 |
from transformers import (
|
| 9 |
AutoProcessor,
|
| 10 |
AutoModelForSpeechSeq2Seq,
|
|
@@ -35,36 +34,40 @@ asr_model = AutoModelForSpeechSeq2Seq.from_pretrained(
|
|
| 35 |
).to(device)
|
| 36 |
|
| 37 |
asr_model.eval()
|
| 38 |
-
|
| 39 |
print("ASR Loaded")
|
| 40 |
|
| 41 |
# ====================================
|
| 42 |
-
# LOAD TOKENIZER
|
| 43 |
# ====================================
|
| 44 |
-
|
|
|
|
|
|
|
| 45 |
|
| 46 |
# ====================================
|
| 47 |
-
# CT2 CONVERT
|
| 48 |
# ====================================
|
| 49 |
if not os.path.exists(CT2_DIR):
|
|
|
|
| 50 |
os.system(
|
| 51 |
f"ct2-transformers-converter "
|
| 52 |
f"--model {MT_MODEL} "
|
| 53 |
f"--output_dir {CT2_DIR} "
|
| 54 |
f"--quantization int8"
|
| 55 |
)
|
|
|
|
| 56 |
|
|
|
|
| 57 |
translator = ctranslate2.Translator(
|
| 58 |
CT2_DIR,
|
| 59 |
device=device,
|
| 60 |
compute_type="int8"
|
| 61 |
)
|
|
|
|
| 62 |
|
| 63 |
# ====================================
|
| 64 |
-
# AUDIO
|
| 65 |
# ====================================
|
| 66 |
def fix_audio(audio):
|
| 67 |
-
|
| 68 |
sr, wav = audio
|
| 69 |
|
| 70 |
if len(wav.shape) > 1:
|
|
@@ -81,12 +84,10 @@ def fix_audio(audio):
|
|
| 81 |
|
| 82 |
return wav
|
| 83 |
|
| 84 |
-
|
| 85 |
# ====================================
|
| 86 |
# ASR
|
| 87 |
# ====================================
|
| 88 |
def transcribe(audio):
|
| 89 |
-
|
| 90 |
if audio is None:
|
| 91 |
return ""
|
| 92 |
|
|
@@ -114,51 +115,47 @@ def transcribe(audio):
|
|
| 114 |
|
| 115 |
return text
|
| 116 |
|
| 117 |
-
|
| 118 |
# ====================================
|
| 119 |
# MT
|
| 120 |
# ====================================
|
| 121 |
def translate(text):
|
| 122 |
-
|
| 123 |
if not text:
|
| 124 |
return ""
|
| 125 |
|
| 126 |
-
|
|
|
|
| 127 |
|
| 128 |
-
|
| 129 |
-
|
|
|
|
| 130 |
|
|
|
|
| 131 |
results = translator.translate_batch(
|
| 132 |
[tokens],
|
| 133 |
target_prefix=[["eng_Latn"]],
|
| 134 |
-
beam_size=
|
| 135 |
)
|
| 136 |
|
| 137 |
out = results[0].hypotheses[0]
|
| 138 |
|
|
|
|
| 139 |
if out[0] == "eng_Latn":
|
| 140 |
out = out[1:]
|
| 141 |
|
| 142 |
-
ids =
|
| 143 |
-
|
| 144 |
-
return tokenizer.decode(ids, skip_special_tokens=True)
|
| 145 |
-
|
| 146 |
|
| 147 |
# ====================================
|
| 148 |
# PIPELINE
|
| 149 |
# ====================================
|
| 150 |
def pipeline(audio):
|
| 151 |
-
|
| 152 |
try:
|
| 153 |
efik = transcribe(audio)
|
| 154 |
eng = translate(efik)
|
| 155 |
-
|
| 156 |
return efik, eng
|
| 157 |
-
|
| 158 |
except Exception as e:
|
| 159 |
return f"ERROR: {str(e)}", ""
|
| 160 |
|
| 161 |
-
|
| 162 |
# ====================================
|
| 163 |
# UI
|
| 164 |
# ====================================
|
|
@@ -171,7 +168,7 @@ with gr.Blocks() as demo:
|
|
| 171 |
type="numpy"
|
| 172 |
)
|
| 173 |
|
| 174 |
-
btn = gr.Button("๐
|
| 175 |
|
| 176 |
out1 = gr.Textbox(label="Efik Text")
|
| 177 |
out2 = gr.Textbox(label="English")
|
|
|
|
| 4 |
import librosa
|
| 5 |
import os
|
| 6 |
import ctranslate2
|
|
|
|
| 7 |
from transformers import (
|
| 8 |
AutoProcessor,
|
| 9 |
AutoModelForSpeechSeq2Seq,
|
|
|
|
| 34 |
).to(device)
|
| 35 |
|
| 36 |
asr_model.eval()
|
|
|
|
| 37 |
print("ASR Loaded")
|
| 38 |
|
| 39 |
# ====================================
|
| 40 |
+
# LOAD MT TOKENIZER
|
| 41 |
# ====================================
|
| 42 |
+
print("Loading MT tokenizer...")
|
| 43 |
+
mt_tokenizer = AutoTokenizer.from_pretrained(MT_MODEL)
|
| 44 |
+
print("MT tokenizer loaded")
|
| 45 |
|
| 46 |
# ====================================
|
| 47 |
+
# CT2 CONVERT & LOAD
|
| 48 |
# ====================================
|
| 49 |
if not os.path.exists(CT2_DIR):
|
| 50 |
+
print("Converting MT model to CTranslate2 format...")
|
| 51 |
os.system(
|
| 52 |
f"ct2-transformers-converter "
|
| 53 |
f"--model {MT_MODEL} "
|
| 54 |
f"--output_dir {CT2_DIR} "
|
| 55 |
f"--quantization int8"
|
| 56 |
)
|
| 57 |
+
print("Conversion done")
|
| 58 |
|
| 59 |
+
print("Loading CTranslate2 translator...")
|
| 60 |
translator = ctranslate2.Translator(
|
| 61 |
CT2_DIR,
|
| 62 |
device=device,
|
| 63 |
compute_type="int8"
|
| 64 |
)
|
| 65 |
+
print("Translator loaded")
|
| 66 |
|
| 67 |
# ====================================
|
| 68 |
+
# AUDIO UTILS
|
| 69 |
# ====================================
|
| 70 |
def fix_audio(audio):
|
|
|
|
| 71 |
sr, wav = audio
|
| 72 |
|
| 73 |
if len(wav.shape) > 1:
|
|
|
|
| 84 |
|
| 85 |
return wav
|
| 86 |
|
|
|
|
| 87 |
# ====================================
|
| 88 |
# ASR
|
| 89 |
# ====================================
|
| 90 |
def transcribe(audio):
|
|
|
|
| 91 |
if audio is None:
|
| 92 |
return ""
|
| 93 |
|
|
|
|
| 115 |
|
| 116 |
return text
|
| 117 |
|
|
|
|
| 118 |
# ====================================
|
| 119 |
# MT
|
| 120 |
# ====================================
|
| 121 |
def translate(text):
|
|
|
|
| 122 |
if not text:
|
| 123 |
return ""
|
| 124 |
|
| 125 |
+
# Prepend source tag directly to text, matching the working API
|
| 126 |
+
input_text = f"ibo_Latn {text}"
|
| 127 |
|
| 128 |
+
# Tokenize
|
| 129 |
+
ids = mt_tokenizer.encode(input_text)
|
| 130 |
+
tokens = mt_tokenizer.convert_ids_to_tokens(ids)
|
| 131 |
|
| 132 |
+
# Translate with CTranslate2
|
| 133 |
results = translator.translate_batch(
|
| 134 |
[tokens],
|
| 135 |
target_prefix=[["eng_Latn"]],
|
| 136 |
+
beam_size=4
|
| 137 |
)
|
| 138 |
|
| 139 |
out = results[0].hypotheses[0]
|
| 140 |
|
| 141 |
+
# Strip target prefix token if present
|
| 142 |
if out[0] == "eng_Latn":
|
| 143 |
out = out[1:]
|
| 144 |
|
| 145 |
+
ids = mt_tokenizer.convert_tokens_to_ids(out)
|
| 146 |
+
return mt_tokenizer.decode(ids, skip_special_tokens=True)
|
|
|
|
|
|
|
| 147 |
|
| 148 |
# ====================================
|
| 149 |
# PIPELINE
|
| 150 |
# ====================================
|
| 151 |
def pipeline(audio):
|
|
|
|
| 152 |
try:
|
| 153 |
efik = transcribe(audio)
|
| 154 |
eng = translate(efik)
|
|
|
|
| 155 |
return efik, eng
|
|
|
|
| 156 |
except Exception as e:
|
| 157 |
return f"ERROR: {str(e)}", ""
|
| 158 |
|
|
|
|
| 159 |
# ====================================
|
| 160 |
# UI
|
| 161 |
# ====================================
|
|
|
|
| 168 |
type="numpy"
|
| 169 |
)
|
| 170 |
|
| 171 |
+
btn = gr.Button("๐ Translate")
|
| 172 |
|
| 173 |
out1 = gr.Textbox(label="Efik Text")
|
| 174 |
out2 = gr.Textbox(label="English")
|