dipesh1701
commited on
Commit
•
f50408f
1
Parent(s):
5797457
code optimization
Browse files
app.py
CHANGED
@@ -1,11 +1,9 @@
|
|
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 |
-
|
9 |
def load_models():
|
10 |
model_name_dict = {
|
11 |
"nllb-distilled-600M": "facebook/nllb-200-distilled-600M",
|
@@ -24,38 +22,37 @@ def load_models():
|
|
24 |
|
25 |
return model_dict
|
26 |
|
27 |
-
# Load models and tokenizers once during initialization
|
28 |
-
model_dict = load_models()
|
29 |
-
|
30 |
# Translate text using preloaded models and tokenizers
|
31 |
-
def translate_text(source, target, text):
|
32 |
model_name = "nllb-distilled-600M"
|
33 |
|
34 |
if model_name in model_dict:
|
35 |
-
|
36 |
-
|
|
|
37 |
|
38 |
start_time = time.time()
|
39 |
-
|
40 |
-
|
|
|
41 |
|
42 |
translator = pipeline(
|
43 |
"translation",
|
44 |
model=model,
|
45 |
tokenizer=tokenizer,
|
46 |
-
src_lang=
|
47 |
-
tgt_lang=
|
48 |
)
|
49 |
output = translator(text, max_length=400)
|
50 |
|
51 |
end_time = time.time()
|
52 |
|
53 |
-
|
54 |
result = {
|
55 |
"inference_time": end_time - start_time,
|
56 |
-
"source":
|
57 |
-
"target":
|
58 |
-
"result":
|
59 |
}
|
60 |
return result
|
61 |
else:
|
@@ -63,6 +60,7 @@ def translate_text(source, target, text):
|
|
63 |
|
64 |
if __name__ == "__main__":
|
65 |
print("\tInitializing models")
|
|
|
66 |
|
67 |
lang_codes = list(flores_codes.keys())
|
68 |
inputs = [
|
@@ -70,16 +68,13 @@ if __name__ == "__main__":
|
|
70 |
gr.inputs.Dropdown(lang_codes, default="Nepali", label="Target"),
|
71 |
gr.inputs.Textbox(lines=5, label="Input text"),
|
72 |
]
|
73 |
-
|
74 |
outputs = gr.outputs.JSON()
|
75 |
|
76 |
title = "The Master Betters Translator"
|
77 |
-
|
78 |
-
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."
|
79 |
description = (
|
80 |
-
|
81 |
)
|
82 |
-
examples = [["English", "Nepali", "
|
83 |
|
84 |
gr.Interface(
|
85 |
translate_text,
|
|
|
|
|
|
|
|
|
1 |
import time
|
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 |
|
23 |
return model_dict
|
24 |
|
|
|
|
|
|
|
25 |
# Translate text using preloaded models and tokenizers
|
26 |
+
def translate_text(source, target, text, model_dict):
|
27 |
model_name = "nllb-distilled-600M"
|
28 |
|
29 |
if model_name in model_dict:
|
30 |
+
model_info = model_dict[model_name]
|
31 |
+
model = model_info["model"]
|
32 |
+
tokenizer = model_info["tokenizer"]
|
33 |
|
34 |
start_time = time.time()
|
35 |
+
|
36 |
+
source_code = flores_codes[source]
|
37 |
+
target_code = flores_codes[target]
|
38 |
|
39 |
translator = pipeline(
|
40 |
"translation",
|
41 |
model=model,
|
42 |
tokenizer=tokenizer,
|
43 |
+
src_lang=source_code,
|
44 |
+
tgt_lang=target_code,
|
45 |
)
|
46 |
output = translator(text, max_length=400)
|
47 |
|
48 |
end_time = time.time()
|
49 |
|
50 |
+
output_text = output[0]["translation_text"]
|
51 |
result = {
|
52 |
"inference_time": end_time - start_time,
|
53 |
+
"source": source_code,
|
54 |
+
"target": target_code,
|
55 |
+
"result": output_text,
|
56 |
}
|
57 |
return result
|
58 |
else:
|
|
|
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 |
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 |
+
"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."
|
76 |
)
|
77 |
+
examples = [["English", "Nepali", "Hello, how are you"]]
|
78 |
|
79 |
gr.Interface(
|
80 |
translate_text,
|