Update app.py
Browse files
app.py
CHANGED
|
@@ -1,46 +1,22 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import torch
|
| 3 |
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
| 4 |
from peft import PeftModel
|
| 5 |
-
import os
|
| 6 |
-
import shutil
|
| 7 |
-
import gc
|
| 8 |
-
|
| 9 |
-
def cleanup_cache():
|
| 10 |
-
cache_dirs = ["/tmp/cache", "/tmp/hf", "~/.cache/huggingface"]
|
| 11 |
-
for cache_dir in cache_dirs:
|
| 12 |
-
expanded_dir = os.path.expanduser(cache_dir)
|
| 13 |
-
if os.path.exists(expanded_dir):
|
| 14 |
-
try:
|
| 15 |
-
shutil.rmtree(expanded_dir)
|
| 16 |
-
print(f"Cleaned: {expanded_dir}")
|
| 17 |
-
except:
|
| 18 |
-
pass
|
| 19 |
-
gc.collect()
|
| 20 |
-
torch.cuda.empty_cache() if torch.cuda.is_available() else None
|
| 21 |
-
|
| 22 |
-
cleanup_cache()
|
| 23 |
|
| 24 |
BASE_MODEL = "facebook/nllb-200-distilled-600M"
|
| 25 |
-
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
print("Loading tokenizer...")
|
| 28 |
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL)
|
| 29 |
|
| 30 |
-
print("Loading model with aggressive optimization...")
|
| 31 |
base_model = AutoModelForSeq2SeqLM.from_pretrained(
|
| 32 |
BASE_MODEL,
|
| 33 |
torch_dtype=torch.float16,
|
| 34 |
low_cpu_mem_usage=True,
|
| 35 |
-
device_map="auto"
|
| 36 |
-
use_safetensors=True
|
| 37 |
)
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
print("Model ready. Cleaning up...")
|
| 43 |
-
cleanup_cache()
|
| 44 |
|
| 45 |
LANG_CODES = {
|
| 46 |
"English": "eng_Latn",
|
|
@@ -55,6 +31,8 @@ def translate(text, source_lang, target_lang):
|
|
| 55 |
return "Source and target languages must be different"
|
| 56 |
|
| 57 |
try:
|
|
|
|
|
|
|
| 58 |
inputs = tokenizer(
|
| 59 |
text,
|
| 60 |
return_tensors="pt",
|
|
@@ -83,7 +61,7 @@ def swap_languages(source, target, text, translation):
|
|
| 83 |
|
| 84 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 85 |
gr.Markdown("# Oil & Gas Professional Translation")
|
| 86 |
-
gr.Markdown("English ↔ Norwegian specialized for petroleum industry")
|
| 87 |
|
| 88 |
with gr.Row():
|
| 89 |
source_lang = gr.Dropdown(
|
|
@@ -150,6 +128,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 150 |
fn=swap_languages,
|
| 151 |
inputs=[source_lang, target_lang, input_text, output_text],
|
| 152 |
outputs=[source_lang, target_lang, input_text, output_text]
|
|
|
|
| 153 |
)
|
| 154 |
|
| 155 |
copy_btn.click(
|
|
|
|
|
|
|
|
|
|
| 1 |
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
| 2 |
from peft import PeftModel
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
BASE_MODEL = "facebook/nllb-200-distilled-600M"
|
| 5 |
+
ADAPTER_NO_TO_EN = "entropy25/mt_en_no_oil"
|
| 6 |
+
#ADAPTER_EN_TO_NO = "entropy25/no_en"
|
| 7 |
+
|
| 8 |
|
|
|
|
| 9 |
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL)
|
| 10 |
|
|
|
|
| 11 |
base_model = AutoModelForSeq2SeqLM.from_pretrained(
|
| 12 |
BASE_MODEL,
|
| 13 |
torch_dtype=torch.float16,
|
| 14 |
low_cpu_mem_usage=True,
|
| 15 |
+
device_map="auto"
|
|
|
|
| 16 |
)
|
| 17 |
|
| 18 |
+
model_no_to_en = PeftModel.from_pretrained(base_model, ADAPTER_NO_TO_EN)
|
| 19 |
+
model_en_to_no = PeftModel.from_pretrained(base_model, ADAPTER_EN_TO_NO)
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
LANG_CODES = {
|
| 22 |
"English": "eng_Latn",
|
|
|
|
| 31 |
return "Source and target languages must be different"
|
| 32 |
|
| 33 |
try:
|
| 34 |
+
model = model_no_to_en if source_lang == "Norwegian" else model_en_to_no
|
| 35 |
+
|
| 36 |
inputs = tokenizer(
|
| 37 |
text,
|
| 38 |
return_tensors="pt",
|
|
|
|
| 61 |
|
| 62 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 63 |
gr.Markdown("# Oil & Gas Professional Translation")
|
| 64 |
+
gr.Markdown("English ↔ Norwegian translation specialized for petroleum industry")
|
| 65 |
|
| 66 |
with gr.Row():
|
| 67 |
source_lang = gr.Dropdown(
|
|
|
|
| 128 |
fn=swap_languages,
|
| 129 |
inputs=[source_lang, target_lang, input_text, output_text],
|
| 130 |
outputs=[source_lang, target_lang, input_text, output_text]
|
| 131 |
+
|
| 132 |
)
|
| 133 |
|
| 134 |
copy_btn.click(
|