JothishJJ commited on
Commit
6fffa48
Β·
verified Β·
1 Parent(s): 115f47b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -22
app.py CHANGED
@@ -1,38 +1,57 @@
1
- import PyPDF2
2
- from transformers import pipeline
3
  import gradio as gr
 
 
 
 
 
 
 
4
 
5
  # Function to extract text from PDF
6
- def extract_text_from_pdf(pdf_file):
7
- reader = PyPDF2.PdfReader(pdf_file)
8
  text = ""
9
  for page in reader.pages:
10
  if page and page.extract_text():
11
  text += page.extract_text()
12
  return text
13
 
14
- # Load text-to-speech pipeline from Hugging Face
15
- tts = pipeline("text-to-speech", model="facebook/fastspeech2-en-ljspeech")
 
 
 
 
 
 
 
16
 
17
- # Function to convert PDF to audio with no text limit
18
- def pdf_to_audio(pdf_file):
19
- text = extract_text_from_pdf(pdf_file)
20
- if not text.strip():
21
- return "", "No text found in PDF"
 
 
 
22
 
23
- audio = tts(text)
24
- audio_path = "output_audio.wav"
25
- with open(audio_path, "wb") as f:
26
- f.write(audio["audio"]) # Hugging Face TTS returns audio data
27
 
28
- return audio_path, "Audio generated successfully"
 
 
29
 
30
  # Gradio interface
31
- interface = gr.Interface(
32
- fn=pdf_to_audio,
33
- inputs=gr.File(type="file"),
34
- outputs=[gr.Audio(type="filepath"), gr.Text()]
 
 
 
 
 
35
  )
36
 
37
- if __name__ == "__main__":
38
- interface.launch()
 
 
 
1
  import gradio as gr
2
+ from transformers import pipeline
3
+ from PyPDF2 import PdfReader
4
+ from ebooklib import epub
5
+ from bs4 import BeautifulSoup
6
+
7
+ # Load the VITS model from Hugging Face
8
+ text_to_speech = pipeline("text-to-speech", model="efficient-speech/lite-whisper-large-v3-turbo")
9
 
10
  # Function to extract text from PDF
11
+ def extract_pdf_text(file):
12
+ reader = PdfReader(file)
13
  text = ""
14
  for page in reader.pages:
15
  if page and page.extract_text():
16
  text += page.extract_text()
17
  return text
18
 
19
+ # Function to extract text from EPUB
20
+ def extract_epub_text(file):
21
+ book = epub.read_epub(file)
22
+ text = ""
23
+ for item in book.get_items():
24
+ if item.get_type() == epub.ITEM_DOCUMENT:
25
+ soup = BeautifulSoup(item.content, 'html.parser')
26
+ text += soup.get_text()
27
+ return text
28
 
29
+ # Unified function to convert text to speech
30
+ def convert_to_audio(file, file_type):
31
+ if file_type == 'PDF':
32
+ text = extract_pdf_text(file)
33
+ elif file_type == 'EPUB':
34
+ text = extract_epub_text(file)
35
+ else:
36
+ text = file.read().decode('utf-8')
37
 
38
+ if not text.strip():
39
+ return "No text found in the file."
 
 
40
 
41
+ # Convert text to speech
42
+ audio = text_to_speech(text[:5000]) # Limiting input to avoid model constraints
43
+ return (audio["audio"],)
44
 
45
  # Gradio interface
46
+ demo = gr.Interface(
47
+ fn=convert_to_audio,
48
+ inputs=[
49
+ gr.File(label="Upload PDF, EPUB, or Text File"),
50
+ gr.Radio(["PDF", "EPUB", "TXT"], label="File Type")
51
+ ],
52
+ outputs="audio",
53
+ title="Unlimited Text-to-Speech Converter",
54
+ description="Upload PDF, EPUB, or text files β€” convert them into audio with no limits!"
55
  )
56
 
57
+ demo.launch()