Spaces:
Running
Running
Darwin Danish
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,44 +1,30 @@
|
|
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 |
-
|
11 |
-
|
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
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
"translated_text": translated_text,
|
38 |
-
"language": f"{src_lang} to {tgt_lang}"
|
39 |
-
}, indent=4)
|
40 |
-
|
41 |
-
# Define the Gradio interface
|
42 |
iface = gr.Interface(
|
43 |
fn=translate_text,
|
44 |
inputs=[
|
@@ -47,8 +33,8 @@ iface = gr.Interface(
|
|
47 |
],
|
48 |
outputs=gr.JSON(),
|
49 |
title="English-Iban Translator",
|
50 |
-
description="This app translates between English and Iban. Choose the translation direction
|
51 |
)
|
52 |
|
53 |
-
# Launch the app
|
54 |
-
iface.launch(
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
|
3 |
import json
|
4 |
import os
|
|
|
|
|
5 |
os.environ["HF_TOKEN"] = os.getenv('hf')
|
6 |
|
7 |
+
# Load the tokenizer and model from Hugging Face (authentication handled via token)
|
8 |
+
tokenizer = AutoTokenizer.from_pretrained("Darwin29/lumina-translate")
|
9 |
+
model = AutoModelForSeq2SeqLM.from_pretrained("Darwin29/lumina-translate")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
+
# Translation function (English to Iban and Iban to English)
|
12 |
def translate_text(text, translation_direction):
|
13 |
+
if translation_direction == "English to Iban":
|
14 |
+
# Translate English to Iban
|
15 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=512)
|
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-iban"})
|
19 |
+
|
20 |
+
elif translation_direction == "Iban to English":
|
21 |
+
# Translate Iban to English (you'll need a reverse model or translation setup here)
|
22 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=512)
|
23 |
+
outputs = model.generate(**inputs)
|
24 |
+
translated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
25 |
+
return json.dumps({"input_text": text, "translated_text": translated_text, "language": "iban-to-en"})
|
26 |
+
|
27 |
+
# Define the Gradio interface with a dropdown to select translation direction
|
|
|
|
|
|
|
|
|
|
|
28 |
iface = gr.Interface(
|
29 |
fn=translate_text,
|
30 |
inputs=[
|
|
|
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
|
40 |
+
iface.launch()
|