File size: 2,900 Bytes
c80826b
 
 
 
 
 
 
ffb27c5
61424c2
c80826b
 
 
 
 
 
 
 
 
27a6bb9
 
 
 
 
 
702a696
27a6bb9
 
c80826b
 
27a6bb9
 
c80826b
 
27a6bb9
61424c2
51c214a
61424c2
27a6bb9
 
 
 
c80826b
 
 
 
27a6bb9
953b6df
61424c2
27a6bb9
 
 
 
c80826b
 
 
27a6bb9
 
c80826b
27a6bb9
 
 
 
 
 
 
 
 
c80826b
 
 
 
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
# app.py
import streamlit as st
from transformers import pipeline
from gtts import gTTS
from io import BytesIO

# Title and Introduction
st.title("Multilingual Language Learning App")
st.write("Translate text, correct grammar, and practice pronunciation in various languages!")

# Sidebar Options
st.sidebar.header("Choose an Operation")
options = ["Translate Text", "Correct Grammar", "Practice Pronunciation"]
operation = st.sidebar.selectbox("Operation", options)

# Input Text
text = st.text_area("Enter your text here:")

# Supported Languages
languages = {
    "English": "en", "French": "fr", "Spanish": "es", "German": "de", "Chinese": "zh", "Japanese": "ja",
    "Arabic": "ar", "Russian": "ru", "Hindi": "hi", "Urdu": "ur", "Portuguese": "pt", "Italian": "it",
    "Dutch": "nl", "Korean": "ko", "Swedish": "sv", "Turkish": "tr", "Polish": "pl", "Danish": "da",
    "Finnish": "fi", "Norwegian": "no", "Greek": "el", "Hebrew": "he", "Thai": "th", "Vietnamese": "vi",
    "Czech": "cs", "Hungarian": "hu", "Indonesian": "id", "Malay": "ms", "Filipino": "tl", "Swahili": "sw",
}

# Translation Functionality
if operation == "Translate Text":
    source_language = st.selectbox("Select Source Language", list(languages.keys()))
    target_language = st.selectbox("Select Target Language", list(languages.keys()))

    if st.button("Translate"):
        try:
            model_name = f"Helsinki-NLP/opus-mt-{languages[source_language]}-{languages[target_language]}"
            translator = pipeline("translation", model=model_name)
            translation = translator(text, max_length=400)[0]["translation_text"]
            st.write("### Translation:")
            st.write(translation)
        except Exception as e:
            st.error(f"Translation failed: {e}")

# Grammar Correction Functionality
elif operation == "Correct Grammar":
    if st.button("Correct Grammar"):
        try:
            corrector = pipeline("text2text-generation", model="t5-base")
            corrected_text = corrector(f"grammar: {text}", max_length=400)[0]["generated_text"]
            st.write("### Corrected Text:")
            st.write(corrected_text)
        except Exception as e:
            st.error(f"Grammar correction failed: {e}")

# Pronunciation Practice Functionality
elif operation == "Practice Pronunciation":
    language = st.selectbox("Select Pronunciation Language", list(languages.keys()))

    if st.button("Generate Audio"):
        try:
            tts = gTTS(text=text, lang=languages[language])
            audio_file = BytesIO()
            tts.write_to_fp(audio_file)
            audio_file.seek(0)
            st.audio(audio_file, format="audio/mp3")
            st.write("### Listen to the pronunciation!")
        except Exception as e:
            st.error(f"Pronunciation generation failed: {e}")

# Prompt for Text Input
else:
    st.write("Please enter text to begin.")