Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
|
|
| 1 |
from transformers import MBartForConditionalGeneration, MBart50TokenizerFast
|
| 2 |
import torch
|
| 3 |
|
| 4 |
-
#
|
| 5 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 6 |
|
| 7 |
# Cargar modelo y tokenizer
|
|
@@ -9,18 +10,45 @@ model_name = "facebook/mbart-large-50"
|
|
| 9 |
tokenizer = MBart50TokenizerFast.from_pretrained(model_name)
|
| 10 |
model = MBartForConditionalGeneration.from_pretrained(model_name).to(device)
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
"""
|
| 14 |
-
Traduce texto
|
| 15 |
-
idioma_destino: código de idioma HuggingFace (ej: 'es_XX', 'en_XX', 'fr_XX')
|
| 16 |
"""
|
| 17 |
-
|
|
|
|
| 18 |
|
|
|
|
| 19 |
encoded = tokenizer(texto, return_tensors="pt").to(device)
|
| 20 |
-
generated_tokens = model.generate(
|
|
|
|
|
|
|
|
|
|
| 21 |
traduccion = tokenizer.batch_decode(generated_tokens, skip_special_tokens=True)[0]
|
| 22 |
return traduccion
|
| 23 |
|
| 24 |
-
#
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
from transformers import MBartForConditionalGeneration, MBart50TokenizerFast
|
| 3 |
import torch
|
| 4 |
|
| 5 |
+
# Dispositivo: GPU si está disponible, sino CPU
|
| 6 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 7 |
|
| 8 |
# Cargar modelo y tokenizer
|
|
|
|
| 10 |
tokenizer = MBart50TokenizerFast.from_pretrained(model_name)
|
| 11 |
model = MBartForConditionalGeneration.from_pretrained(model_name).to(device)
|
| 12 |
|
| 13 |
+
# Lista de idiomas disponibles
|
| 14 |
+
idiomas = {
|
| 15 |
+
"Inglés": "en_XX",
|
| 16 |
+
"Español": "es_XX",
|
| 17 |
+
"Francés": "fr_XX",
|
| 18 |
+
"Alemán": "de_XX",
|
| 19 |
+
"Ruso": "ru_RU",
|
| 20 |
+
"Chino": "zh_CN",
|
| 21 |
+
"Árabe": "ar_AR"
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
def traducir(texto, idioma_origen, idioma_destino):
|
| 25 |
"""
|
| 26 |
+
Traduce texto de un idioma a otro usando mBART-50.
|
|
|
|
| 27 |
"""
|
| 28 |
+
if not texto.strip():
|
| 29 |
+
return "Por favor ingresa algún texto."
|
| 30 |
|
| 31 |
+
tokenizer.src_lang = idioma_origen
|
| 32 |
encoded = tokenizer(texto, return_tensors="pt").to(device)
|
| 33 |
+
generated_tokens = model.generate(
|
| 34 |
+
**encoded,
|
| 35 |
+
forced_bos_token_id=tokenizer.lang_code_to_id[idioma_destino]
|
| 36 |
+
)
|
| 37 |
traduccion = tokenizer.batch_decode(generated_tokens, skip_special_tokens=True)[0]
|
| 38 |
return traduccion
|
| 39 |
|
| 40 |
+
# Crear interfaz Gradio
|
| 41 |
+
iface = gr.Interface(
|
| 42 |
+
fn=traducir,
|
| 43 |
+
inputs=[
|
| 44 |
+
gr.Textbox(label="Texto a traducir", placeholder="Escribe aquí tu texto...", lines=4),
|
| 45 |
+
gr.Dropdown(label="Idioma de origen", choices=list(idiomas.values()), value="en_XX"),
|
| 46 |
+
gr.Dropdown(label="Idioma de destino", choices=list(idiomas.values()), value="es_XX")
|
| 47 |
+
],
|
| 48 |
+
outputs=gr.Textbox(label="Traducción"),
|
| 49 |
+
title="Traductor mBART-50",
|
| 50 |
+
description="Traduce texto entre múltiples idiomas usando mBART-50."
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
# Ejecutar interfaz
|
| 54 |
+
iface.launch()
|