File size: 2,009 Bytes
659981a
44b8c4f
ec09ed1
f577737
44b8c4f
659981a
83fef69
ad9e152
 
83fef69
ad9e152
83fef69
ad9e152
83fef69
44b8c4f
 
 
 
ad9e152
 
 
 
0fbc2ce
ec09ed1
ad9e152
225c9d9
44b8c4f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import time
from transformers import pipeline

st.title("Enhanced Multilingual Translator Chatbot")

# Choose the translation models from Hugging Face
translation_models = {
    "English to German": "Helsinki-NLP/opus-mt-en-de",
    "German to English": "Helsinki-NLP/opus-mt-de-en",
    "English to French": "Helsinki-NLP/opus-mt-en-fr",
    "French to English": "Helsinki-NLP/opus-mt-fr-en",
    "English to Urdu": "Helsinki-NLP/opus-mt-en-ur",
    "Urdu to English": "Helsinki-NLP/opus-mt-ur-en",
    "English to Spanish": "Helsinki-NLP/opus-mt-en-es",
    "Spanish to English": "Helsinki-NLP/opus-mt-es-en",
    "English to Chinese": "Helsinki-NLP/opus-mt-en-zh",
    "Chinese to English": "Helsinki-NLP/opus-mt-zh-en",
    # Add more language pairs as needed
}

selected_translation = st.selectbox("Select translation model", list(translation_models.keys()))

# Load the translation pipeline
translator = pipeline(task="translation", model=translation_models[selected_translation])

# User input for translation
user_input = st.text_area("Enter text for translation:", "")

# Display loading indicator
if st.button("Translate"):
    with st.spinner("Translating..."):
        # Simulate translation delay for demonstration
        time.sleep(2)
        if user_input:
            # Perform translation
            translated_text = translator(user_input, max_length=500)[0]['translation_text']
            st.success(f"Translated Text: {translated_text}")
        else:
            st.warning("Please enter text for translation.")

# Clear button to reset input and result
if st.button("Clear"):
    user_input = ""
    st.success("Input cleared.")
    st.empty()  # Clear previous results if any

st.markdown("---")

st.subheader("About")
st.write(
    "This is an enhanced Multilingual Translator chatbot that uses the Hugging Face Transformers library."
)
st.write(
    "Select a translation model from the dropdown, enter text, and click 'Translate' to see the translation."
)