-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
52 lines (40 loc) · 1.68 KB
/
app.py
File metadata and controls
52 lines (40 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# ...
from flask import Flask, render_template, request, send_file
from gtts import gTTS
import PyPDF2
import io
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
# Check if the post request has the file part
if 'file' not in request.files:
return render_template('index.html', error='No file part')
file = request.files['file']
# Check if the file is empty
if file.filename == '':
return render_template('index.html', error='No selected file')
# Check if the file is a PDF
if file and file.filename.endswith('.pdf'):
pdfreader = PyPDF2.PdfReader(file)
combined_text = ""
# Iterate through pages
for page_num in range(len(pdfreader.pages)):
text = pdfreader.pages[page_num].extract_text()
clean_text = text.strip().replace('\n', ' ')
combined_text += clean_text + " "
# Generate audio
speaker = gTTS(text=combined_text, lang='en', slow=False)
audio_file = io.BytesIO()
speaker.write_to_fp(audio_file)
audio_file.seek(0)
# Return the audio file for download
return send_file(io.BytesIO(audio_file.read()),
mimetype='audio/mpeg',
as_attachment=True,
download_name='output.mp3')
else:
return render_template('index.html', error='Invalid file format. Please upload a PDF file.')
return render_template('index.html', error=None)
if __name__ == '__main__':
app.run(debug=True)