Spaces:
Sleeping
Sleeping
File size: 935 Bytes
17822e0 55a67a5 17822e0 55a67a5 17822e0 55a67a5 17822e0 85c04da 17822e0 55a67a5 17822e0 55a67a5 17822e0 85c04da 17822e0 55a67a5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
import gradio as gr
from transformers import MarianMTModel, MarianTokenizer
# Load pre-trained MarianMT model and tokenizer for translation
model_name = "Helsinki-NLP/opus-mt-en-de"
model = MarianMTModel.from_pretrained(model_name)
tokenizer = MarianTokenizer.from_pretrained(model_name)
# Define the translation function
def translate_text(text):
# Tokenize the input text
inputs = tokenizer(text, return_tensors="pt")
# Perform translation
translation = model.generate(**inputs)
# Decode the translated text
translated_text = tokenizer.decode(translation[0], skip_special_tokens=True)
return translated_text
# Create Gradio interface
iface = gr.Interface(
fn=translate_text,
inputs=gr.Textbox(),
outputs=gr.Textbox(),
live=True,
title="Language Translation App",
description="Translate text from English to German using MarianMT.",
)
# Launch the Gradio app
iface.launch()
|