Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 (
|
| 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
|
| 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 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
return None
|
| 148 |
|
| 149 |
|
| 150 |
# ==========================================================
|
| 151 |
-
# π PDF
|
| 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
|
| 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
|
| 176 |
|
| 177 |
-
# ---
|
| 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=
|
| 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
|
| 194 |
|
| 195 |
### β¨ Features
|
| 196 |
- Extracts and cleans text from PDFs
|
| 197 |
-
- Creates adaptive,
|
| 198 |
-
- Identifies
|
| 199 |
-
-
|
| 200 |
|
| 201 |
-
Built with β€οΈ using **Hugging Face Transformers**, **Gradio**, and **
|
| 202 |
""")
|
| 203 |
|
| 204 |
-
# ---
|
| 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],
|