Update app.py
Browse files
app.py
CHANGED
|
@@ -4,25 +4,39 @@ from transformers import AutoModelForSeq2SeqLM, AutoTokenizer, BitsAndBytesConfi
|
|
| 4 |
from peft import PeftModel
|
| 5 |
from functools import lru_cache
|
| 6 |
import os
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
| 13 |
|
| 14 |
print("Loading shared base model with 8-bit quantization...")
|
| 15 |
quantization_config = BitsAndBytesConfig(load_in_8bit=True)
|
| 16 |
|
| 17 |
base_model = AutoModelForSeq2SeqLM.from_pretrained(
|
| 18 |
-
|
| 19 |
quantization_config=quantization_config,
|
| 20 |
low_cpu_mem_usage=True
|
| 21 |
)
|
| 22 |
|
| 23 |
print("Loading adapters...")
|
| 24 |
-
model = PeftModel.from_pretrained(base_model,
|
| 25 |
-
model.load_adapter(
|
| 26 |
model.eval()
|
| 27 |
|
| 28 |
QUALITY_PRESETS = {
|
|
@@ -74,10 +88,13 @@ QUALITY_TEST_CASES = {
|
|
| 74 |
}
|
| 75 |
|
| 76 |
MAX_FILE_SIZE = 1024 * 1024
|
|
|
|
| 77 |
|
| 78 |
def translate_core(text, source_lang, target_lang, quality_preset):
|
| 79 |
if not text.strip() or source_lang == target_lang:
|
| 80 |
-
return text
|
|
|
|
|
|
|
| 81 |
|
| 82 |
if source_lang == "English" and target_lang == "Norwegian":
|
| 83 |
model.set_adapter("en_to_no")
|
|
@@ -86,63 +103,74 @@ def translate_core(text, source_lang, target_lang, quality_preset):
|
|
| 86 |
model.set_adapter("no_to_en")
|
| 87 |
tgt_code = "eng_Latn"
|
| 88 |
else:
|
| 89 |
-
return "Unsupported language pair"
|
| 90 |
|
| 91 |
preset = QUALITY_PRESETS[quality_preset]
|
| 92 |
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
if not non_empty_lines:
|
| 97 |
-
return text
|
| 98 |
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
batch = non_empty_lines[i:i+batch_size]
|
| 104 |
|
| 105 |
-
|
| 106 |
-
batch,
|
| 107 |
-
return_tensors="pt",
|
| 108 |
-
padding=True,
|
| 109 |
-
truncation=True,
|
| 110 |
-
max_length=preset["max_length"]
|
| 111 |
-
)
|
| 112 |
|
| 113 |
-
|
| 114 |
-
|
| 115 |
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
|
|
|
|
|
|
| 123 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
|
| 125 |
-
|
| 126 |
-
all_results.extend(batch_results)
|
| 127 |
-
|
| 128 |
-
result_iter = iter(all_results)
|
| 129 |
-
final_lines = []
|
| 130 |
-
for line in lines:
|
| 131 |
-
if line.strip():
|
| 132 |
-
final_lines.append(next(result_iter))
|
| 133 |
-
else:
|
| 134 |
-
final_lines.append("")
|
| 135 |
|
| 136 |
-
|
|
|
|
| 137 |
|
| 138 |
@lru_cache(maxsize=512)
|
| 139 |
def translate_cached(text, source_lang, target_lang, quality_preset):
|
| 140 |
-
|
|
|
|
| 141 |
|
| 142 |
def translate(text, source_lang, target_lang, quality_preset):
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
|
| 147 |
def run_quality_tests():
|
| 148 |
results = []
|
|
@@ -157,7 +185,7 @@ def run_quality_tests():
|
|
| 157 |
results.append(f"\n{src_lang} to {tgt_lang}\n")
|
| 158 |
|
| 159 |
for i, case in enumerate(test_cases, 1):
|
| 160 |
-
translation = translate_core(case["input"], src_lang, tgt_lang, "Professional (Best Quality)")
|
| 161 |
|
| 162 |
passed_checks = []
|
| 163 |
failed_checks = []
|
|
@@ -195,26 +223,26 @@ def swap_languages(src, tgt, input_txt, output_txt):
|
|
| 195 |
|
| 196 |
def load_file(file):
|
| 197 |
if file is None:
|
| 198 |
-
return ""
|
| 199 |
|
| 200 |
try:
|
| 201 |
if os.path.getsize(file.name) > MAX_FILE_SIZE:
|
| 202 |
-
return "Error: File too large (max 1MB)"
|
| 203 |
|
| 204 |
with open(file.name, 'r', encoding='utf-8') as f:
|
| 205 |
content = f.read()
|
| 206 |
-
if len(content) >
|
| 207 |
-
return "Error: File content too long (max
|
| 208 |
-
return content
|
| 209 |
except:
|
| 210 |
try:
|
| 211 |
with open(file.name, 'r', encoding='latin-1') as f:
|
| 212 |
content = f.read()
|
| 213 |
-
if len(content) >
|
| 214 |
-
return "Error: File content too long (max
|
| 215 |
-
return content
|
| 216 |
except Exception as e:
|
| 217 |
-
return f"Error reading file: {str(e)}"
|
| 218 |
|
| 219 |
EXAMPLES_EN = {
|
| 220 |
"drilling_short": "Mud weight adjusted to 1.82 specific gravity at 3,247 meters depth.",
|
|
@@ -312,7 +340,7 @@ custom_css = """
|
|
| 312 |
border-color: #0f6fff !important;
|
| 313 |
}
|
| 314 |
.translate-btn {
|
| 315 |
-
background: #
|
| 316 |
color: white !important;
|
| 317 |
border: none !important;
|
| 318 |
padding: 12px 24px !important;
|
|
@@ -322,7 +350,14 @@ custom_css = """
|
|
| 322 |
cursor: pointer !important;
|
| 323 |
}
|
| 324 |
.translate-btn:hover {
|
| 325 |
-
background: #
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 326 |
}
|
| 327 |
.footer-info {
|
| 328 |
text-align: center !important;
|
|
@@ -335,10 +370,27 @@ custom_css = """
|
|
| 335 |
border: 1px solid #0f6fff !important;
|
| 336 |
border-radius: 4px !important;
|
| 337 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 338 |
"""
|
| 339 |
|
| 340 |
with gr.Blocks(css=custom_css, theme=gr.themes.Default()) as demo:
|
| 341 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 342 |
|
| 343 |
with gr.Row():
|
| 344 |
quality_preset = gr.Radio(
|
|
@@ -398,7 +450,15 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Default()) as demo:
|
|
| 398 |
with gr.Row():
|
| 399 |
translate_btn = gr.Button("Translate", variant="primary", elem_classes="translate-btn", size="lg")
|
| 400 |
|
| 401 |
-
gr.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 402 |
|
| 403 |
with gr.Accordion("Example Sentences", open=True):
|
| 404 |
with gr.Row():
|
|
@@ -460,7 +520,7 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Default()) as demo:
|
|
| 460 |
translate_btn.click(
|
| 461 |
fn=translate,
|
| 462 |
inputs=[input_text, source_lang, target_lang, quality_preset],
|
| 463 |
-
outputs=output_text
|
| 464 |
)
|
| 465 |
|
| 466 |
swap_btn.click(
|
|
@@ -469,6 +529,6 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Default()) as demo:
|
|
| 469 |
outputs=[source_lang, target_lang, input_text, output_text]
|
| 470 |
)
|
| 471 |
|
| 472 |
-
file_input.change(fn=load_file, inputs=file_input, outputs=input_text)
|
| 473 |
|
| 474 |
demo.queue().launch()
|
|
|
|
| 4 |
from peft import PeftModel
|
| 5 |
from functools import lru_cache
|
| 6 |
import os
|
| 7 |
+
import time
|
| 8 |
+
import nltk
|
| 9 |
|
| 10 |
+
try:
|
| 11 |
+
nltk.data.find('tokenizers/punkt')
|
| 12 |
+
except LookupError:
|
| 13 |
+
print("Downloading NLTK punkt tokenizer...")
|
| 14 |
+
nltk.download('punkt')
|
| 15 |
+
try:
|
| 16 |
+
nltk.download('punkt_tab')
|
| 17 |
+
except:
|
| 18 |
+
pass
|
| 19 |
+
|
| 20 |
+
from nltk.tokenize import sent_tokenize
|
| 21 |
+
|
| 22 |
+
BASE_MODEL = os.getenv("BASE_MODEL", "facebook/nllb-200-distilled-600M")
|
| 23 |
+
ADAPTER_EN_TO_NO = os.getenv("ADAPTER_EN_TO_NO", "entropy25/mt_en_no_oil")
|
| 24 |
+
ADAPTER_NO_TO_EN = os.getenv("ADAPTER_NO_TO_EN", "entropy25/mt_no_en_oil")
|
| 25 |
|
| 26 |
+
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL)
|
| 27 |
|
| 28 |
print("Loading shared base model with 8-bit quantization...")
|
| 29 |
quantization_config = BitsAndBytesConfig(load_in_8bit=True)
|
| 30 |
|
| 31 |
base_model = AutoModelForSeq2SeqLM.from_pretrained(
|
| 32 |
+
BASE_MODEL,
|
| 33 |
quantization_config=quantization_config,
|
| 34 |
low_cpu_mem_usage=True
|
| 35 |
)
|
| 36 |
|
| 37 |
print("Loading adapters...")
|
| 38 |
+
model = PeftModel.from_pretrained(base_model, ADAPTER_EN_TO_NO, adapter_name="en_to_no")
|
| 39 |
+
model.load_adapter(ADAPTER_NO_TO_EN, adapter_name="no_to_en")
|
| 40 |
model.eval()
|
| 41 |
|
| 42 |
QUALITY_PRESETS = {
|
|
|
|
| 88 |
}
|
| 89 |
|
| 90 |
MAX_FILE_SIZE = 1024 * 1024
|
| 91 |
+
MAX_TEXT_LENGTH = 10000
|
| 92 |
|
| 93 |
def translate_core(text, source_lang, target_lang, quality_preset):
|
| 94 |
if not text.strip() or source_lang == target_lang:
|
| 95 |
+
return text, 0.0
|
| 96 |
+
|
| 97 |
+
start_time = time.time()
|
| 98 |
|
| 99 |
if source_lang == "English" and target_lang == "Norwegian":
|
| 100 |
model.set_adapter("en_to_no")
|
|
|
|
| 103 |
model.set_adapter("no_to_en")
|
| 104 |
tgt_code = "eng_Latn"
|
| 105 |
else:
|
| 106 |
+
return "Unsupported language pair", 0.0
|
| 107 |
|
| 108 |
preset = QUALITY_PRESETS[quality_preset]
|
| 109 |
|
| 110 |
+
original_paragraphs = text.split('\n')
|
| 111 |
+
final_translated_paragraphs = []
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
+
for paragraph in original_paragraphs:
|
| 114 |
+
if not paragraph.strip():
|
| 115 |
+
final_translated_paragraphs.append("")
|
| 116 |
+
continue
|
|
|
|
| 117 |
|
| 118 |
+
sentences = sent_tokenize(paragraph)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
+
batch_size = preset["batch_size"]
|
| 121 |
+
paragraph_results = []
|
| 122 |
|
| 123 |
+
for i in range(0, len(sentences), batch_size):
|
| 124 |
+
batch = sentences[i:i+batch_size]
|
| 125 |
+
|
| 126 |
+
inputs = tokenizer(
|
| 127 |
+
batch,
|
| 128 |
+
return_tensors="pt",
|
| 129 |
+
padding=True,
|
| 130 |
+
truncation=True,
|
| 131 |
+
max_length=preset["max_length"]
|
| 132 |
)
|
| 133 |
+
|
| 134 |
+
if hasattr(model, 'device'):
|
| 135 |
+
inputs = {k: v.to(model.device) for k, v in inputs.items()}
|
| 136 |
+
|
| 137 |
+
with torch.inference_mode():
|
| 138 |
+
outputs = model.generate(
|
| 139 |
+
**inputs,
|
| 140 |
+
forced_bos_token_id=tokenizer.convert_tokens_to_ids(tgt_code),
|
| 141 |
+
max_length=preset["max_length"],
|
| 142 |
+
num_beams=preset["num_beams"],
|
| 143 |
+
early_stopping=True
|
| 144 |
+
)
|
| 145 |
+
|
| 146 |
+
batch_texts = tokenizer.batch_decode(outputs, skip_special_tokens=True)
|
| 147 |
+
paragraph_results.extend(batch_texts)
|
| 148 |
|
| 149 |
+
final_translated_paragraphs.append(" ".join(paragraph_results))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
|
| 151 |
+
elapsed_time = time.time() - start_time
|
| 152 |
+
return '\n'.join(final_translated_paragraphs), elapsed_time
|
| 153 |
|
| 154 |
@lru_cache(maxsize=512)
|
| 155 |
def translate_cached(text, source_lang, target_lang, quality_preset):
|
| 156 |
+
result, elapsed = translate_core(text, source_lang, target_lang, quality_preset)
|
| 157 |
+
return result, elapsed
|
| 158 |
|
| 159 |
def translate(text, source_lang, target_lang, quality_preset):
|
| 160 |
+
try:
|
| 161 |
+
if len(text) > MAX_TEXT_LENGTH:
|
| 162 |
+
return f"Error: Text too long (max {MAX_TEXT_LENGTH:,} characters)", ""
|
| 163 |
+
|
| 164 |
+
if not text.strip():
|
| 165 |
+
return "", ""
|
| 166 |
+
|
| 167 |
+
result, elapsed = translate_cached(text, source_lang, target_lang, quality_preset)
|
| 168 |
+
|
| 169 |
+
time_info = f"Translation completed in {elapsed:.2f}s"
|
| 170 |
+
return result, time_info
|
| 171 |
+
|
| 172 |
+
except Exception as e:
|
| 173 |
+
return f"Translation error: {str(e)}. Please try again.", ""
|
| 174 |
|
| 175 |
def run_quality_tests():
|
| 176 |
results = []
|
|
|
|
| 185 |
results.append(f"\n{src_lang} to {tgt_lang}\n")
|
| 186 |
|
| 187 |
for i, case in enumerate(test_cases, 1):
|
| 188 |
+
translation, _ = translate_core(case["input"], src_lang, tgt_lang, "Professional (Best Quality)")
|
| 189 |
|
| 190 |
passed_checks = []
|
| 191 |
failed_checks = []
|
|
|
|
| 223 |
|
| 224 |
def load_file(file):
|
| 225 |
if file is None:
|
| 226 |
+
return "", ""
|
| 227 |
|
| 228 |
try:
|
| 229 |
if os.path.getsize(file.name) > MAX_FILE_SIZE:
|
| 230 |
+
return "Error: File too large (max 1MB)", ""
|
| 231 |
|
| 232 |
with open(file.name, 'r', encoding='utf-8') as f:
|
| 233 |
content = f.read()
|
| 234 |
+
if len(content) > MAX_TEXT_LENGTH:
|
| 235 |
+
return f"Error: File content too long (max {MAX_TEXT_LENGTH:,} characters)", ""
|
| 236 |
+
return content, ""
|
| 237 |
except:
|
| 238 |
try:
|
| 239 |
with open(file.name, 'r', encoding='latin-1') as f:
|
| 240 |
content = f.read()
|
| 241 |
+
if len(content) > MAX_TEXT_LENGTH:
|
| 242 |
+
return f"Error: File content too long (max {MAX_TEXT_LENGTH:,} characters)", ""
|
| 243 |
+
return content, ""
|
| 244 |
except Exception as e:
|
| 245 |
+
return f"Error reading file: {str(e)}", ""
|
| 246 |
|
| 247 |
EXAMPLES_EN = {
|
| 248 |
"drilling_short": "Mud weight adjusted to 1.82 specific gravity at 3,247 meters depth.",
|
|
|
|
| 340 |
border-color: #0f6fff !important;
|
| 341 |
}
|
| 342 |
.translate-btn {
|
| 343 |
+
background: #ff8c00 !important;
|
| 344 |
color: white !important;
|
| 345 |
border: none !important;
|
| 346 |
padding: 12px 24px !important;
|
|
|
|
| 350 |
cursor: pointer !important;
|
| 351 |
}
|
| 352 |
.translate-btn:hover {
|
| 353 |
+
background: #e67e00 !important;
|
| 354 |
+
}
|
| 355 |
+
.time-info {
|
| 356 |
+
text-align: center !important;
|
| 357 |
+
color: #666 !important;
|
| 358 |
+
font-size: 13px !important;
|
| 359 |
+
padding: 10px !important;
|
| 360 |
+
font-style: italic !important;
|
| 361 |
}
|
| 362 |
.footer-info {
|
| 363 |
text-align: center !important;
|
|
|
|
| 370 |
border: 1px solid #0f6fff !important;
|
| 371 |
border-radius: 4px !important;
|
| 372 |
}
|
| 373 |
+
.disclaimer {
|
| 374 |
+
background: #fff9e6 !important;
|
| 375 |
+
border-left: 4px solid #ff8c00 !important;
|
| 376 |
+
padding: 15px 20px !important;
|
| 377 |
+
margin: 20px 0 !important;
|
| 378 |
+
border-radius: 4px !important;
|
| 379 |
+
font-size: 13px !important;
|
| 380 |
+
color: #666 !important;
|
| 381 |
+
}
|
| 382 |
"""
|
| 383 |
|
| 384 |
with gr.Blocks(css=custom_css, theme=gr.themes.Default()) as demo:
|
| 385 |
+
|
| 386 |
+
gr.HTML("""
|
| 387 |
+
<div class='disclaimer'>
|
| 388 |
+
<strong>Privacy & Compliance:</strong> This model is fine-tuned on public domain oil & gas terminology.
|
| 389 |
+
No proprietary data was used in training. Local inference ensures data privacy and GDPR compliance.
|
| 390 |
+
<br><br>
|
| 391 |
+
<strong>Terminology Enforcement:</strong> Advanced lexical constraints for critical technical terms are currently in development.
|
| 392 |
+
</div>
|
| 393 |
+
""")
|
| 394 |
|
| 395 |
with gr.Row():
|
| 396 |
quality_preset = gr.Radio(
|
|
|
|
| 450 |
with gr.Row():
|
| 451 |
translate_btn = gr.Button("Translate", variant="primary", elem_classes="translate-btn", size="lg")
|
| 452 |
|
| 453 |
+
with gr.Row():
|
| 454 |
+
time_display = gr.Textbox(
|
| 455 |
+
show_label=False,
|
| 456 |
+
container=False,
|
| 457 |
+
interactive=False,
|
| 458 |
+
elem_classes="time-info"
|
| 459 |
+
)
|
| 460 |
+
|
| 461 |
+
gr.HTML("<div class='footer-info'>Oil & Gas Translation • English ↔ Norwegian • Production-Ready System</div>")
|
| 462 |
|
| 463 |
with gr.Accordion("Example Sentences", open=True):
|
| 464 |
with gr.Row():
|
|
|
|
| 520 |
translate_btn.click(
|
| 521 |
fn=translate,
|
| 522 |
inputs=[input_text, source_lang, target_lang, quality_preset],
|
| 523 |
+
outputs=[output_text, time_display]
|
| 524 |
)
|
| 525 |
|
| 526 |
swap_btn.click(
|
|
|
|
| 529 |
outputs=[source_lang, target_lang, input_text, output_text]
|
| 530 |
)
|
| 531 |
|
| 532 |
+
file_input.change(fn=load_file, inputs=file_input, outputs=[input_text, time_display])
|
| 533 |
|
| 534 |
demo.queue().launch()
|