JothishJJ commited on
Commit
fa1a7f6
Β·
verified Β·
1 Parent(s): 72015ca

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -0
app.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()