File size: 2,825 Bytes
6554823
2f2de3b
6554823
 
 
 
2f2de3b
 
6554823
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2f2de3b
 
 
 
6554823
2f2de3b
 
 
 
 
 
 
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
82
83
import streamlit as st
from transformers import M2M100ForConditionalGeneration, M2M100Tokenizer

# App Title and Description
st.title("🌐 Universal Language Translator App")
st.write("""
Translate text from any language to any other language using the open-source M2M100 multilingual model.  
This app supports over 100 languages and provides a seamless translation experience.
""")

# 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",
    "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:
            # Load M2M100 model and tokenizer
            model_name = "facebook/m2m100_418M"
            tokenizer = M2M100Tokenizer.from_pretrained(model_name)
            model = M2M100ForConditionalGeneration.from_pretrained(model_name)
            
            # Set source and target language
            tokenizer.src_lang = languages[source_language]
            encoded_text = tokenizer(source_text, return_tensors="pt")
            generated_tokens = model.generate(**encoded_text, forced_bos_token_id=tokenizer.get_lang_id(languages[target_language]))
            
            # Decode the translated text
            translated_text = tokenizer.decode(generated_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**")