Langify / app.py
mishiawan's picture
Create app.py
76a3539 verified
# Import necessary libraries
import streamlit as st
from deep_translator import GoogleTranslator
# Function to translate text and provide pronunciation
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:
# Translate the text using deep-translator
translated = GoogleTranslator(source='auto', target=target_language).translate(text)
# Provide a placeholder for pronunciation
pronunciation = translated # For simplicity, use translated text as pronunciation
return translated, pronunciation
except Exception as e:
return f"Error: {str(e)}", ""
# App UI
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.
""")
# Translation modes
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"
}
# Select translation mode
choice = st.selectbox("Select translation mode", options=list(language_options.keys()))
# Text input
text = st.text_area("Enter text to translate:", placeholder="Type your text here...")
# Translate button
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!")