Text to Speech Convertor using Python
Youtube Video: https://youtu.be/KXLQAKjs2yI
Introduction:
The Text to Speech Converter is a user-friendly web application that transforms written text into lifelike speech with just the click of a button. Powered by advanced technology, it offers seamless integration with popular programming libraries like Streamlit and GTTS (Google Text-to-Speech), ensuring high-quality audio output in multiple languages.
Project Prerequisites:
Here are some prerequisites for setting up and using the Text to Speech Converter project on your system:
- Python: Make sure you have Python installed on your system. You can download and install Python from the official website: https://www.python.org/downloads/
2. Streamlit: Streamlit is a Python library used for building interactive web applications. You can install it via pip:
pip install streamlit
3. gtts: gtts (Google Text-to-Speech) is a Python library for text-to-speech conversion using Google’s Text-to-Speech API. Install it using pip:
pip install gtts
4. Internet Connection: Ensure that you have an active internet connection, as the Text to Speech Converter relies on Google’s Text-to-Speech API for generating speech.
5. Text Editor or IDE: Use a text editor or an Integrated Development Environment (IDE) such as Visual Studio Code, PyCharm, or Jupyter Notebook to write and run your Python code.
Once you have these prerequisites installed and set up on your system, you should be ready to run the Text to Speech Converter project smoothly.
Getting Started:
Here's a step-by-step tutorial to get started with the Text to Speech Converter project:
Step 1: Install Python and Required Libraries
- If you haven't already, download and install Python from the official website: https://www.python.org/downloads/
- Open your command line interface (CLI) or terminal.
- Install Streamlit by running the following command:
- Install gtts (Google Text-to-Speech) library using pip:
Step 2: Create a New Python Script
- Open your preferred text editor or Integrated Development Environment (IDE).
- Create a new Python script file, e.g., text_to_speech_converter.py.
Step 3: Write the Code
- Copy and paste the following code into your Python script file:
import streamlit as st
from gtts import gTTS
from io import BytesIO
import base64
def text_to_speech(text, lang='en'):
tts = gTTS(text=text, lang=lang)
fp = BytesIO()
tts.write_to_fp(fp)
return fp
# Streamlit UI
st.title("Text to Speech Converter")
# Text input box
text_input = st.text_area("Enter text:")
# Language selection dropdown
languages = {'English': 'en', 'French': 'fr', 'German': 'de'} # Add more languages if needed
selected_language = st.selectbox("Select language:", list(languages.keys()))
if st.button("Convert to Speech"):
# Convert text to speech
lang_code = languages[selected_language]
speech_fp = text_to_speech(text_input, lang=lang_code)
# Play the speech
st.audio(speech_fp, format='audio/mpeg', start_time=0)
# Download link
b64 = base64.b64encode(speech_fp.getvalue()).decode()
href = f'<a href="data:audio/mpeg;base64,{b64}" download="speech.mp3">Download Audio</a>'
st.markdown(href, unsafe_allow_html=True)
Step 4: Run the Application
- Save the Python script file.
- Open your CLI or terminal and navigate to the directory where your Python script is saved.
- Run the following command to start the Streamlit application:
Step 5: Use the Text to Speech Converter
- Once the Streamlit application is running, you’ll see the input box where you can enter the text you want to convert to speech.
- Select the desired language from the dropdown menu.
- Click the “Convert to Speech” button.
- You’ll hear the generated speech and also have the option to download the audio file.
That’s it! You’ve successfully set up and used the Text to Speech Converter project. Feel free to customize the code or explore additional features to further enhance the project.
Output:
The output of the Text to Speech Converter project is an interactive web application where users can input text, select a language, and convert the text into speech. Here’s how the output looks when the Streamlit application is run:
1. Title: The application title, “Text to Speech Converter,” is displayed at the top of the page.
2. Text Input Box: Below the title, there is a text input box where users can enter the text they want to convert to speech.
3. Language Selection Dropdown: Next to the text input box, there is a dropdown menu labelled “Select language.” Users can choose the desired language for the speech output from the available options.
4. Convert to Speech Button: Beneath the language selection dropdown, there is a button labelled “Convert to Speech.” When clicked, this button triggers the text-to-speech conversion process.
5. Speech Audio Playback: After clicking the “Convert to Speech” button, the generated speech audio is played back to the user directly within the web application.
6. Download Link: Below the speech audio playback section, there is a download link labelled “Download Audio.” Users can click on this link to download the generated speech audio file (in MP3 format) to their local device.
The output provides a seamless and user-friendly experience for converting text to speech, making it accessible to users of all technical backgrounds. Users can easily input text, select their preferred language, listen to the generated speech, and download the audio file for offline use.
For more watch my tutorial video on YouTube channel: https://youtu.be/KXLQAKjs2yI
In that video I explained the code and given a brief of the project “ Text to Speech Convertor using Python”
If you like this and video then don’t forget to subscribe and follow my Instagram account: GalvanMoto
Join the conversation