Spaces:
Sleeping
Sleeping
Darwin Danish
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
3 |
+
import json
|
4 |
+
|
5 |
+
# Load the tokenizer and model
|
6 |
+
tokenizer = AutoTokenizer.from_pretrained("Darwin29/lumina-translate")
|
7 |
+
model = AutoModelForSeq2SeqLM.from_pretrained("Darwin29/lumina-translate")
|
8 |
+
|
9 |
+
# Translation function (English to Iban and Iban to English)
|
10 |
+
def translate_text(text, translation_direction):
|
11 |
+
if translation_direction == "English to Iban":
|
12 |
+
# Translate English to Iban
|
13 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=512)
|
14 |
+
outputs = model.generate(**inputs)
|
15 |
+
translated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
16 |
+
return json.dumps({"input_text": text, "translated_text": translated_text, "language": "en-to-iban"})
|
17 |
+
|
18 |
+
elif translation_direction == "Iban to English":
|
19 |
+
# Translate Iban to English (you'll need a reverse model or translation setup here)
|
20 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=512)
|
21 |
+
outputs = model.generate(**inputs)
|
22 |
+
translated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
23 |
+
return json.dumps({"input_text": text, "translated_text": translated_text, "language": "iban-to-en"})
|
24 |
+
|
25 |
+
# Define the Gradio interface with a dropdown to select translation direction
|
26 |
+
iface = gr.Interface(
|
27 |
+
fn=translate_text,
|
28 |
+
inputs=[
|
29 |
+
gr.Textbox(label="Enter Text (English or Iban)"),
|
30 |
+
gr.Radio(["English to Iban", "Iban to English"], label="Select Translation Direction")
|
31 |
+
],
|
32 |
+
outputs=gr.JSON(),
|
33 |
+
title="English-Iban Translator",
|
34 |
+
description="This app translates between English and Iban. Choose the translation direction."
|
35 |
+
)
|
36 |
+
|
37 |
+
# Launch the app
|
38 |
+
iface.launch()
|