Rahul-Crudcook commited on
Commit
f932b29
·
verified ·
1 Parent(s): 5c66bdb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -41
app.py CHANGED
@@ -1,41 +1,40 @@
1
- import streamlit as st
2
- from gtts import gTTS
3
- import tempfile
4
- from googletrans import Translator
5
-
6
- # Title of the app
7
- st.title("Multilingual Text-to-Speech Converter")
8
-
9
- # User input text
10
- text_input = st.text_area("Enter the text you want to convert to speech:")
11
-
12
- # Language selection
13
- language_options = {"English": "en", "Hindi": "hi", "Spanish": "es", "French": "fr", "German": "de"}
14
- selected_language = st.selectbox("Select output language", options=language_options.keys())
15
- selected_lang_code = language_options[selected_language]
16
-
17
- # Generate audio if text is provided
18
- if st.button("Convert to Speech"):
19
- if text_input.strip() == "":
20
- st.warning("Please enter some text.")
21
- else:
22
- # Translate text to the selected language
23
- translator = Translator()
24
- translated_text = translator.translate(text_input, dest=selected_lang_code).text
25
-
26
- # Convert translated text to speech using gTTS
27
- tts = gTTS(text=translated_text, lang=selected_lang_code, slow=False)
28
-
29
- # Save the output to a temporary file
30
- with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
31
- tts.save(tmp_file.name)
32
- audio_file_path = tmp_file.name
33
-
34
- # Load and play the audio file in the app
35
- st.audio(audio_file_path, format="audio/mp3")
36
-
37
- # Provide download link
38
- st.success("Speech synthesis complete!")
39
- with open(audio_file_path, "rb") as file:
40
- st.download_button(label="Download MP3", data=file, file_name="translated_speech_output.mp3", mime="audio/mp3")
41
-
 
1
+ from deep_translator import GoogleTranslator
2
+ import streamlit as st
3
+ from gtts import gTTS
4
+ import tempfile
5
+
6
+ # Title of the app
7
+ st.title("Multilingual Text-to-Speech Converter")
8
+
9
+ # User input text
10
+ text_input = st.text_area("Enter the text you want to convert to speech:")
11
+
12
+ # Language selection
13
+ language_options = {"English": "en", "Hindi": "hi", "Spanish": "es", "French": "fr", "German": "de"}
14
+ selected_language = st.selectbox("Select output language", options=language_options.keys())
15
+ selected_lang_code = language_options[selected_language]
16
+
17
+ # Generate audio if text is provided
18
+ if st.button("Convert to Speech"):
19
+ if text_input.strip() == "":
20
+ st.warning("Please enter some text.")
21
+ else:
22
+ # Translate text to the selected language using deep-translator
23
+ translator = GoogleTranslator(source='auto', target=selected_lang_code)
24
+ translated_text = translator.translate(text_input)
25
+
26
+ # Convert translated text to speech using gTTS
27
+ tts = gTTS(text=translated_text, lang=selected_lang_code, slow=False)
28
+
29
+ # Save the output to a temporary file
30
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
31
+ tts.save(tmp_file.name)
32
+ audio_file_path = tmp_file.name
33
+
34
+ # Load and play the audio file in the app
35
+ st.audio(audio_file_path, format="audio/mp3")
36
+
37
+ # Provide download link
38
+ st.success("Speech synthesis complete!")
39
+ with open(audio_file_path, "rb") as file:
40
+ st.download_button(label="Download MP3", data=file, file_name="translated_speech_output.mp3", mime="audio/mp3")