dipesh1701
commited on
Commit
·
9003587
1
Parent(s):
93d168d
fix
Browse files
app.py
CHANGED
@@ -1,10 +1,11 @@
|
|
|
|
1 |
import torch
|
2 |
import gradio as gr
|
3 |
import time
|
4 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
|
5 |
from flores200_codes import flores_codes
|
6 |
|
7 |
-
|
8 |
def load_models():
|
9 |
model_name_dict = {
|
10 |
"nllb-distilled-600M": "facebook/nllb-200-distilled-600M",
|
@@ -23,8 +24,11 @@ def load_models():
|
|
23 |
|
24 |
return model_dict
|
25 |
|
|
|
|
|
|
|
26 |
# Translate text using preloaded models and tokenizers
|
27 |
-
def translate_text(
|
28 |
model_name = "nllb-distilled-600M"
|
29 |
|
30 |
if model_name in model_dict:
|
@@ -32,58 +36,57 @@ def translate_text(source_lang, target_lang, input_text, model_dict):
|
|
32 |
tokenizer = model_dict[model_name]["tokenizer"]
|
33 |
|
34 |
start_time = time.time()
|
35 |
-
|
36 |
-
|
37 |
|
38 |
translator = pipeline(
|
39 |
"translation",
|
40 |
model=model,
|
41 |
tokenizer=tokenizer,
|
42 |
-
src_lang=
|
43 |
-
tgt_lang=
|
44 |
)
|
45 |
-
|
46 |
|
47 |
end_time = time.time()
|
48 |
|
49 |
-
|
|
|
50 |
"inference_time": end_time - start_time,
|
51 |
-
"source":
|
52 |
-
"target":
|
53 |
-
"result":
|
54 |
}
|
55 |
-
return
|
56 |
else:
|
57 |
raise KeyError(f"Model '{model_name}' not found in model_dict")
|
58 |
|
59 |
if __name__ == "__main__":
|
60 |
print("\tInitializing models")
|
61 |
|
62 |
-
# Load models and tokenizers
|
63 |
-
model_dict = load_models()
|
64 |
-
|
65 |
lang_codes = list(flores_codes.keys())
|
66 |
inputs = [
|
67 |
-
gr.inputs.Dropdown(lang_codes, default="English", label="Source
|
68 |
-
gr.inputs.Dropdown(lang_codes, default="Nepali", label="Target
|
69 |
-
gr.inputs.Textbox(lines=5, label="Input
|
70 |
]
|
71 |
|
72 |
outputs = gr.outputs.JSON()
|
73 |
|
74 |
title = "The Master Betters Translator"
|
75 |
|
76 |
-
|
77 |
-
|
|
|
78 |
)
|
79 |
-
examples = [["English", "Nepali", "
|
80 |
|
81 |
gr.Interface(
|
82 |
-
|
83 |
-
inputs
|
84 |
-
outputs
|
85 |
title=title,
|
86 |
-
description=
|
87 |
examples=examples,
|
88 |
examples_per_page=50,
|
89 |
).launch()
|
|
|
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 |
|
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:
|
|
|
36 |
tokenizer = model_dict[model_name]["tokenizer"]
|
37 |
|
38 |
start_time = time.time()
|
39 |
+
source = flores_codes[source]
|
40 |
+
target = flores_codes[target]
|
41 |
|
42 |
translator = pipeline(
|
43 |
"translation",
|
44 |
model=model,
|
45 |
tokenizer=tokenizer,
|
46 |
+
src_lang=source,
|
47 |
+
tgt_lang=target,
|
48 |
)
|
49 |
+
output = translator(text, max_length=400)
|
50 |
|
51 |
end_time = time.time()
|
52 |
|
53 |
+
output = output[0]["translation_text"]
|
54 |
+
result = {
|
55 |
"inference_time": end_time - start_time,
|
56 |
+
"source": source,
|
57 |
+
"target": target,
|
58 |
+
"result": output,
|
59 |
}
|
60 |
+
return result
|
61 |
else:
|
62 |
raise KeyError(f"Model '{model_name}' not found in model_dict")
|
63 |
|
64 |
if __name__ == "__main__":
|
65 |
print("\tInitializing models")
|
66 |
|
|
|
|
|
|
|
67 |
lang_codes = list(flores_codes.keys())
|
68 |
inputs = [
|
69 |
+
gr.inputs.Dropdown(lang_codes, default="English", label="Source"),
|
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 |
+
f"{desc}"
|
81 |
)
|
82 |
+
examples = [["English", "Nepali", "Hi. nice to meet you"]]
|
83 |
|
84 |
gr.Interface(
|
85 |
+
translate_text,
|
86 |
+
inputs,
|
87 |
+
outputs,
|
88 |
title=title,
|
89 |
+
description=description,
|
90 |
examples=examples,
|
91 |
examples_per_page=50,
|
92 |
).launch()
|