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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -11
app.py CHANGED
@@ -7,30 +7,30 @@ 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("Darwin29/lumina-translate")
11
- model = AutoModelForSeq2SeqLM.from_pretrained("Darwin29/lumina-translate")
12
 
13
  # Translation function
14
  def translate_text(text, translation_direction):
15
  if translation_direction == "English to Iban":
16
- input_text = f"en->iba: {text}" # Explicitly set source and target languages
17
- lang_code = "en-to-iba"
18
  elif translation_direction == "Iban to English":
19
- input_text = f"iba->en: {text}"
20
- lang_code = "iba-to-en"
21
  else:
22
  return json.dumps({"error": "Invalid translation direction"})
23
 
24
  # Tokenize and translate
25
- inputs = tokenizer(input_text, return_tensors="pt", truncation=True, padding=True, max_length=512)
26
- outputs = model.generate(**inputs)
27
  translated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
28
 
29
  # Return JSON response
30
  return json.dumps({
31
  "input_text": text,
32
  "translated_text": translated_text,
33
- "language": lang_code
34
  }, indent=4)
35
 
36
  # Define the Gradio interface
@@ -45,5 +45,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
49
- iface.launch()
 
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
30
  return json.dumps({
31
  "input_text": text,
32
  "translated_text": translated_text,
33
+ "language": f"{src_lang} to {tgt_lang}"
34
  }, indent=4)
35
 
36
  # Define the Gradio 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)