| import gradio as gr |
| from transformers import AutoTokenizer, AutoModelForSeq2SeqLM |
| import torch |
|
|
| MODEL_ID = "Camayli/practica8" |
|
|
| tokenizer = AutoTokenizer.from_pretrained(MODEL_ID) |
| model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_ID) |
|
|
| device = "cuda" if torch.cuda.is_available() else "cpu" |
| model = model.to(device) |
|
|
| def traducir(texto): |
| inputs = tokenizer( |
| [texto], |
| return_tensors="pt", |
| truncation=True, |
| padding=True, |
| max_length=128 |
| ).to(device) |
|
|
| with torch.no_grad(): |
| outputs = model.generate( |
| **inputs, |
| max_new_tokens=64, |
| num_beams=4 |
| ) |
|
|
| return tokenizer.batch_decode(outputs, skip_special_tokens=True)[0] |
|
|
| demo = gr.Interface( |
| fn=traducir, |
| inputs=gr.Textbox(lines=4, label="Texto en inglés"), |
| outputs=gr.Textbox(lines=4, label="Traducción"), |
| title="Práctica 8 - Traductor EN → ES", |
| description="Space desplegado en Hugging Face usando Gradio." |
| ) |
|
|
| if __name__ == "__main__": |
| demo.launch() |