pradeep4321 commited on
Commit
36d7981
·
verified ·
1 Parent(s): ab8c1ea

Update src/app.py

Browse files
Files changed (1) hide show
  1. src/app.py +41 -46
src/app.py CHANGED
@@ -1,21 +1,14 @@
1
  import os
2
- os.environ["HOME"] = "/tmp" # Fix: Redirect .streamlit to a writable path
3
-
4
  import tempfile
5
- import pyttsx3
6
  from docx import Document
7
  from PyPDF2 import PdfReader
8
  import streamlit as st
9
  import base64
10
- from io import BytesIO
11
 
12
  def text_to_speech(text, output_file):
13
- engine = pyttsx3.init()
14
- engine.setProperty('rate', 150)
15
- voices = engine.getProperty('voices')
16
- engine.setProperty('voice', voices[1].id)
17
- engine.save_to_file(text, output_file)
18
- engine.runAndWait()
19
 
20
  def convert_docx_to_text(docx_file):
21
  doc = Document(docx_file)
@@ -28,55 +21,57 @@ def convert_pdf_to_text(pdf_file):
28
  text = ""
29
  pdf_reader = PdfReader(pdf_file)
30
  for page in pdf_reader.pages:
31
- text += page.extract_text()
 
 
32
  return text
33
 
 
 
 
 
 
 
34
  def main():
35
  st.title("Text to Speech Converter")
36
-
37
  uploaded_file = st.file_uploader("Upload a text, docx, or pdf file", type=["txt", "docx", "pdf"])
38
-
39
  if uploaded_file:
40
  try:
41
- # Save uploaded file content to a temporary file
42
- temp_file = tempfile.NamedTemporaryFile(delete=False)
43
- temp_file.write(uploaded_file.read())
44
- temp_file.close()
 
45
 
46
- file_extension = uploaded_file.name.split('.')[-1]
47
-
48
- if file_extension.lower() == 'txt':
49
- with open(temp_file.name, 'r', encoding='utf-8') as txt_file:
50
- text = txt_file.read()
51
- elif file_extension.lower() == 'docx':
52
- text = convert_docx_to_text(temp_file.name)
53
- elif file_extension.lower() == 'pdf':
54
- text = convert_pdf_to_text(temp_file.name)
55
  else:
56
  st.error("Unsupported file format")
57
  return
58
-
 
 
 
 
59
  with st.spinner("Converting text to speech..."):
60
- output_audio_file = "output.mp3"
61
  text_to_speech(text, output_audio_file)
62
-
63
- st.audio(output_audio_file, format="audio/mp3", start_time=0)
64
-
65
- # Provide a download link for the audio file
66
- with open(output_audio_file, 'rb') as audio_file:
67
- audio_bytes = audio_file.read()
68
- st.markdown(get_binary_file_downloader_html(audio_bytes, output_audio_file), unsafe_allow_html=True)
69
-
70
- except Exception as e:
71
- st.error(f"An error occurred: {str(e)}")
72
 
73
- # Function to create a download link
74
- def get_binary_file_downloader_html(bin_file, file_label='File'):
75
- with st.spinner("Preparing download link..."):
76
- data = bin_file
77
- b64 = base64.b64encode(data).decode()
78
- href = f'<a href="data:application/octet-stream;base64,{b64}" download="{file_label}" target="_blank">Download {file_label}</a>'
79
- return href
 
80
 
81
- if __name__ == '__main__':
82
  main()
 
1
  import os
 
 
2
  import tempfile
3
+ from gtts import gTTS
4
  from docx import Document
5
  from PyPDF2 import PdfReader
6
  import streamlit as st
7
  import base64
 
8
 
9
  def text_to_speech(text, output_file):
10
+ tts = gTTS(text)
11
+ tts.save(output_file)
 
 
 
 
12
 
13
  def convert_docx_to_text(docx_file):
14
  doc = Document(docx_file)
 
21
  text = ""
22
  pdf_reader = PdfReader(pdf_file)
23
  for page in pdf_reader.pages:
24
+ page_text = page.extract_text()
25
+ if page_text:
26
+ text += page_text
27
  return text
28
 
29
+ def get_binary_file_downloader_html(bin_file, file_label='File'):
30
+ data = bin_file
31
+ b64 = base64.b64encode(data).decode()
32
+ href = f'<a href="data:application/octet-stream;base64,{b64}" download="{file_label}" target="_blank">Download {file_label}</a>'
33
+ return href
34
+
35
  def main():
36
  st.title("Text to Speech Converter")
37
+
38
  uploaded_file = st.file_uploader("Upload a text, docx, or pdf file", type=["txt", "docx", "pdf"])
39
+
40
  if uploaded_file:
41
  try:
42
+ with tempfile.NamedTemporaryFile(delete=False, suffix='.' + uploaded_file.name.split('.')[-1]) as temp_file:
43
+ temp_file.write(uploaded_file.read())
44
+ temp_file_path = temp_file.name
45
+
46
+ file_extension = uploaded_file.name.split('.')[-1].lower()
47
 
48
+ if file_extension == 'txt':
49
+ with open(temp_file_path, 'r', encoding='utf-8') as f:
50
+ text = f.read()
51
+ elif file_extension == 'docx':
52
+ text = convert_docx_to_text(temp_file_path)
53
+ elif file_extension == 'pdf':
54
+ text = convert_pdf_to_text(temp_file_path)
 
 
55
  else:
56
  st.error("Unsupported file format")
57
  return
58
+
59
+ if not text.strip():
60
+ st.warning("No text found in the file.")
61
+ return
62
+
63
  with st.spinner("Converting text to speech..."):
64
+ output_audio_file = os.path.join(tempfile.gettempdir(), "output.mp3")
65
  text_to_speech(text, output_audio_file)
 
 
 
 
 
 
 
 
 
 
66
 
67
+ st.audio(output_audio_file, format="audio/mp3")
68
+
69
+ with open(output_audio_file, 'rb') as f:
70
+ audio_bytes = f.read()
71
+ st.markdown(get_binary_file_downloader_html(audio_bytes, "output.mp3"), unsafe_allow_html=True)
72
+
73
+ except Exception as e:
74
+ st.error(f"An error occurred: {e}")
75
 
76
+ if __name__ == "__main__":
77
  main()