Spaces:
Sleeping
Sleeping
Darwin Danish
commited on
Update app.py
Browse files
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("
|
11 |
-
model = AutoModelForSeq2SeqLM.from_pretrained("
|
12 |
|
13 |
# Translation function
|
14 |
def translate_text(text, translation_direction):
|
15 |
if translation_direction == "English to Iban":
|
16 |
-
|
17 |
-
|
18 |
elif translation_direction == "Iban to English":
|
19 |
-
|
20 |
-
|
21 |
else:
|
22 |
return json.dumps({"error": "Invalid translation direction"})
|
23 |
|
24 |
# Tokenize and translate
|
25 |
-
inputs = tokenizer(
|
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":
|
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)
|