zionia commited on
Commit
b5f7961
1 Parent(s): 3b68341

update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -12
app.py CHANGED
@@ -1,5 +1,5 @@
1
  import gradio as gr
2
- from transformers import M2M100Tokenizer, M2M100ForConditionalGeneration, pipeline
3
 
4
  model_name = "dsfsi/nr-en-m2m100-gov"
5
  tokenizer = M2M100Tokenizer.from_pretrained(model_name)
@@ -9,25 +9,22 @@ tokenizer.src_lang = "nr"
9
 
10
  model.config.forced_bos_token_id = tokenizer.get_lang_id("en")
11
 
12
- translater_nr_en = pipeline(
13
- "translation",
14
- model=model,
15
- tokenizer=tokenizer,
16
- )
17
-
18
  def translate(inp):
19
- res = translater_nr_en(inp, max_length=512, early_stopping=True)[0]['translation_text']
20
- return res
 
 
 
 
21
 
22
- # Update description and article as needed
23
  description = """
24
  <p>
25
  <center>
26
- One-way Translation from isiNdebele to English
27
  </center>
28
  </p>
29
  """
30
- article = "<p style='text-align: center'><a href='https://huggingface.co/dsfsi/nr-en-m2m100-gov' target='_blank'>by dsfsi</a></p></center></p>"
31
 
32
  examples = [
33
  ["Ngiyabonga kakhulu ngesipho osinike sona."],
@@ -46,3 +43,4 @@ iface = gr.Interface(
46
 
47
  iface.launch(enable_queue=True)
48
 
 
 
1
  import gradio as gr
2
+ from transformers import M2M100ForConditionalGeneration, M2M100Tokenizer
3
 
4
  model_name = "dsfsi/nr-en-m2m100-gov"
5
  tokenizer = M2M100Tokenizer.from_pretrained(model_name)
 
9
 
10
  model.config.forced_bos_token_id = tokenizer.get_lang_id("en")
11
 
 
 
 
 
 
 
12
  def translate(inp):
13
+ inputs = tokenizer(inp, return_tensors="pt")
14
+
15
+ translated_tokens = model.generate(**inputs, max_length=512, forced_bos_token_id=tokenizer.get_lang_id("en"))
16
+
17
+ translated_text = tokenizer.decode(translated_tokens[0], skip_special_tokens=True)
18
+ return translated_text
19
 
 
20
  description = """
21
  <p>
22
  <center>
23
+ isiNdebele to English Translation
24
  </center>
25
  </p>
26
  """
27
+ article = "<p style='text-align: center'><a href='https://huggingface.co/dsfsi/nr-en-m2m100-gov' target='_blank'>by dsfsi</a></p>"
28
 
29
  examples = [
30
  ["Ngiyabonga kakhulu ngesipho osinike sona."],
 
43
 
44
  iface.launch(enable_queue=True)
45
 
46
+