Spaces:
Running
Running
import streamlit as st | |
from transformers import MarianMTModel, MarianTokenizer | |
# Title of the app | |
st.title('French to English Translator') | |
# Load the tokenizer and model | |
tokenizer = MarianTokenizer.from_pretrained('Helsinki-NLP/opus-mt-fr-en') | |
model = MarianMTModel.from_pretrained('Helsinki-NLP/opus-mt-fr-en') | |
# Text area for user input | |
user_input = st.text_area("Enter French text", "Bonjour le monde!") | |
# Function to translate text | |
def translate(text): | |
# Tokenize the text | |
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True, max_length=512) | |
# Generate translation outputs | |
translated = model.generate(**inputs) | |
# Decode the translated text | |
translated_text = tokenizer.batch_decode(translated, skip_special_tokens=True)[0] | |
return translated_text | |
# Button to perform translation | |
if st.button('Translate'): | |
translation = translate(user_input) | |
st.write('English Translation:', translation) | |