Darwin Danish commited on
Commit
e9416a9
·
verified ·
1 Parent(s): f923f4b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -13
app.py CHANGED
@@ -7,23 +7,28 @@ import os
7
  os.environ["HF_TOKEN"] = os.getenv('hf')
8
 
9
  # Load the tokenizer and model from Hugging Face
10
- tokenizer = AutoTokenizer.from_pretrained("facebook/nllb-200-distilled-600M")
11
- model = AutoModelForSeq2SeqLM.from_pretrained("facebook/nllb-200-distilled-600M")
 
 
 
 
 
 
 
12
 
13
  # Translation function
14
  def translate_text(text, translation_direction):
15
- if translation_direction == "English to Iban":
16
- src_lang = "eng_Latn"
17
- tgt_lang = "iba_Latn"
18
- elif translation_direction == "Iban to English":
19
- src_lang = "iba_Latn"
20
- tgt_lang = "eng_Latn"
21
- else:
22
  return json.dumps({"error": "Invalid translation direction"})
23
 
24
- # Tokenize and translate
25
- inputs = tokenizer(text, return_tensors="pt", src_lang=src_lang)
26
- outputs = model.generate(**inputs, forced_bos_token_id=tokenizer.lang_code_to_id[tgt_lang])
 
 
 
 
27
  translated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
28
 
29
  # Return JSON response
@@ -45,5 +50,5 @@ iface = gr.Interface(
45
  description="This app translates between English and Iban. Choose the translation direction and enter your text."
46
  )
47
 
48
- # Launch the app with shareable link
49
  iface.launch(share=True)
 
7
  os.environ["HF_TOKEN"] = os.getenv('hf')
8
 
9
  # Load the tokenizer and model from Hugging Face
10
+ model_name = "facebook/nllb-200-distilled-600M"
11
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
12
+ model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
13
+
14
+ # Mapping for language codes
15
+ lang_codes = {
16
+ "English to Iban": ("eng_Latn", "iba_Latn"),
17
+ "Iban to English": ("iba_Latn", "eng_Latn")
18
+ }
19
 
20
  # Translation function
21
  def translate_text(text, translation_direction):
22
+ if translation_direction not in lang_codes:
 
 
 
 
 
 
23
  return json.dumps({"error": "Invalid translation direction"})
24
 
25
+ src_lang, tgt_lang = lang_codes[translation_direction]
26
+
27
+ # Tokenize input text
28
+ inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=512)
29
+
30
+ # Generate translation
31
+ outputs = model.generate(**inputs, forced_bos_token_id=tokenizer.convert_tokens_to_ids(f"[{tgt_lang}]"))
32
  translated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
33
 
34
  # Return JSON response
 
50
  description="This app translates between English and Iban. Choose the translation direction and enter your text."
51
  )
52
 
53
+ # Launch the app with a shareable link
54
  iface.launch(share=True)