Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Import necessary libraries
|
| 2 |
+
import streamlit as st
|
| 3 |
+
from deep_translator import GoogleTranslator
|
| 4 |
+
|
| 5 |
+
# Function to translate text and provide pronunciation
|
| 6 |
+
def translate_text(text, target_language):
|
| 7 |
+
"""
|
| 8 |
+
Translate text to the target language and provide its pronunciation.
|
| 9 |
+
:param text: Input text to be translated.
|
| 10 |
+
:param target_language: Language code ('en' for English, 'ur' for Urdu, 'zh-cn' for Chinese, etc.)
|
| 11 |
+
:return: Translated text and pronunciation.
|
| 12 |
+
"""
|
| 13 |
+
try:
|
| 14 |
+
# Translate the text using deep-translator
|
| 15 |
+
translated = GoogleTranslator(source='auto', target=target_language).translate(text)
|
| 16 |
+
|
| 17 |
+
# Provide a placeholder for pronunciation
|
| 18 |
+
pronunciation = translated # For simplicity, use translated text as pronunciation
|
| 19 |
+
return translated, pronunciation
|
| 20 |
+
except Exception as e:
|
| 21 |
+
return f"Error: {str(e)}", ""
|
| 22 |
+
|
| 23 |
+
# App UI
|
| 24 |
+
st.title("Language Translator and Pronunciation Tool ๐๐ฃ๏ธ")
|
| 25 |
+
st.markdown("""
|
| 26 |
+
This app helps you translate text between different languages and provides a pronunciation guide.
|
| 27 |
+
1. Select the translation mode (input and output language pair).
|
| 28 |
+
2. Enter text to translate.
|
| 29 |
+
3. View the translated text and pronunciation.
|
| 30 |
+
""")
|
| 31 |
+
|
| 32 |
+
# Translation modes
|
| 33 |
+
language_options = {
|
| 34 |
+
"English to Urdu ๐บ๐ธโก๏ธ๐ต๐ฐ": "ur",
|
| 35 |
+
"Urdu to English ๐ต๐ฐโก๏ธ๐บ๐ธ": "en",
|
| 36 |
+
"English to Chinese ๐บ๐ธโก๏ธ๐จ๐ณ": "zh-cn",
|
| 37 |
+
"English to Italian ๐บ๐ธโก๏ธ๐ฎ๐น": "it",
|
| 38 |
+
"English to German ๐บ๐ธโก๏ธ๐ฉ๐ช": "de",
|
| 39 |
+
"English to Japanese ๐บ๐ธโก๏ธ๐ฏ๐ต": "ja",
|
| 40 |
+
"English to Korean ๐บ๐ธโก๏ธ๐ฐ๐ท": "ko"
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
# Select translation mode
|
| 44 |
+
choice = st.selectbox("Select translation mode", options=list(language_options.keys()))
|
| 45 |
+
|
| 46 |
+
# Text input
|
| 47 |
+
text = st.text_area("Enter text to translate:", placeholder="Type your text here...")
|
| 48 |
+
|
| 49 |
+
# Translate button
|
| 50 |
+
if st.button("Translate"):
|
| 51 |
+
if not text.strip():
|
| 52 |
+
st.warning("Please enter text to translate!")
|
| 53 |
+
else:
|
| 54 |
+
target_language = language_options.get(choice, None)
|
| 55 |
+
if target_language:
|
| 56 |
+
translated_text, pronunciation = translate_text(text, target_language)
|
| 57 |
+
st.subheader("Translated Text")
|
| 58 |
+
st.success(translated_text)
|
| 59 |
+
st.subheader("Pronunciation")
|
| 60 |
+
st.info(pronunciation)
|
| 61 |
+
else:
|
| 62 |
+
st.error("Invalid translation mode selected!")
|