rapacious's picture
Create app.py
a26d575 verified
raw
history blame contribute delete
965 Bytes
import gradio as gr
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
import torch
# Load model and tokenizer
model_name = "VietAI/envit5-translation"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
def translate(text, source_lang):
"""Translate text based on the source language."""
input_text = f"{source_lang}: {text}"
inputs = tokenizer(input_text, return_tensors="pt", padding=True).input_ids.to('cpu')
outputs = model.generate(inputs, max_length=512)
return tokenizer.decode(outputs[0], skip_special_tokens=True)
# Create UI
demo = gr.Interface(
fn=translate,
inputs=[gr.Textbox(label="Input Text"), gr.Radio(["vi", "en"], label="Source Language")],
outputs=gr.Textbox(label="Translated Text"),
title="VietAI Translation",
description="Translate between Vietnamese and English using envit5-translation model."
)
# Launch app
demo.launch()