dipesh1701 commited on
Commit
48ff56c
1 Parent(s): a9c115a
Files changed (1) hide show
  1. app.py +29 -26
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
- model = model_dict[model_name]["model"]
36
- tokenizer = model_dict[model_name]["tokenizer"]
37
-
38
- translator = pipeline(
39
- "translation",
40
- model=model,
41
- tokenizer=tokenizer,
42
- src_lang=source_code,
43
- tgt_lang=target_code,
44
- )
45
- translated_output = translator(input_text, max_length=400)
46
-
47
- end_time = time.time()
48
-
49
- translated_result = {
50
- "inference_time": end_time - start_time,
51
- "source": source_lang,
52
- "target": target_lang,
53
- "result": translated_output[0]["translation_text"],
54
- }
55
- return translated_result
 
 
 
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 = "The Master Betters Translator"
73
 
74
  app_description = (
75
- "This is a beta version of The Master Betters Translator that utilizes pre-trained language models for translation."
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,