File size: 2,016 Bytes
ce1f204
334c0b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ce1f204
 
 
 
 
334c0b0
 
 
 
ce1f204
 
334c0b0
 
ce1f204
334c0b0
 
ce1f204
334c0b0
 
ce1f204
 
334c0b0
 
 
 
 
 
 
ce1f204
 
334c0b0
 
 
 
9e31db4
 
 
 
 
 
 
 
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
import streamlit as st
from transformers import MarianMTModel, MarianTokenizer

# Define a dictionary to map language names to model identifiers
models = {
    'French': 'Helsinki-NLP/opus-mt-en-fr',
    'Spanish': 'Helsinki-NLP/opus-mt-en-es',
    'German': 'Helsinki-NLP/opus-mt-en-de',
    'Italian': 'Helsinki-NLP/opus-mt-en-it',
    'Urdu': 'Helsinki-NLP/opus-mt-en-ur',
    'Arabic': 'Helsinki-NLP/opus-mt-en-ar', 
    # Add more language models if needed
}

def load_model(model_name):
    """Load the model and tokenizer based on the selected model name."""
    model = MarianMTModel.from_pretrained(model_name)
    tokenizer = MarianTokenizer.from_pretrained(model_name)
    return model, tokenizer

def translate_text(text, model, tokenizer):
    """Translate text using the provided model and tokenizer."""
    inputs = tokenizer.encode(text, return_tensors="pt")
    translated = model.generate(inputs)
    translated_text = tokenizer.decode(translated[0], skip_special_tokens=True)
    return translated_text

def main():
    st.title("Multilingual Translator")

    # User input for English text
    text_to_translate = st.text_area("Enter text in English:")

    # Language options
    selected_language = st.selectbox("Select target language:", list(models.keys()))

    if st.button("Translate"):
        if text_to_translate:
            # Load the selected model
            model_name = models[selected_language]
            model, tokenizer = load_model(model_name)
            
            translated_text = translate_text(text_to_translate, model, tokenizer)
            st.write(f"**Translation in {selected_language}:**")
            st.write(translated_text)
        else:
            st.warning("Please enter text to translate.")

if __name__ == "__main__":
    main()
    try:
    import transformers
    from transformers import MarianMTModel, MarianTokenizer
    print("Transformers imported successfully")
except ImportError as e:
    print(f"ImportError: {e}")
python test_import.py