Rahul-Crudcook commited on
Commit
00f74ec
·
verified ·
1 Parent(s): 954c19c

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -0
app.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+