Dddixyy's picture
Update app.py
e7a45f0 verified
raw
history blame
1.34 kB
import gradio as gr
import torch
from transformers import MarianMTModel, MarianTokenizer
# Load the MarianMT model and tokenizer
model_name = "Dddixyy/latin-italian-translator"
tokenizer = MarianTokenizer.from_pretrained(model_name)
model = MarianMTModel.from_pretrained(model_name)
# Translation function
def translate_latin_to_italian(latin_text):
# Truncate input to a maximum length of 512 tokens to avoid overload
inputs = tokenizer(latin_text, return_tensors="pt", padding=True, truncation=True, max_length=512)
# Use torch.no_grad() to speed up inference by not calculating gradients
with torch.no_grad():
generated_ids = model.generate(inputs["input_ids"])
# Decode the generated ids into a readable translation
translation = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)
return translation[0]
# Define the Gradio interface
interface = gr.Interface(
fn=translate_latin_to_italian,
inputs="text",
outputs="text",
title="Latin to Italian Translator",
description="Translate Latin sentences to Italian using a fine-tuned MarianMT model.",
examples=[["Amor vincit omnia."], ["Veni, vidi, vici."], ["Carpe diem."], ["Alea iacta est."]]
)
# Launch the app
if __name__ == "__main__":
interface.launch(server_name="0.0.0.0", server_port=7860)