File size: 1,360 Bytes
05e0d8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
41
42
43
import streamlit as st
from transformers import pipeline

# Load the translation pipeline from Hugging Face
@st.cache_resource
def load_translation_pipeline(model_name):
    return pipeline("translation", model=model_name)

# Dictionary of available models and target languages
models = {
    "French": "Helsinki-NLP/opus-mt-en-fr",
    "Spanish": "Helsinki-NLP/opus-mt-en-es",
    "German": "Helsinki-NLP/opus-mt-en-de",
    "Chinese": "Helsinki-NLP/opus-mt-en-zh",
    "Hindi": "Helsinki-NLP/opus-mt-en-hi",
}

# Streamlit app
def main():
    st.title("Language Translator")

    # Input text from the user
    text_to_translate = st.text_area("Enter the text in English:")

    # Select the target language
    target_language = st.selectbox("Select the target language:", list(models.keys()))

    # Translate button
    if st.button("Translate"):
        # Load the appropriate translation model
        translation_pipeline = load_translation_pipeline(models[target_language])

        # Translate the text
        if text_to_translate.strip():
            translated_text = translation_pipeline(text_to_translate)[0]['translation_text']
            st.success(f"Translated Text ({target_language}):")
            st.write(translated_text)
        else:
            st.error("Please enter some text to translate.")

if __name__ == "__main__":
    main()