Skip to content

Commit

Permalink
📃: Added TextToTalk - A text to speech project (#318)
Browse files Browse the repository at this point in the history
# Pull Request for PyVerse 💡


## Issue Title : 📃: Adding Text to Speech Machine Learning project in
the Machine_Learning directory #303

- **Info about the related issue (Aim of the project)** : To get to know
the T2S
- **Name:** Tejas
- **GitHub ID:**  @LitZeus
- **Email ID:** [email protected]
- **Idenitfy yourself:** Participating as a `GSSoc-extd | Contributor` ,
`Hacktoberfest`


Closes: #303 

### Added a Text to Speech converter 

Introducing a Streamlit web app that enables users to upload a PDF file
and convert its text content into an MP3 audio file. It uses
pdfminer.six for extracting text and gtts (Google Text-to-Speech) for
audio conversion, providing a simple interface to make written content
accessible through speech.

## Type of change ☑️

What sort of change have you made:
<!--
Example how to mark a checkbox:-
- [x] My code follows the code style of this project.
-->
- [ ] Bug fix (non-breaking change which fixes an issue)
- [x] New feature (non-breaking change which adds functionality)
- [ ] Code style update (formatting, local variables)
- [ ] Breaking change (fix or feature that would cause existing
functionality to not work as expected)
- [ ] This change requires a documentation update

## How Has This Been Tested? ⚙️

It has been tested by me in my local machine and also hosted on
Streamlit without any errors and conflicts

## Checklist: ☑️
<!--
Example how to mark a checkbox:-
- [x] My code follows the code style of this project.
-->
- [x] My code follows the guidelines of this project.
- [x] I have performed a self-review of my own code.
- [x] I have commented my code, particularly wherever it was hard to
understand.
- [x] I have made corresponding changes to the documentation.
- [x] My changes generate no new warnings.
- [x] I have added things that prove my fix is effective or that my
feature works.
- [x] Any dependent changes have been merged and published in downstream
modules.
  • Loading branch information
UTSAVS26 authored Oct 10, 2024
2 parents 45d57d8 + 285ad10 commit 4e6d0fc
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 0 deletions.
21 changes: 21 additions & 0 deletions Machine_Learning/TextToTalk/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Tejas Athalye

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
70 changes: 70 additions & 0 deletions Machine_Learning/TextToTalk/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# TextToTalk: A PDF to MP3 Converter Web App

This is a Streamlit-based web application that allows users to upload a PDF file and convert its text content to an MP3 audio file. The application uses `pdfminer.six` for extracting text from the PDF and `gtts` (Google Text-to-Speech) for converting text to speech.

## Features

- Upload a PDF file using a drag-and-drop interface or a file upload button.
- Select the desired language for text-to-speech conversion.
- Convert the text content of the PDF to an MP3 audio file.
- Play the generated audio file directly on the web app.
- Download the generated audio file.

## Requirements

- `Python 3.6 or higher`
- `pdfminer.six`
- `gtts`
- `streamlit`

## Installation

#### First Fork the repository and then follow the steps given below!

1. Clone the repository in your local machine:
```sh
git clone https://github.com/<your-username>/PyVerse.git
cd Machine_Learning/TextToTalk
```

2. Create a virtual environment:
```sh
python -m venv venv
source venv/bin/activate # On Windows, use `venv\Scripts\activate`
```

3. Install the required packages:
```sh
pip install -r requirements.txt
```

## Usage

1. Run the Streamlit app:
```sh
cd scripts
streamlit run app.py
```

2. Open your web browser and go to `http://localhost:8501` to access the app.

3. Upload a PDF file using the provided upload button or drag and drop the file into the designated area.

4. Select the desired language for the text-to-speech conversion.

5. The app will extract the text from the uploaded PDF, convert it to speech, and display an audio player for you to listen to the generated MP3 file.

6. You can also download the generated MP3 file using the download button.

## File Structure
- `ExtText.py`: Contains the function for extracting text from the uploaded PDF file using pdfminer.six.
- `TTS.py`: Contains the function for converting text to speech using gtts.
- `Pipeline.py`: Integrates the text extraction and text-to-speech conversion functions into a single pipeline.
- `app.py`: The main Streamlit app that provides the web interface for the PDF to MP3 conversion.

## Contributing
Contributions are welcome! If you find any bugs or have suggestions for improvements, please open an issue or create a pull request.

## License
This project is licensed under the MIT License. See the `LICENSE` file for more details.

3 changes: 3 additions & 0 deletions Machine_Learning/TextToTalk/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pdfminer.six
gtts
streamlit
17 changes: 17 additions & 0 deletions Machine_Learning/TextToTalk/scripts/ExtText.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from io import BytesIO
from pdfminer.high_level import extract_text_to_fp
from pdfminer.layout import LAParams

def extract_text(uploaded_file):
"""Extracts text from the uploaded PDF file using pdfminer.six."""
if not uploaded_file:
return None

try:
output_string = BytesIO()
laparams = LAParams()
extract_text_to_fp(uploaded_file, output_string, laparams=laparams)
return output_string.getvalue().decode('utf-8')
except Exception as e:
print(f"Error extracting text: {e}")
return None
10 changes: 10 additions & 0 deletions Machine_Learning/TextToTalk/scripts/Pipeline.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from ExtText import extract_text
from TTS import text_to_speech

def pipeline(uploaded_file, lang='en'):
"""Extracts text and converts it to speech in a pipeline."""
extracted_text = extract_text(uploaded_file)
if extracted_text:
return text_to_speech(extracted_text, lang=lang)
else:
return None
17 changes: 17 additions & 0 deletions Machine_Learning/TextToTalk/scripts/TTS.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from gtts import gTTS
from io import BytesIO

def text_to_speech(text, lang='en'):
"""Converts the extracted text to audio (MP3) using gTTS."""
if not text:
return None

try:
tts = gTTS(text=text, lang=lang)
audio_file = BytesIO()
tts.write_to_fp(audio_file)
audio_file.seek(0)
return audio_file
except Exception as e:
print(f"Error generating audio: {e}")
return None
24 changes: 24 additions & 0 deletions Machine_Learning/TextToTalk/scripts/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import streamlit as st
from Pipeline import pipeline

def main():
"""Streamlit app for PDF to MP3 conversion."""
st.title("PDF to MP3 Converter")

uploaded_file = st.file_uploader("Choose a PDF file to convert:", type=['pdf'])

if uploaded_file is not None:
audio_file = pipeline(uploaded_file, lang=lang_code)
if audio_file:
st.audio(audio_file, format='audio/mp3')
st.download_button(
label="Download Audio",
data=audio_file,
file_name="output.mp3",
mime="audio/mp3"
)
else:
st.error("Failed to convert PDF to audio.")

if __name__ == '__main__':
main()

0 comments on commit 4e6d0fc

Please sign in to comment.