mishiawan commited on
Commit
76a3539
ยท
verified ยท
1 Parent(s): 8c76070

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -0
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!")