nemotronchip commited on
Commit
5f40da0
·
verified ·
1 Parent(s): f8d333a

Upload app.py

Browse files

nllb gradio model

Files changed (1) hide show
  1. app.py +62 -0
app.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
4
+
5
+ # Load model and tokenizer
6
+ model_name = "./model"
7
+ tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=False)
8
+ model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
9
+
10
+ # Check if CUDA (GPU) is available, otherwise fall back to CPU
11
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
12
+ model = model.to(device)
13
+
14
+ # Define lang code to token ID mapping manually
15
+ lang_code_to_token = {
16
+ "eng_Latn": "__eng_Latn__",
17
+ "deu_Latn": "__deu_Latn__",
18
+ "fra_Latn": "__fra_Latn__",
19
+ "spa_Latn": "__spa_Latn__"
20
+ }
21
+
22
+ # Choose target language (change as needed)
23
+ target_lang_code = "eng_Latn" # ← change to "deu_Latn" for German
24
+ src_lang_code = "mya" # Burmese
25
+
26
+ def translate(text):
27
+ if not text.strip():
28
+ return "Please enter some Burmese text."
29
+
30
+ # Set source language for tokenizer
31
+ tokenizer.src_lang = src_lang_code
32
+
33
+ # Tokenize input
34
+ inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=512).to(device)
35
+
36
+ # ✅ Get forced BOS token ID manually
37
+ lang_token = lang_code_to_token[target_lang_code]
38
+ forced_bos_token_id = tokenizer.convert_tokens_to_ids(lang_token)
39
+
40
+ # Generate translation
41
+ with torch.no_grad():
42
+ generated_tokens = model.generate(
43
+ **inputs,
44
+ forced_bos_token_id=forced_bos_token_id,
45
+ max_length=128
46
+ )
47
+
48
+ return tokenizer.decode(generated_tokens[0], skip_special_tokens=True)
49
+
50
+ # Gradio UI
51
+ with gr.Blocks(title="Burmese to English Translator") as demo:
52
+ gr.Markdown("## 🇲🇲 ➡️ 🇬🇧 Burmese to English Translator")
53
+ with gr.Row():
54
+ with gr.Column():
55
+ input_text = gr.Textbox(label="Burmese Text", placeholder="Type Burmese here...", lines=4)
56
+ btn = gr.Button("Translate")
57
+ with gr.Column():
58
+ output_text = gr.Textbox(label="Translation", interactive=False)
59
+
60
+ btn.click(translate, inputs=input_text, outputs=output_text)
61
+
62
+ demo.launch()