Spaces:
Sleeping
Sleeping
Update src/app.py
Browse files- src/app.py +44 -60
src/app.py
CHANGED
@@ -1,80 +1,64 @@
|
|
1 |
import os
|
|
|
|
|
|
|
2 |
import tempfile
|
3 |
-
import
|
4 |
from docx import Document
|
5 |
from PyPDF2 import PdfReader
|
6 |
import streamlit as st
|
7 |
-
import base64
|
8 |
from io import BytesIO
|
9 |
|
10 |
-
def text_to_speech(text
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
engine.runAndWait()
|
17 |
|
18 |
def convert_docx_to_text(docx_file):
|
19 |
doc = Document(docx_file)
|
20 |
-
text
|
21 |
-
for paragraph in doc.paragraphs:
|
22 |
-
text += paragraph.text + "\n"
|
23 |
-
return text
|
24 |
|
25 |
def convert_pdf_to_text(pdf_file):
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
|
|
|
|
31 |
|
32 |
def main():
|
33 |
-
st.title("Text to Speech Converter")
|
34 |
-
|
35 |
uploaded_file = st.file_uploader("Upload a text, docx, or pdf file", type=["txt", "docx", "pdf"])
|
36 |
-
|
37 |
if uploaded_file:
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
with open(temp_file.name, 'r', encoding='utf-8') as txt_file:
|
48 |
-
text = txt_file.read()
|
49 |
-
elif file_extension.lower() == 'docx':
|
50 |
-
text = convert_docx_to_text(temp_file.name)
|
51 |
-
elif file_extension.lower() == 'pdf':
|
52 |
-
text = convert_pdf_to_text(temp_file.name)
|
53 |
-
else:
|
54 |
-
st.error("Unsupported file format")
|
55 |
-
return
|
56 |
-
|
57 |
-
with st.spinner("Converting text to speech..."):
|
58 |
-
output_audio_file = "output.mp3"
|
59 |
-
text_to_speech(text, output_audio_file)
|
60 |
-
|
61 |
-
st.audio(output_audio_file, format="audio/mp3", start_time=0)
|
62 |
-
|
63 |
-
# Provide a download link for the audio file
|
64 |
-
with open(output_audio_file, 'rb') as audio_file:
|
65 |
-
audio_bytes = audio_file.read()
|
66 |
-
st.markdown(get_binary_file_downloader_html(audio_bytes, output_audio_file), unsafe_allow_html=True)
|
67 |
-
|
68 |
-
except Exception as e:
|
69 |
-
st.error(f"An error occurred: {str(e)}")
|
70 |
|
71 |
-
|
72 |
-
|
73 |
-
with st.spinner("Preparing download link..."):
|
74 |
-
data = bin_file
|
75 |
-
b64 = base64.b64encode(data).decode()
|
76 |
-
href = f'<a href="data:application/octet-stream;base64,{b64}" download="{file_label}" target="_blank">Download {file_label}</a>'
|
77 |
-
return href
|
78 |
|
79 |
-
if __name__ ==
|
80 |
main()
|
|
|
1 |
import os
|
2 |
+
os.environ["STREAMLIT_CONFIG_DIR"] = "/tmp/.streamlit"
|
3 |
+
os.makedirs("/tmp/.streamlit", exist_ok=True)
|
4 |
+
|
5 |
import tempfile
|
6 |
+
from gtts import gTTS
|
7 |
from docx import Document
|
8 |
from PyPDF2 import PdfReader
|
9 |
import streamlit as st
|
10 |
+
import base64
|
11 |
from io import BytesIO
|
12 |
|
13 |
+
def text_to_speech(text):
|
14 |
+
tts = gTTS(text)
|
15 |
+
audio_buffer = BytesIO()
|
16 |
+
tts.write_to_fp(audio_buffer)
|
17 |
+
audio_buffer.seek(0)
|
18 |
+
return audio_buffer
|
|
|
19 |
|
20 |
def convert_docx_to_text(docx_file):
|
21 |
doc = Document(docx_file)
|
22 |
+
return "\n".join([p.text for p in doc.paragraphs])
|
|
|
|
|
|
|
23 |
|
24 |
def convert_pdf_to_text(pdf_file):
|
25 |
+
reader = PdfReader(pdf_file)
|
26 |
+
return "\n".join([page.extract_text() or '' for page in reader.pages])
|
27 |
+
|
28 |
+
def get_download_link(audio_data, filename="output.mp3"):
|
29 |
+
b64 = base64.b64encode(audio_data).decode()
|
30 |
+
href = f'<a href="data:audio/mp3;base64,{b64}" download="{filename}">Download {filename}</a>'
|
31 |
+
return href
|
32 |
|
33 |
def main():
|
34 |
+
st.title("Text to Speech Converter (No File Write)")
|
35 |
+
|
36 |
uploaded_file = st.file_uploader("Upload a text, docx, or pdf file", type=["txt", "docx", "pdf"])
|
37 |
+
|
38 |
if uploaded_file:
|
39 |
+
ext = uploaded_file.name.split('.')[-1].lower()
|
40 |
+
text = ""
|
41 |
+
|
42 |
+
if ext == 'txt':
|
43 |
+
text = uploaded_file.read().decode("utf-8")
|
44 |
+
elif ext == 'docx':
|
45 |
+
text = convert_docx_to_text(uploaded_file)
|
46 |
+
elif ext == 'pdf':
|
47 |
+
text = convert_pdf_to_text(uploaded_file)
|
48 |
+
else:
|
49 |
+
st.error("Unsupported file format")
|
50 |
+
return
|
51 |
+
|
52 |
+
if not text.strip():
|
53 |
+
st.warning("No readable text found.")
|
54 |
+
return
|
55 |
|
56 |
+
with st.spinner("Converting to speech..."):
|
57 |
+
audio_buffer = text_to_speech(text)
|
58 |
+
audio_bytes = audio_buffer.read()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
+
st.audio(audio_bytes, format="audio/mp3")
|
61 |
+
st.markdown(get_download_link(audio_bytes), unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
62 |
|
63 |
+
if __name__ == "__main__":
|
64 |
main()
|