File size: 856 Bytes
0f80043
167f186
0f80043
167f186
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from transformers import T5ForTranslation, T5Tokenizer

# Load the pre-trained model and tokenizer
model = T5ForTranslation.from_pretrained("Helsinki-NLP/opus-mt-en-ro")
tokenizer = T5Tokenizer.from_pretrained("Helsinki-NLP/opus-mt-en-ro")

# Create the app layout
st.title("Text Translation App")
text_input = st.text_input("Enter text to translate:")
submit_button = st.button("Translate")
translated_text = st.text("")

# Handle the submit button click
if submit_button:
    # Encode the input text
    encoded = tokenizer(text_input, return_tensors="pt")

    # Perform translation
    translated = model.generate(**encoded)

    # Decode the translated text
    translated_text = tokenizer.batch_decode(translated, skip_special_tokens=True)

    # Display the translated text
    st.write("Translated Text:", translated_text[0])