Nehal07 commited on
Commit
18e757e
1 Parent(s): 46d13a6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -42
app.py CHANGED
@@ -4,10 +4,10 @@ from gtts import gTTS
4
  import os
5
 
6
  # Streamlit app
7
- st.title("Text Translator")
8
 
9
  # Input text
10
- user_text = st.text_area("Enter the text you want to translate:")
11
 
12
  # Define a dictionary of language codes and their full names
13
  language_names = {
@@ -46,46 +46,21 @@ def translate_text(text, target_language_code):
46
  st.subheader("Select the target language for translation:")
47
  selected_target_language = st.selectbox("Select a target language:", list(language_names.keys()))
48
 
49
- # Display the translated text and audio as the user types
50
  if user_text:
51
  translated_text = translate_text(user_text, language_names[selected_target_language])
52
-
53
- # Display translated text with CSS
54
- st.markdown(f'<div style="background-color: #f0f0f0; padding: 10px; border-radius: 5px;">'
55
- f'<p style="font-size: 18px; color: #333;">Translated Text:</p>'
56
- f'<p style="font-size: 24px; color: #0066cc;">{translated_text}</p>'
57
- f'</div>', unsafe_allow_html=True)
58
-
59
- # Convert the translated text to audio
60
  tts = gTTS(translated_text, lang=language_names[selected_target_language])
61
- audio_file = "translated_audio.mp3"
62
- tts.save(audio_file)
63
-
64
- # Check if the audio file exists
65
- if os.path.exists(audio_file):
66
- # Display the audio
67
- st.audio(audio_file, format="audio/mp3")
68
-
69
- # Remove the temporary audio file
70
- os.remove(audio_file)
71
- else:
72
- st.warning("Audio file could not be generated.")
73
-
74
- # CSS for the "Speak" button
75
- speak_button_css = """
76
- <style>
77
- .st-ep::before {
78
- content: "\1F50A";
79
- font-size: 24px;
80
- margin-right: 8px;
81
- }
82
- </style>
83
- """
84
-
85
- # Speak button
86
- if st.button("Speak"):
87
- st.markdown(speak_button_css, unsafe_allow_html=True)
88
- if os.path.exists(audio_file):
89
- st.audio(audio_file, format="audio/mp3")
90
- else:
91
- st.warning("Audio file not found.")
 
4
  import os
5
 
6
  # Streamlit app
7
+ st.title("Text Translator with Voice")
8
 
9
  # Input text
10
+ user_text = st.text_input("Enter the text you want to translate:")
11
 
12
  # Define a dictionary of language codes and their full names
13
  language_names = {
 
46
  st.subheader("Select the target language for translation:")
47
  selected_target_language = st.selectbox("Select a target language:", list(language_names.keys()))
48
 
49
+ # Display the translated text and speak it
50
  if user_text:
51
  translated_text = translate_text(user_text, language_names[selected_target_language])
52
+
53
+ # Apply advanced CSS for better display in a bordered box
54
+ styled_text = f'<div style="border: 2px solid #0072B5; padding: 10px; background-color: #E5E5E5; border-radius: 5px; color: black;">{translated_text}</div>'
55
+ st.markdown(styled_text, unsafe_allow_html=True)
56
+
57
+ # Text-to-Speech (TTS) conversion and playback
 
 
58
  tts = gTTS(translated_text, lang=language_names[selected_target_language])
59
+ tts_file_path = "translated_audio.mp3"
60
+ tts.save(tts_file_path)
61
+
62
+ # Play the TTS audio
63
+ st.audio(tts_file_path, format="audio/mp3")
64
+
65
+ # Clean up the TTS audio file
66
+ os.remove(tts_file_path)