Spaces:
Sleeping
Sleeping
Create App.py
Browse files
App.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import fitz # PyMuPDF for PDF text extraction
|
| 3 |
+
from TTS.api import TTS # Coqui TTS
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# Title of the app
|
| 7 |
+
st.title("PDF to Speech with Coqui TTS")
|
| 8 |
+
|
| 9 |
+
# File upload
|
| 10 |
+
uploaded_file = st.file_uploader("Upload a PDF", type=["pdf"])
|
| 11 |
+
|
| 12 |
+
if uploaded_file is not None:
|
| 13 |
+
# Save the uploaded PDF temporarily
|
| 14 |
+
with open("uploaded.pdf", "wb") as f:
|
| 15 |
+
f.write(uploaded_file.read())
|
| 16 |
+
|
| 17 |
+
# Extract text from PDF
|
| 18 |
+
with fitz.open("uploaded.pdf") as pdf:
|
| 19 |
+
text = ""
|
| 20 |
+
for page in pdf:
|
| 21 |
+
text += page.get_text()
|
| 22 |
+
|
| 23 |
+
# Display the extracted text
|
| 24 |
+
st.subheader("Extracted Text")
|
| 25 |
+
st.write(text)
|
| 26 |
+
|
| 27 |
+
if text.strip() == "":
|
| 28 |
+
st.warning("No text found in the PDF. Please upload a valid document.")
|
| 29 |
+
else:
|
| 30 |
+
# Convert text to speech using Coqui TTS
|
| 31 |
+
st.subheader("Generate Speech")
|
| 32 |
+
model_name = "tts_models/en/ljspeech/tacotron2-DDC"
|
| 33 |
+
tts = TTS(model_name)
|
| 34 |
+
audio_path = "output.wav"
|
| 35 |
+
tts.tts_to_file(text, audio_path)
|
| 36 |
+
st.success("Speech generation complete!")
|
| 37 |
+
|
| 38 |
+
# Display audio player
|
| 39 |
+
st.audio(audio_path, format="audio/wav", start_time=0)
|
| 40 |
+
|
| 41 |
+
# Clean up temporary files
|
| 42 |
+
if st.button("Clear Audio"):
|
| 43 |
+
os.remove("uploaded.pdf")
|
| 44 |
+
os.remove(audio_path)
|
| 45 |
+
st.experimental_rerun()
|