File size: 2,654 Bytes
6554823
 
 
 
 
 
 
9b4807e
6554823
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9b4807e
 
6554823
 
 
9b4807e
 
6554823
 
 
 
 
 
 
9b4807e
6554823
 
 
 
 
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import streamlit as st
from transformers import MarianMTModel, MarianTokenizer

# App Title and Description
st.title("🌐 Universal Language Translator App")
st.write("""
Translate text from any language to any other language using an open-source multilingual model.  
This app supports a wide range of languages with ease of use.
""")

# Instructions
st.sidebar.header("πŸ“ Instructions")
st.sidebar.write("""
1. Enter the text you want to translate in the input box.
2. Select the source language of the input text.
3. Choose the target language for translation.
4. Click the "Translate" button to see the translated text.
""")

# Supported Languages
languages = {
    "English": "en",
    "Spanish": "es",
    "French": "fr",
    "German": "de",
    "Chinese": "zh",
    "Hindi": "hi",
    "Arabic": "ar",
    "Russian": "ru",
    "Italian": "it",
    "Portuguese": "pt",
    "Japanese": "ja",
    "Korean": "ko",
    "Dutch": "nl",
    "Bengali": "bn",
    "Turkish": "tr",
    "Urdu": "ur",
    "Greek": "el",
    "Polish": "pl",
    "Thai": "th",
    "Vietnamese": "vi",
    "Hebrew": "he",
    "Swahili": "sw",
    "Amharic": "am",
    "Tamil": "ta",
    "Telugu": "te",
    "Punjabi": "pa",
    "Malayalam": "ml",
}

# UI for Input and Language Selection
source_text = st.text_area("Enter text to translate:", height=150, placeholder="Type here...")
source_language = st.selectbox("Source Language:", options=list(languages.keys()), index=0)
target_language = st.selectbox("Target Language:", options=list(languages.keys()), index=1)

if st.button("Translate"):
    if source_text.strip() == "":
        st.error("Please enter some text to translate.")
    else:
        try:
            # Universal Model for Language Translation
            model_name = f"Helsinki-NLP/opus-mt-mul-mul"
            tokenizer = MarianTokenizer.from_pretrained(model_name)
            model = MarianMTModel.from_pretrained(model_name)
            
            # Translation
            inputs = tokenizer(f">>{languages[target_language]}<< {source_text}", return_tensors="pt", padding=True, truncation=True)
            translated_tokens = model.generate(**inputs)
            translated_text = tokenizer.decode(translated_tokens[0], skip_special_tokens=True)
            
            # Display Translated Text
            st.subheader("πŸ”„ Translated Text:")
            st.text_area("Translation Output:", value=translated_text, height=150)
        except Exception as e:
            st.error("Translation failed. Ensure the text and languages are valid.")
            st.error(str(e))

# Footer
st.write("---")
st.markdown("πŸ’‘ **Developed by Abdullah**")