BasitAliii commited on
Commit
b8ffb22
Β·
verified Β·
1 Parent(s): ca8c5b3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -26
app.py CHANGED
@@ -9,9 +9,10 @@ from gtts import gTTS
9
  import nltk
10
  import numpy as np
11
  from sklearn.feature_extraction.text import TfidfVectorizer
 
12
 
13
  # ==========================================================
14
- # 🧠 NLTK Setup (Fixed punkt_tab Issue)
15
  # ==========================================================
16
  for pkg in ["punkt", "punkt_tab"]:
17
  try:
@@ -26,11 +27,11 @@ DEVICE = -1 # CPU (-1), use 0 for GPU if available
26
  SUMMARIZER_MODEL = "facebook/bart-large-cnn"
27
 
28
  print("Loading summarization model... please wait ⏳")
29
-
30
  try:
31
  summarizer = pipeline("summarization", model=SUMMARIZER_MODEL, device=DEVICE)
 
32
  except Exception as e:
33
- print("Model load error:", e)
34
  summarizer = None
35
 
36
 
@@ -48,7 +49,7 @@ def clean_text(text: str) -> str:
48
 
49
 
50
  def extract_text_from_pdf(path: str) -> str:
51
- """Extract text from all pages of PDF."""
52
  try:
53
  text = ""
54
  with pdfplumber.open(path) as pdf:
@@ -106,7 +107,7 @@ def summarize_long_text(text: str) -> str:
106
  text = clean_text(text)
107
  L = len(text)
108
 
109
- # Dynamic chunking
110
  if L < 1500:
111
  max_len, min_len, chunk_size = 180, 60, 1400
112
  elif L < 5000:
@@ -133,22 +134,31 @@ def summarize_long_text(text: str) -> str:
133
 
134
 
135
  # ==========================================================
136
- # πŸ”Š Text-to-Speech
137
  # ==========================================================
138
  def text_to_speech(text):
139
- """Convert text to speech."""
140
  if not text:
141
  return None
142
  try:
143
- t = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
144
- gTTS(text=text[:900], lang="en").save(t.name)
145
- return t.name
146
- except Exception:
 
 
 
 
 
 
 
 
 
147
  return None
148
 
149
 
150
  # ==========================================================
151
- # πŸ“„ PDF Handler
152
  # ==========================================================
153
  def process_pdf(pdf_file):
154
  """Main handler to process PDF."""
@@ -168,40 +178,41 @@ def process_pdf(pdf_file):
168
 
169
 
170
  # ==========================================================
171
- # 🎨 Gradio UI
172
  # ==========================================================
173
  with gr.Blocks(title="AI PDF Summarizer", theme=gr.themes.Soft()) as demo:
174
  gr.Markdown("# πŸ“˜ AI PDF Summarizer β€” Extract, Summarize & Listen")
175
- gr.Markdown("Easily extract and summarize text from PDFs with AI, and listen to audio summaries.")
176
 
177
- # --- Analyze PDF Tab ---
178
  with gr.Tab("πŸ“„ Analyze PDF"):
179
  with gr.Row():
180
  with gr.Column(scale=1):
181
- pdf_input = gr.File(label="Upload PDF", file_types=[".pdf"], type="filepath")
182
  process_btn = gr.Button("πŸš€ Process PDF", variant="primary")
 
183
  with gr.Column(scale=2):
184
- extracted_text = gr.Textbox(label="Extracted Text", lines=8, interactive=False)
185
- summary_box = gr.Textbox(label="Summary", lines=6, interactive=False)
186
- audio_box = gr.Audio(label="Summary Audio", interactive=False)
187
- keywords_box = gr.Textbox(label="Top Keywords", lines=2, interactive=False)
188
 
189
  # --- About Tab ---
190
  with gr.Tab("ℹ️ About"):
191
  gr.Markdown("""
192
  ## πŸ“˜ About AI PDF Summarizer
193
- **AI PDF Summarizer** helps you quickly understand the contents of any PDF using AI.
194
 
195
  ### ✨ Features
196
  - Extracts and cleans text from PDFs
197
- - Creates adaptive, high-quality summaries
198
- - Identifies key terms and topics using TF-IDF
199
- - Generates audio summaries for listening convenience
200
 
201
- Built with ❀️ using **Hugging Face Transformers**, **Gradio**, and **gTTS**.
202
  """)
203
 
204
- # --- Event Connections ---
205
  process_btn.click(
206
  process_pdf,
207
  inputs=[pdf_input],
 
9
  import nltk
10
  import numpy as np
11
  from sklearn.feature_extraction.text import TfidfVectorizer
12
+ from pydub import AudioSegment
13
 
14
  # ==========================================================
15
+ # 🧠 NLTK Setup (Fixes punkt_tab issue)
16
  # ==========================================================
17
  for pkg in ["punkt", "punkt_tab"]:
18
  try:
 
27
  SUMMARIZER_MODEL = "facebook/bart-large-cnn"
28
 
29
  print("Loading summarization model... please wait ⏳")
 
30
  try:
31
  summarizer = pipeline("summarization", model=SUMMARIZER_MODEL, device=DEVICE)
32
+ print("βœ… Summarizer loaded successfully.")
33
  except Exception as e:
34
+ print("❌ Model load error:", e)
35
  summarizer = None
36
 
37
 
 
49
 
50
 
51
  def extract_text_from_pdf(path: str) -> str:
52
+ """Extract text from all pages of a PDF."""
53
  try:
54
  text = ""
55
  with pdfplumber.open(path) as pdf:
 
107
  text = clean_text(text)
108
  L = len(text)
109
 
110
+ # Dynamic summarization scaling
111
  if L < 1500:
112
  max_len, min_len, chunk_size = 180, 60, 1400
113
  elif L < 5000:
 
134
 
135
 
136
  # ==========================================================
137
+ # πŸ”Š Text-to-Speech (Fixed for Hugging Face)
138
  # ==========================================================
139
  def text_to_speech(text):
140
+ """Convert text to speech and ensure WAV output for Hugging Face playback."""
141
  if not text:
142
  return None
143
  try:
144
+ # Temporary paths
145
+ mp3_path = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3").name
146
+ wav_path = tempfile.NamedTemporaryFile(delete=False, suffix=".wav").name
147
+
148
+ # Generate TTS (MP3)
149
+ gTTS(text=text[:900], lang="en").save(mp3_path)
150
+
151
+ # Convert to WAV for browser playback
152
+ AudioSegment.from_mp3(mp3_path).export(wav_path, format="wav")
153
+
154
+ return wav_path
155
+ except Exception as e:
156
+ print("TTS error:", e)
157
  return None
158
 
159
 
160
  # ==========================================================
161
+ # πŸ“„ PDF Processing
162
  # ==========================================================
163
  def process_pdf(pdf_file):
164
  """Main handler to process PDF."""
 
178
 
179
 
180
  # ==========================================================
181
+ # 🎨 Gradio Interface
182
  # ==========================================================
183
  with gr.Blocks(title="AI PDF Summarizer", theme=gr.themes.Soft()) as demo:
184
  gr.Markdown("# πŸ“˜ AI PDF Summarizer β€” Extract, Summarize & Listen")
185
+ gr.Markdown("Easily extract and summarize text from PDFs using AI, and listen to clear audio summaries.")
186
 
187
+ # --- Main Tab ---
188
  with gr.Tab("πŸ“„ Analyze PDF"):
189
  with gr.Row():
190
  with gr.Column(scale=1):
191
+ pdf_input = gr.File(label="πŸ“‚ Upload PDF", file_types=[".pdf"], type="filepath")
192
  process_btn = gr.Button("πŸš€ Process PDF", variant="primary")
193
+
194
  with gr.Column(scale=2):
195
+ extracted_text = gr.Textbox(label="🧾 Extracted Text", lines=10, interactive=False)
196
+ summary_box = gr.Textbox(label="🧠 Summary", lines=6, interactive=False)
197
+ audio_box = gr.Audio(label="πŸ”Š Summary Audio (Playable)", type="filepath", interactive=False)
198
+ keywords_box = gr.Textbox(label="🏷️ Top Keywords", lines=2, interactive=False)
199
 
200
  # --- About Tab ---
201
  with gr.Tab("ℹ️ About"):
202
  gr.Markdown("""
203
  ## πŸ“˜ About AI PDF Summarizer
204
+ **AI PDF Summarizer** helps you quickly understand long PDFs using Artificial Intelligence.
205
 
206
  ### ✨ Features
207
  - Extracts and cleans text from PDFs
208
+ - Creates adaptive, context-aware summaries
209
+ - Identifies top keywords using TF-IDF
210
+ - Converts summaries into **natural-sounding speech** (WAV format for Spaces compatibility)
211
 
212
+ Built with ❀️ using **Hugging Face Transformers**, **Gradio**, **gTTS**, and **pydub**.
213
  """)
214
 
215
+ # --- Button Functionality ---
216
  process_btn.click(
217
  process_pdf,
218
  inputs=[pdf_input],