File size: 1,659 Bytes
0f80043
c1b6fa4
3ddb276
c1b6fa4
167f186
 
e1dc136
 
497174a
 
fc95630
497174a
ee74465
 
61e85d4
497174a
ee74465
 
61e85d4
3ddb276
 
497174a
167f186
 
 
 
 
 
c1b6fa4
93b3d67
e1dc136
167f186
 
c1b6fa4
167f186
 
c1b6fa4
167f186
 
45313cb
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
36
37
38
39
40
import streamlit as st
from transformers import T5Tokenizer, T5ForConditionalGeneration

# tokenizer = T5Tokenizer.from_pretrained("Helsinki-NLP/opus-mt-en-ro")

# Create the app layout
st.title("Text Translation")
input_text = st.text_input("Enter text to translate:")
# Create a list of options for the select box
options = ["English", "Romanian", "German", "French", "Spanish"]
models = ["t5-base", "t5-small", "opus-mt-en-ro"]
# Create the select box
sselected_language = st.selectbox("Select a source language:", options)
tselected_language = st.selectbox("Select a target language:", options)
model_name = st.selectbox("Select a model:", models)
# Display the selected language
st.session_state["slanguage_selector"] = sselected_language
st.session_state["tlanguage_selector"] = tselected_language
st.session_state["model_name"] = model_name
tokenizer = T5Tokenizer.from_pretrained(model_name)
model = T5ForConditionalGeneration.from_pretrained(model_name)
st.write("Selected language combination:", selected_language, ' - ', selected_language)
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")
    
    input_ids = tokenizer.encode(f'Translate {sselected_language} to {tselected_language}: {input_text}', return_tensors='pt')

    # Perform translation
    output_ids = model.generate(input_ids)

    # Decode the translated text
    translated_text = tokenizer.decode(output_ids[0], skip_special_tokens=True)

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