Commit
·
48ff56c
1
Parent(s):
a9c115a
bug fix
Browse files
app.py
CHANGED
@@ -32,27 +32,30 @@ def translate_text(source_lang, target_lang, input_text, model_dict):
|
|
32 |
source_code = flores_codes[source_lang]
|
33 |
target_code = flores_codes[target_lang]
|
34 |
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
|
|
|
|
|
|
56 |
|
57 |
async def main():
|
58 |
print("\tInitializing models")
|
@@ -69,17 +72,17 @@ async def main():
|
|
69 |
|
70 |
outputs = gr.outputs.JSON()
|
71 |
|
72 |
-
title = "
|
73 |
|
74 |
app_description = (
|
75 |
-
"This is a beta version of
|
76 |
)
|
77 |
examples = [["English", "Nepali", "Hello, how are you?"]]
|
78 |
|
79 |
gr.Interface(
|
80 |
-
translate_text,
|
81 |
-
inputs,
|
82 |
-
outputs,
|
83 |
title=title,
|
84 |
description=app_description,
|
85 |
examples=examples,
|
|
|
32 |
source_code = flores_codes[source_lang]
|
33 |
target_code = flores_codes[target_lang]
|
34 |
|
35 |
+
if model_name in model_dict:
|
36 |
+
model = model_dict[model_name]["model"]
|
37 |
+
tokenizer = model_dict[model_name]["tokenizer"]
|
38 |
+
|
39 |
+
translator = pipeline(
|
40 |
+
"translation",
|
41 |
+
model=model,
|
42 |
+
tokenizer=tokenizer,
|
43 |
+
src_lang=source_code,
|
44 |
+
tgt_lang=target_code,
|
45 |
+
)
|
46 |
+
translated_output = translator(input_text, max_length=400)
|
47 |
+
|
48 |
+
end_time = time.time()
|
49 |
+
|
50 |
+
translated_result = {
|
51 |
+
"inference_time": end_time - start_time,
|
52 |
+
"source": source_lang,
|
53 |
+
"target": target_lang,
|
54 |
+
"result": translated_output[0]["translation_text"],
|
55 |
+
}
|
56 |
+
return translated_result
|
57 |
+
else:
|
58 |
+
raise KeyError(f"Model '{model_name}' not found in model_dict")
|
59 |
|
60 |
async def main():
|
61 |
print("\tInitializing models")
|
|
|
72 |
|
73 |
outputs = gr.outputs.JSON()
|
74 |
|
75 |
+
title = "Masterful Translator"
|
76 |
|
77 |
app_description = (
|
78 |
+
"This is a beta version of the Masterful Translator that utilizes pre-trained language models for translation."
|
79 |
)
|
80 |
examples = [["English", "Nepali", "Hello, how are you?"]]
|
81 |
|
82 |
gr.Interface(
|
83 |
+
fn=translate_text,
|
84 |
+
inputs=inputs,
|
85 |
+
outputs=outputs,
|
86 |
title=title,
|
87 |
description=app_description,
|
88 |
examples=examples,
|