futranbg commited on
Commit
a866003
·
1 Parent(s): 307936e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -16
app.py CHANGED
@@ -4,6 +4,7 @@ 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
  # build model and tokenizer
@@ -24,25 +25,38 @@ def load_models():
24
 
25
  return model_dict
26
 
27
-
28
  def translation(source, target, text):
29
- if len(model_dict) == 2:
30
- #model_name = 'nllb-distilled-600M'
31
- model_name = 'nllb-distilled-1.3B'
32
-
33
- start_time = time.time()
34
  source = flores_codes[source]
35
  target = flores_codes[target]
36
-
37
- model = model_dict[model_name + '_model']
38
- tokenizer = model_dict[model_name + '_tokenizer']
39
-
40
- translator = pipeline('translation', model=model, tokenizer=tokenizer, src_lang=source, tgt_lang=target)
41
- chunks = text.splitlines(True)
42
  output = ""
43
- for chunk in chunks:
44
- stchunk = translator(chunk+"<mask>", max_length=400, num_beams=5)
45
- output += stchunk[0]['translation_text']+"\n"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
 
47
  end_time = time.time()
48
 
@@ -52,7 +66,6 @@ def translation(source, target, text):
52
  'result': output}
53
  return result
54
 
55
-
56
  if __name__ == '__main__':
57
  print('\tinit models')
58
 
 
4
  import time
5
  from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
6
  from flores200_codes import flores_codes
7
+ import vinai_translator
8
 
9
  def load_models():
10
  # build model and tokenizer
 
25
 
26
  return model_dict
27
 
 
28
  def translation(source, target, text):
 
 
 
 
 
29
  source = flores_codes[source]
30
  target = flores_codes[target]
 
 
 
 
 
 
31
  output = ""
32
+
33
+ start_time = time.time()
34
+
35
+ if source = "vie_Latn" or target ="eng_Latn":
36
+ chunks = text.splitlines(True)
37
+ for chunk in chunks:
38
+ stchunk = vinai_translator.translate_vi2en(chunk)
39
+ output += stchunk+"\n"
40
+ pass
41
+ elif source = "eng_Latn" or target ="vie_Latn":
42
+ chunks = text.splitlines(True)
43
+ for chunk in chunks:
44
+ stchunk = vinai_translator.translate_en2vi(chunk)
45
+ output += stchunk+"\n"
46
+ pass
47
+ else:
48
+ if len(model_dict) == 2:
49
+ #model_name = 'nllb-distilled-600M'
50
+ model_name = 'nllb-distilled-1.3B'
51
+
52
+ model = model_dict[model_name + '_model']
53
+ tokenizer = model_dict[model_name + '_tokenizer']
54
+
55
+ translator = pipeline('translation', model=model, tokenizer=tokenizer, src_lang=source, tgt_lang=target)
56
+ chunks = text.splitlines(True)
57
+ for chunk in chunks:
58
+ stchunk = translator(chunk+"<mask>", max_length=400, num_beams=5)
59
+ output += stchunk[0]['translation_text']+"\n"
60
 
61
  end_time = time.time()
62
 
 
66
  'result': output}
67
  return result
68
 
 
69
  if __name__ == '__main__':
70
  print('\tinit models')
71