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()