Spaces:
Sleeping
Sleeping
Darwin Danish
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,30 +1,39 @@
|
|
1 |
import gradio as gr
|
2 |
-
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
3 |
import json
|
4 |
import os
|
|
|
|
|
5 |
os.environ["HF_TOKEN"] = os.getenv('hf')
|
6 |
|
7 |
-
# Load the tokenizer and model from Hugging Face
|
8 |
tokenizer = AutoTokenizer.from_pretrained("Darwin29/lumina-translate")
|
9 |
model = AutoModelForSeq2SeqLM.from_pretrained("Darwin29/lumina-translate")
|
10 |
|
11 |
-
# Translation function
|
12 |
def translate_text(text, translation_direction):
|
13 |
if translation_direction == "English to Iban":
|
14 |
-
#
|
15 |
-
|
16 |
-
outputs = model.generate(**inputs)
|
17 |
-
translated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
18 |
-
return json.dumps({"input_text": text, "translated_text": translated_text, "language": "en-to-iba"})
|
19 |
-
|
20 |
elif translation_direction == "Iban to English":
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
-
# Define the Gradio interface
|
28 |
iface = gr.Interface(
|
29 |
fn=translate_text,
|
30 |
inputs=[
|
@@ -33,7 +42,7 @@ iface = gr.Interface(
|
|
33 |
],
|
34 |
outputs=gr.JSON(),
|
35 |
title="English-Iban Translator",
|
36 |
-
description="This app translates between English and Iban. Choose the translation direction."
|
37 |
)
|
38 |
|
39 |
# Launch the app
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
3 |
import json
|
4 |
import os
|
5 |
+
|
6 |
+
# Ensure Hugging Face API token is set
|
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
|
37 |
iface = gr.Interface(
|
38 |
fn=translate_text,
|
39 |
inputs=[
|
|
|
42 |
],
|
43 |
outputs=gr.JSON(),
|
44 |
title="English-Iban Translator",
|
45 |
+
description="This app translates between English and Iban. Choose the translation direction and enter your text."
|
46 |
)
|
47 |
|
48 |
# Launch the app
|