dipesh1701
commited on
Commit
•
00bfa33
1
Parent(s):
f50408f
bug fix
Browse files
app.py
CHANGED
@@ -1,9 +1,10 @@
|
|
1 |
-
import
|
|
|
2 |
import gradio as gr
|
|
|
3 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
|
4 |
from flores200_codes import flores_codes
|
5 |
|
6 |
-
# Load models and tokenizers once during initialization
|
7 |
def load_models():
|
8 |
model_name_dict = {
|
9 |
"nllb-distilled-600M": "facebook/nllb-200-distilled-600M",
|
@@ -22,37 +23,38 @@ def load_models():
|
|
22 |
|
23 |
return model_dict
|
24 |
|
|
|
|
|
|
|
25 |
# Translate text using preloaded models and tokenizers
|
26 |
-
def translate_text(source, target, text
|
27 |
model_name = "nllb-distilled-600M"
|
28 |
|
29 |
-
if model_name in model_dict:
|
30 |
-
|
31 |
-
|
32 |
-
tokenizer = model_info["tokenizer"]
|
33 |
|
34 |
start_time = time.time()
|
35 |
-
|
36 |
-
|
37 |
-
target_code = flores_codes[target]
|
38 |
|
39 |
translator = pipeline(
|
40 |
"translation",
|
41 |
model=model,
|
42 |
tokenizer=tokenizer,
|
43 |
-
src_lang=
|
44 |
-
tgt_lang=
|
45 |
)
|
46 |
output = translator(text, max_length=400)
|
47 |
|
48 |
end_time = time.time()
|
49 |
|
50 |
-
|
51 |
result = {
|
52 |
"inference_time": end_time - start_time,
|
53 |
-
"source":
|
54 |
-
"target":
|
55 |
-
"result":
|
56 |
}
|
57 |
return result
|
58 |
else:
|
@@ -60,7 +62,6 @@ def translate_text(source, target, text, model_dict):
|
|
60 |
|
61 |
if __name__ == "__main__":
|
62 |
print("\tInitializing models")
|
63 |
-
model_dict = load_models()
|
64 |
|
65 |
lang_codes = list(flores_codes.keys())
|
66 |
inputs = [
|
@@ -68,13 +69,16 @@ if __name__ == "__main__":
|
|
68 |
gr.inputs.Dropdown(lang_codes, default="Nepali", label="Target"),
|
69 |
gr.inputs.Textbox(lines=5, label="Input text"),
|
70 |
]
|
|
|
71 |
outputs = gr.outputs.JSON()
|
72 |
|
73 |
title = "The Master Betters Translator"
|
|
|
|
|
74 |
description = (
|
75 |
-
"
|
76 |
)
|
77 |
-
examples = [["English", "Nepali", "
|
78 |
|
79 |
gr.Interface(
|
80 |
translate_text,
|
|
|
1 |
+
import os
|
2 |
+
import torch
|
3 |
import gradio as gr
|
4 |
+
import time
|
5 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
|
6 |
from flores200_codes import flores_codes
|
7 |
|
|
|
8 |
def load_models():
|
9 |
model_name_dict = {
|
10 |
"nllb-distilled-600M": "facebook/nllb-200-distilled-600M",
|
|
|
23 |
|
24 |
return model_dict
|
25 |
|
26 |
+
# Load models and tokenizers once during initialization
|
27 |
+
model_dict = load_models()
|
28 |
+
|
29 |
# Translate text using preloaded models and tokenizers
|
30 |
+
def translate_text(source, target, text):
|
31 |
model_name = "nllb-distilled-600M"
|
32 |
|
33 |
+
if model_name in model_dict and model_dict[model_name]["model"] is not None:
|
34 |
+
model = model_dict[model_name]["model"]
|
35 |
+
tokenizer = model_dict[model_name]["tokenizer"]
|
|
|
36 |
|
37 |
start_time = time.time()
|
38 |
+
source = flores_codes[source]
|
39 |
+
target = flores_codes[target]
|
|
|
40 |
|
41 |
translator = pipeline(
|
42 |
"translation",
|
43 |
model=model,
|
44 |
tokenizer=tokenizer,
|
45 |
+
src_lang=source,
|
46 |
+
tgt_lang=target,
|
47 |
)
|
48 |
output = translator(text, max_length=400)
|
49 |
|
50 |
end_time = time.time()
|
51 |
|
52 |
+
output = output[0]["translation_text"]
|
53 |
result = {
|
54 |
"inference_time": end_time - start_time,
|
55 |
+
"source": source,
|
56 |
+
"target": target,
|
57 |
+
"result": output,
|
58 |
}
|
59 |
return result
|
60 |
else:
|
|
|
62 |
|
63 |
if __name__ == "__main__":
|
64 |
print("\tInitializing models")
|
|
|
65 |
|
66 |
lang_codes = list(flores_codes.keys())
|
67 |
inputs = [
|
|
|
69 |
gr.inputs.Dropdown(lang_codes, default="Nepali", label="Target"),
|
70 |
gr.inputs.Textbox(lines=5, label="Input text"),
|
71 |
]
|
72 |
+
|
73 |
outputs = gr.outputs.JSON()
|
74 |
|
75 |
title = "The Master Betters Translator"
|
76 |
+
|
77 |
+
desc = "This is a beta version of The Master Betters Translator that utilizes pre-trained language models for translation. To use this app you need to have chosen the source and target language with your input text to get the output."
|
78 |
description = (
|
79 |
+
f"{desc}"
|
80 |
)
|
81 |
+
examples = [["English", "Nepali", "Hi. nice to meet you"]]
|
82 |
|
83 |
gr.Interface(
|
84 |
translate_text,
|