Spaces:
Sleeping
MAJOR UPGRADE: Enhanced Study Guide Generator
Browse filesMassive improvements to output quality and user experience:
β¨ NEW FEATURES:
- 3 Detail Levels: Concise / Detailed / Very Detailed
- Smart sentence-boundary chunking (no mid-sentence cuts)
- Real-time progress updates with emojis
- Bullet point formatting for easy scanning
- Text cleaning (removes page numbers, fixes hyphenation)
- Better gr.Blocks UI with organized layout
π QUALITY IMPROVEMENTS:
- Intelligent text overlap between chunks for context
- Longer outputs: up to 500 words per section (Very Detailed)
- Extract key points as bullet lists
- Professional markdown formatting
- Document statistics (pages, words, sections)
π STUDY FEATURES:
- Comprehensive header with document info
- Organized sections with clear numbering
- Study synthesis section with tips
- How to use this guide instructions
- Study strategy recommendations
π¨ UX IMPROVEMENTS:
- Live progress indicators during processing
- Clear status messages with icons
- Better error handling
- Helpful tips in sidebar
- Larger output area (25-40 lines)
This transforms the tool from a basic summarizer into a
professional study guide generator perfect for exam prep!
|
@@ -1,4 +1,5 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import fitz
|
| 4 |
from transformers import pipeline
|
|
@@ -10,102 +11,263 @@ device = 0 if torch.cuda.is_available() else -1
|
|
| 10 |
summarizer = pipeline("summarization", model="facebook/bart-large-cnn", device=device)
|
| 11 |
print("Model ready!")
|
| 12 |
|
| 13 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
if pdf_file is None:
|
| 15 |
-
return "Please upload a PDF file."
|
| 16 |
|
| 17 |
-
# Extract text
|
| 18 |
-
text = ""
|
| 19 |
try:
|
|
|
|
|
|
|
|
|
|
| 20 |
with fitz.open(pdf_file.name) as doc:
|
| 21 |
-
|
|
|
|
| 22 |
text += page.get_text()
|
| 23 |
-
|
| 24 |
-
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
|
|
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
return "Could not generate study guide. Please try a different PDF."
|
| 67 |
|
| 68 |
-
|
| 69 |
-
study_guide = f"""# π Study Guide
|
| 70 |
|
| 71 |
-
**
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
---
|
| 74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
"""
|
| 76 |
|
| 77 |
-
|
|
|
|
|
|
|
| 78 |
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
---
|
| 82 |
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
-
|
| 86 |
-
- Review each section carefully
|
| 87 |
-
- Focus on key concepts highlighted above
|
| 88 |
-
- Use this as your primary study material
|
| 89 |
|
| 90 |
-
|
| 91 |
"""
|
| 92 |
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
if __name__ == "__main__":
|
| 111 |
demo.launch()
|
|
|
|
| 1 |
import os
|
| 2 |
+
import re
|
| 3 |
import gradio as gr
|
| 4 |
import fitz
|
| 5 |
from transformers import pipeline
|
|
|
|
| 11 |
summarizer = pipeline("summarization", model="facebook/bart-large-cnn", device=device)
|
| 12 |
print("Model ready!")
|
| 13 |
|
| 14 |
+
def clean_text(text):
|
| 15 |
+
"""Clean and normalize extracted text."""
|
| 16 |
+
# Remove excessive whitespace
|
| 17 |
+
text = re.sub(r'\s+', ' ', text)
|
| 18 |
+
# Remove page numbers and headers/footers (common patterns)
|
| 19 |
+
text = re.sub(r'\n\d+\n', '\n', text)
|
| 20 |
+
# Fix common OCR issues
|
| 21 |
+
text = re.sub(r'(\w)-\s+(\w)', r'\1\2', text) # Fix hyphenated words
|
| 22 |
+
return text.strip()
|
| 23 |
+
|
| 24 |
+
def smart_chunk_text(text, chunk_size=3000, overlap=600):
|
| 25 |
+
"""
|
| 26 |
+
Intelligently chunk text by trying to break at sentence boundaries.
|
| 27 |
+
"""
|
| 28 |
+
sentences = re.split(r'(?<=[.!?])\s+', text)
|
| 29 |
+
chunks = []
|
| 30 |
+
current_chunk = ""
|
| 31 |
+
|
| 32 |
+
for sentence in sentences:
|
| 33 |
+
if len(current_chunk) + len(sentence) < chunk_size:
|
| 34 |
+
current_chunk += sentence + " "
|
| 35 |
+
else:
|
| 36 |
+
if current_chunk:
|
| 37 |
+
chunks.append(current_chunk.strip())
|
| 38 |
+
current_chunk = sentence + " "
|
| 39 |
+
|
| 40 |
+
if current_chunk:
|
| 41 |
+
chunks.append(current_chunk.strip())
|
| 42 |
+
|
| 43 |
+
# Add overlap between chunks for context
|
| 44 |
+
overlapped_chunks = []
|
| 45 |
+
for i, chunk in enumerate(chunks):
|
| 46 |
+
if i > 0 and overlap > 0:
|
| 47 |
+
# Add last part of previous chunk for context
|
| 48 |
+
prev_words = chunks[i-1].split()[-overlap:]
|
| 49 |
+
chunk = " ".join(prev_words) + " " + chunk
|
| 50 |
+
overlapped_chunks.append(chunk)
|
| 51 |
+
|
| 52 |
+
return overlapped_chunks
|
| 53 |
+
|
| 54 |
+
def extract_key_points(summary_text):
|
| 55 |
+
"""Format summary as bullet points for better readability."""
|
| 56 |
+
# Split into sentences
|
| 57 |
+
sentences = re.split(r'(?<=[.!?])\s+', summary_text)
|
| 58 |
+
|
| 59 |
+
# Create bullet points
|
| 60 |
+
bullet_points = []
|
| 61 |
+
for sentence in sentences:
|
| 62 |
+
sentence = sentence.strip()
|
| 63 |
+
if len(sentence) > 20: # Only substantial sentences
|
| 64 |
+
bullet_points.append(f"β’ {sentence}")
|
| 65 |
+
|
| 66 |
+
return "\n".join(bullet_points)
|
| 67 |
+
|
| 68 |
+
def create_study_guide(pdf_file, detail_level="Detailed"):
|
| 69 |
if pdf_file is None:
|
| 70 |
+
return "β οΈ Please upload a PDF file first."
|
| 71 |
|
|
|
|
|
|
|
| 72 |
try:
|
| 73 |
+
# Extract text
|
| 74 |
+
yield "π Extracting text from PDF..."
|
| 75 |
+
text = ""
|
| 76 |
with fitz.open(pdf_file.name) as doc:
|
| 77 |
+
total_pages = len(doc)
|
| 78 |
+
for page_num, page in enumerate(doc, 1):
|
| 79 |
text += page.get_text()
|
| 80 |
+
if page_num % 5 == 0:
|
| 81 |
+
yield f"π Reading pages... {page_num}/{total_pages}"
|
| 82 |
|
| 83 |
+
if not text.strip():
|
| 84 |
+
yield "β PDF is empty or contains no readable text."
|
| 85 |
+
return
|
| 86 |
|
| 87 |
+
# Clean text
|
| 88 |
+
yield "π§Ή Cleaning and processing text..."
|
| 89 |
+
text = clean_text(text)
|
| 90 |
+
word_count = len(text.split())
|
| 91 |
|
| 92 |
+
# Determine parameters based on detail level
|
| 93 |
+
if detail_level == "Very Detailed":
|
| 94 |
+
chunk_size = 3500
|
| 95 |
+
max_length = 500
|
| 96 |
+
min_length = 200
|
| 97 |
+
elif detail_level == "Detailed":
|
| 98 |
+
chunk_size = 3000
|
| 99 |
+
max_length = 400
|
| 100 |
+
min_length = 150
|
| 101 |
+
else: # Concise
|
| 102 |
+
chunk_size = 2500
|
| 103 |
+
max_length = 300
|
| 104 |
+
min_length = 100
|
| 105 |
|
| 106 |
+
# Smart chunking
|
| 107 |
+
yield "π Dividing into logical sections..."
|
| 108 |
+
chunks = smart_chunk_text(text, chunk_size=chunk_size, overlap=100)
|
| 109 |
+
total_chunks = len(chunks)
|
| 110 |
|
| 111 |
+
# Process each chunk
|
| 112 |
+
study_sections = []
|
| 113 |
+
for i, chunk in enumerate(chunks, 1):
|
| 114 |
+
yield f"π€ Generating study notes for section {i}/{total_chunks}..."
|
| 115 |
+
|
| 116 |
+
try:
|
| 117 |
+
# Generate detailed summary
|
| 118 |
+
result = summarizer(
|
| 119 |
+
chunk,
|
| 120 |
+
max_length=max_length,
|
| 121 |
+
min_length=min_length,
|
| 122 |
+
do_sample=False,
|
| 123 |
+
truncation=True,
|
| 124 |
+
early_stopping=True
|
| 125 |
+
)
|
| 126 |
+
|
| 127 |
+
section_summary = result[0]['summary_text']
|
| 128 |
+
|
| 129 |
+
# Format as bullet points for readability
|
| 130 |
+
formatted_section = extract_key_points(section_summary)
|
| 131 |
|
| 132 |
+
study_sections.append({
|
| 133 |
+
'number': i,
|
| 134 |
+
'content': formatted_section,
|
| 135 |
+
'raw': section_summary
|
| 136 |
+
})
|
| 137 |
|
| 138 |
+
except Exception as e:
|
| 139 |
+
continue
|
| 140 |
|
| 141 |
+
if not study_sections:
|
| 142 |
+
yield "β Could not generate study guide. Please try a different PDF."
|
| 143 |
+
return
|
| 144 |
|
| 145 |
+
# Create comprehensive study guide
|
| 146 |
+
yield "β¨ Formatting your study guide..."
|
|
|
|
| 147 |
|
| 148 |
+
study_guide = f"""# π COMPREHENSIVE STUDY GUIDE
|
|
|
|
| 149 |
|
| 150 |
+
**Document:** {os.path.basename(pdf_file.name)}
|
| 151 |
+
**Pages:** {total_pages}
|
| 152 |
+
**Words in Original:** {word_count:,}
|
| 153 |
+
**Study Sections Generated:** {len(study_sections)}
|
| 154 |
+
**Detail Level:** {detail_level}
|
| 155 |
|
| 156 |
---
|
| 157 |
|
| 158 |
+
## π KEY CONCEPTS AND IMPORTANT POINTS
|
| 159 |
+
|
| 160 |
+
"""
|
| 161 |
+
|
| 162 |
+
# Add all sections
|
| 163 |
+
for section in study_sections:
|
| 164 |
+
study_guide += f"""
|
| 165 |
+
### π Section {section['number']} of {total_chunks}
|
| 166 |
+
|
| 167 |
+
{section['content']}
|
| 168 |
+
|
| 169 |
+
---
|
| 170 |
"""
|
| 171 |
|
| 172 |
+
# Add synthesis section if we have multiple sections
|
| 173 |
+
if len(study_sections) > 1:
|
| 174 |
+
study_guide += """
|
| 175 |
|
| 176 |
+
## π― STUDY SYNTHESIS
|
| 177 |
+
|
| 178 |
+
This study guide extracted the most important points from your document. Each section above covers key concepts you need to understand.
|
| 179 |
+
|
| 180 |
+
### π How to Use This Guide:
|
| 181 |
+
|
| 182 |
+
1. **First Read**: Go through all sections to get an overview
|
| 183 |
+
2. **Deep Dive**: Study each section carefully, one at a time
|
| 184 |
+
3. **Make Connections**: Link concepts between different sections
|
| 185 |
+
4. **Active Recall**: Try to remember key points without looking
|
| 186 |
+
5. **Review Regularly**: Come back to this guide before your exam
|
| 187 |
+
|
| 188 |
+
### π‘ Study Tips:
|
| 189 |
+
|
| 190 |
+
β’ Focus on understanding the concepts, not memorizing word-for-word
|
| 191 |
+
β’ Create your own examples for each key point
|
| 192 |
+
β’ Explain these concepts to someone else to test your understanding
|
| 193 |
+
β’ Highlight or annotate the most important points for quick review
|
| 194 |
|
| 195 |
---
|
| 196 |
|
| 197 |
+
"""
|
| 198 |
+
|
| 199 |
+
study_guide += f"""
|
| 200 |
+
## β
STUDY GUIDE COMPLETE
|
| 201 |
+
|
| 202 |
+
**Total Sections Processed:** {len(study_sections)}/{total_chunks}
|
| 203 |
+
**Coverage:** Comprehensive overview of all important topics
|
| 204 |
+
|
| 205 |
+
*This study guide was generated using AI to extract and organize the most relevant information for your studies. Use it as your primary study material alongside your class notes.*
|
| 206 |
|
| 207 |
+
---
|
|
|
|
|
|
|
|
|
|
| 208 |
|
| 209 |
+
π Good luck with your studies! π
|
| 210 |
"""
|
| 211 |
|
| 212 |
+
yield study_guide
|
| 213 |
+
|
| 214 |
+
except Exception as e:
|
| 215 |
+
yield f"β Error: {str(e)}\n\nPlease try uploading the PDF again."
|
| 216 |
+
|
| 217 |
+
# Create enhanced interface
|
| 218 |
+
with gr.Blocks(title="PDF Study Guide Generator", theme=gr.themes.Soft()) as demo:
|
| 219 |
+
gr.Markdown("""
|
| 220 |
+
# π AI-Powered Study Guide Generator
|
| 221 |
+
|
| 222 |
+
Upload your PDF and get a comprehensive, well-organized study guide perfect for exam preparation!
|
| 223 |
+
""")
|
| 224 |
+
|
| 225 |
+
with gr.Row():
|
| 226 |
+
with gr.Column():
|
| 227 |
+
pdf_input = gr.File(
|
| 228 |
+
label="π Upload Your PDF Document",
|
| 229 |
+
file_types=[".pdf"]
|
| 230 |
+
)
|
| 231 |
+
|
| 232 |
+
detail_level = gr.Radio(
|
| 233 |
+
choices=["Concise", "Detailed", "Very Detailed"],
|
| 234 |
+
value="Detailed",
|
| 235 |
+
label="π Detail Level",
|
| 236 |
+
info="Choose how comprehensive you want your study guide"
|
| 237 |
+
)
|
| 238 |
+
|
| 239 |
+
generate_btn = gr.Button("π Generate Study Guide", variant="primary", size="lg")
|
| 240 |
+
|
| 241 |
+
gr.Markdown("""
|
| 242 |
+
### π‘ Tips:
|
| 243 |
+
- **Concise**: Quick overview, main points only
|
| 244 |
+
- **Detailed**: Balanced coverage (recommended)
|
| 245 |
+
- **Very Detailed**: Comprehensive, thorough notes
|
| 246 |
+
""")
|
| 247 |
+
|
| 248 |
+
with gr.Column():
|
| 249 |
+
output = gr.Textbox(
|
| 250 |
+
label="π Your Study Guide",
|
| 251 |
+
lines=25,
|
| 252 |
+
max_lines=40,
|
| 253 |
+
placeholder="Your detailed study guide will appear here...\n\nIt will include:\nβ’ Organized sections\nβ’ Key concepts\nβ’ Important points\nβ’ Study tips"
|
| 254 |
+
)
|
| 255 |
+
|
| 256 |
+
generate_btn.click(
|
| 257 |
+
fn=create_study_guide,
|
| 258 |
+
inputs=[pdf_input, detail_level],
|
| 259 |
+
outputs=output
|
| 260 |
+
)
|
| 261 |
+
|
| 262 |
+
gr.Markdown("""
|
| 263 |
+
---
|
| 264 |
+
### π― What You'll Get:
|
| 265 |
+
- β
Comprehensive coverage of all important topics
|
| 266 |
+
- β
Organized in easy-to-study sections
|
| 267 |
+
- β
Bullet points for quick scanning
|
| 268 |
+
- β
Study tips and guidance
|
| 269 |
+
- β
Perfect for exam preparation
|
| 270 |
+
""")
|
| 271 |
|
| 272 |
if __name__ == "__main__":
|
| 273 |
demo.launch()
|