|
|
|
|
|
import streamlit as st |
|
|
from deep_translator import GoogleTranslator |
|
|
|
|
|
|
|
|
def translate_text(text, target_language): |
|
|
""" |
|
|
Translate text to the target language and provide its pronunciation. |
|
|
:param text: Input text to be translated. |
|
|
:param target_language: Language code ('en' for English, 'ur' for Urdu, 'zh-cn' for Chinese, etc.) |
|
|
:return: Translated text and pronunciation. |
|
|
""" |
|
|
try: |
|
|
|
|
|
translated = GoogleTranslator(source='auto', target=target_language).translate(text) |
|
|
|
|
|
|
|
|
pronunciation = translated |
|
|
return translated, pronunciation |
|
|
except Exception as e: |
|
|
return f"Error: {str(e)}", "" |
|
|
|
|
|
|
|
|
st.title("Language Translator and Pronunciation Tool ๐๐ฃ๏ธ") |
|
|
st.markdown(""" |
|
|
This app helps you translate text between different languages and provides a pronunciation guide. |
|
|
1. Select the translation mode (input and output language pair). |
|
|
2. Enter text to translate. |
|
|
3. View the translated text and pronunciation. |
|
|
""") |
|
|
|
|
|
|
|
|
language_options = { |
|
|
"English to Urdu ๐บ๐ธโก๏ธ๐ต๐ฐ": "ur", |
|
|
"Urdu to English ๐ต๐ฐโก๏ธ๐บ๐ธ": "en", |
|
|
"English to Chinese ๐บ๐ธโก๏ธ๐จ๐ณ": "zh-cn", |
|
|
"English to Italian ๐บ๐ธโก๏ธ๐ฎ๐น": "it", |
|
|
"English to German ๐บ๐ธโก๏ธ๐ฉ๐ช": "de", |
|
|
"English to Japanese ๐บ๐ธโก๏ธ๐ฏ๐ต": "ja", |
|
|
"English to Korean ๐บ๐ธโก๏ธ๐ฐ๐ท": "ko" |
|
|
} |
|
|
|
|
|
|
|
|
choice = st.selectbox("Select translation mode", options=list(language_options.keys())) |
|
|
|
|
|
|
|
|
text = st.text_area("Enter text to translate:", placeholder="Type your text here...") |
|
|
|
|
|
|
|
|
if st.button("Translate"): |
|
|
if not text.strip(): |
|
|
st.warning("Please enter text to translate!") |
|
|
else: |
|
|
target_language = language_options.get(choice, None) |
|
|
if target_language: |
|
|
translated_text, pronunciation = translate_text(text, target_language) |
|
|
st.subheader("Translated Text") |
|
|
st.success(translated_text) |
|
|
st.subheader("Pronunciation") |
|
|
st.info(pronunciation) |
|
|
else: |
|
|
st.error("Invalid translation mode selected!") |
|
|
|