Kurian07 commited on
Commit
daeeb06
·
verified ·
1 Parent(s): 56774bb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -9
app.py CHANGED
@@ -1,6 +1,6 @@
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
@@ -27,21 +27,39 @@ if uploaded_file is not None:
27
  if text.strip() == "":
28
  st.warning("No text found in the PDF. Please upload a valid document.")
29
  else:
30
- # Select TTS model
31
  st.subheader("Generate Speech")
32
  st.info("Using TTS single-speaker model: 'tts_models/en/ljspeech/tacotron2-DDC'")
 
33
  model_name = "tts_models/en/ljspeech/tacotron2-DDC" # Single-speaker model
34
  tts = TTS(model_name)
35
-
36
- # Specify output file
37
  audio_path = "output.wav"
38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  try:
40
- # Generate speech
41
- tts.tts_to_file(text=text, file_path=audio_path) # Ensure no `speaker` for single-speaker models
 
 
 
42
  st.success("Speech generation complete!")
43
-
44
- # Audio playback
45
  st.audio(audio_path, format="audio/wav", start_time=0)
46
 
47
  # Cleanup button
 
1
  import streamlit as st
2
+ import fitz # PyMuPDF
3
+ from TTS.api import TTS
4
  import os
5
 
6
  # Title of the app
 
27
  if text.strip() == "":
28
  st.warning("No text found in the PDF. Please upload a valid document.")
29
  else:
30
+ # Text-to-Speech Conversion
31
  st.subheader("Generate Speech")
32
  st.info("Using TTS single-speaker model: 'tts_models/en/ljspeech/tacotron2-DDC'")
33
+
34
  model_name = "tts_models/en/ljspeech/tacotron2-DDC" # Single-speaker model
35
  tts = TTS(model_name)
 
 
36
  audio_path = "output.wav"
37
 
38
+ # Split text into smaller chunks (e.g., 500 characters)
39
+ def split_text(text, max_length=500):
40
+ sentences = text.split(". ")
41
+ chunks = []
42
+ chunk = ""
43
+ for sentence in sentences:
44
+ if len(chunk) + len(sentence) < max_length:
45
+ chunk += sentence + ". "
46
+ else:
47
+ chunks.append(chunk.strip())
48
+ chunk = sentence + ". "
49
+ if chunk:
50
+ chunks.append(chunk.strip())
51
+ return chunks
52
+
53
+ chunks = split_text(text)
54
+
55
+ # Generate audio for each chunk
56
  try:
57
+ with open(audio_path, "wb") as audio_file:
58
+ for i, chunk in enumerate(chunks):
59
+ st.write(f"Processing chunk {i + 1} of {len(chunks)}...")
60
+ audio_chunk = tts.tts(chunk)
61
+ audio_file.write(audio_chunk)
62
  st.success("Speech generation complete!")
 
 
63
  st.audio(audio_path, format="audio/wav", start_time=0)
64
 
65
  # Cleanup button