ilhamsyahids commited on
Commit
74a97fe
1 Parent(s): b601f49

sentence-wise translator

Browse files

Signed-off-by: Ilham Syahid S <ilhamsyahids@gmail.com>

Files changed (1) hide show
  1. app.py +27 -9
app.py CHANGED
@@ -40,20 +40,30 @@ def translation(model_name, source, target, text):
40
  src_lang=source,
41
  tgt_lang=target,
42
  )
43
- output = translator(text, max_length=400)
 
 
 
 
 
 
 
 
 
44
 
45
  end_time = time.time()
46
 
47
- full_output = output
48
- output = output[0]["translation_text"]
 
49
  result = {
50
  "inference_time": end_time - start_time,
51
  "source": source,
52
  "target": target,
53
  "result": output,
54
- "full_output": full_output,
55
  }
56
- return result
57
 
58
 
59
  if __name__ == "__main__":
@@ -71,7 +81,7 @@ if __name__ == "__main__":
71
  "nllb-distilled-600M",
72
  "nllb-distilled-1.3B",
73
  # "nllb-1.3B",
74
- # "nllb-3.3B"
75
  ],
76
  label="NLLB Model",
77
  default="nllb-distilled-1.3B",
@@ -81,15 +91,23 @@ if __name__ == "__main__":
81
  gr.inputs.Textbox(lines=5, label="Input text"),
82
  ]
83
 
84
- outputs = gr.outputs.JSON()
 
 
 
85
 
86
  title = "NLLB (No Language Left Behind) demo"
87
 
88
  demo_status = "Demo is running on CPU"
89
- description = f"Using NLLB model, details: https://github.com/facebookresearch/fairseq/tree/nllb. {demo_status}"
90
  examples = [
91
  ["nllb-distilled-1.3B", "Najdi Arabic", "English", "جلست اطفال"],
92
- ["nllb-distilled-600M", "Najdi Arabic", "English", "شد للبيع طابقين مع شرع له نظيف حق غمارتين"]
 
 
 
 
 
93
  ]
94
 
95
  gr.Interface(
 
40
  src_lang=source,
41
  tgt_lang=target,
42
  )
43
+
44
+ # sentence-wise translation
45
+ sentences = text.split("\n")
46
+ translated_sentences = []
47
+ for sentence in sentences:
48
+ translated_sentence = translator(sentence, max_length=400)[0][
49
+ "translation_text"
50
+ ]
51
+ translated_sentences.append(translated_sentence)
52
+ output = "\n".join(translated_sentences)
53
 
54
  end_time = time.time()
55
 
56
+ # output = translator(text, max_length=400)
57
+ # full_output = output
58
+ # output = output[0]["translation_text"]
59
  result = {
60
  "inference_time": end_time - start_time,
61
  "source": source,
62
  "target": target,
63
  "result": output,
64
+ # "full_output": full_output,
65
  }
66
+ return result, output
67
 
68
 
69
  if __name__ == "__main__":
 
81
  "nllb-distilled-600M",
82
  "nllb-distilled-1.3B",
83
  # "nllb-1.3B",
84
+ # "nllb-3.3B"
85
  ],
86
  label="NLLB Model",
87
  default="nllb-distilled-1.3B",
 
91
  gr.inputs.Textbox(lines=5, label="Input text"),
92
  ]
93
 
94
+ outputs = [
95
+ gr.outputs.JSON(label="Metadata"),
96
+ gr.outputs.Textbox(label="Output text"),
97
+ ]
98
 
99
  title = "NLLB (No Language Left Behind) demo"
100
 
101
  demo_status = "Demo is running on CPU"
102
+ description = f"Using NLLB model, details: https://github.com/facebookresearch/fairseq/tree/nllb.\n{demo_status}"
103
  examples = [
104
  ["nllb-distilled-1.3B", "Najdi Arabic", "English", "جلست اطفال"],
105
+ [
106
+ "nllb-distilled-600M",
107
+ "Najdi Arabic",
108
+ "English",
109
+ "شد للبيع طابقين مع شرع له نظيف حق غمارتين",
110
+ ],
111
  ]
112
 
113
  gr.Interface(