File size: 10,843 Bytes
4815095
b79eb66
6b31f29
4815095
b026de9
dbabd5b
 
4815095
03a9c76
4815095
dbabd5b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b79eb66
 
 
 
29b5dc9
b79eb66
 
dbabd5b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b026de9
b79eb66
4815095
 
b79eb66
 
 
4815095
b79eb66
 
dbabd5b
29b5dc9
b79eb66
4815095
b79eb66
 
 
4815095
b79eb66
dbabd5b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29b5dc9
 
 
dbabd5b
 
03a9c76
dbabd5b
2ae8413
 
 
dbabd5b
03a9c76
dbabd5b
80d371c
 
 
dbabd5b
 
 
 
b79eb66
dbabd5b
 
80d371c
b79eb66
 
dbabd5b
 
 
2ae8413
dbabd5b
 
80d371c
dbabd5b
 
 
03a9c76
dbabd5b
 
29b5dc9
dbabd5b
29b5dc9
03a9c76
29b5dc9
 
03a9c76
 
dbabd5b
 
 
 
 
03a9c76
dbabd5b
 
 
 
 
 
03a9c76
dbabd5b
03a9c76
dbabd5b
 
 
 
80d371c
dbabd5b
80d371c
 
dbabd5b
 
 
80d371c
 
dbabd5b
 
80d371c
 
 
dbabd5b
 
03a9c76
29b5dc9
 
dbabd5b
 
29b5dc9
 
 
80d371c
29b5dc9
dbabd5b
29b5dc9
dbabd5b
 
 
03a9c76
 
80d371c
dbabd5b
29b5dc9
 
 
03a9c76
80d371c
dbabd5b
 
 
 
 
b79eb66
dbabd5b
 
 
 
 
2ae8413
b79eb66
2ae8413
dbabd5b
 
2ae8413
 
b79eb66
 
 
 
 
03a9c76
dbabd5b
b79eb66
dbabd5b
 
b79eb66
dbabd5b
b79eb66
 
 
29b5dc9
b79eb66
29b5dc9
b79eb66
 
 
 
03a9c76
29b5dc9
b79eb66
dbabd5b
b79eb66
 
29b5dc9
dbabd5b
29b5dc9
 
 
b79eb66
 
dbabd5b
 
 
 
 
29b5dc9
 
dbabd5b
 
 
29b5dc9
dbabd5b
b79eb66
 
29b5dc9
b79eb66
dbabd5b
29b5dc9
 
dbabd5b
b79eb66
 
 
dbabd5b
03a9c76
b79eb66
 
 
 
 
dbabd5b
29b5dc9
dbabd5b
 
 
 
 
29b5dc9
dbabd5b
 
 
 
 
29b5dc9
 
 
dbabd5b
b79eb66
4815095
 
03a9c76
b026de9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
import os
import re
import warnings
import gradio as gr
import fitz
from transformers import pipeline
import torch

warnings.filterwarnings("ignore")

# Initialize models
print("Loading AI models for detailed analysis...")
device = 0 if torch.cuda.is_available() else -1

# Use FLAN-T5 for better text generation and explanation
try:
    # FLAN-T5 is instruction-tuned and better at generating detailed explanations
    analyzer = pipeline("text2text-generation", model="google/flan-t5-base", device=device, max_length=512)
    print("βœ“ FLAN-T5 model loaded (instruction-tuned for detailed explanations)")
    use_flan = True
except:
    # Fallback to regular T5
    analyzer = pipeline("text2text-generation", model="t5-base", device=device, max_length=512)
    print("βœ“ T5 model loaded")
    use_flan = False

print("Models ready!")

def clean_text(text):
    """Clean and normalize extracted text."""
    text = re.sub(r'\s+', ' ', text)
    text = re.sub(r'\n\d+\n', '\n', text)
    text = re.sub(r'(\w)-\s+(\w)', r'\1\2', text)
    return text.strip()

def analyze_and_explain_section(text, section_num, total_sections):
    """Use AI to analyze and create detailed explanations."""

    # Create detailed instruction for the AI
    if use_flan:
        prompt = f"""Analyze this educational content and create a detailed study guide section. Include:
1. Main concepts and what they mean
2. Key definitions with clear explanations
3. Important points students must know
4. Examples if mentioned

Content: {text[:2000]}

Create detailed study notes:"""
    else:
        prompt = f"explain in detail for students: {text[:2000]}"

    try:
        result = analyzer(prompt, max_length=500, min_length=200, do_sample=False, num_beams=4)
        return result[0]['generated_text']
    except:
        # If AI fails, provide structured extraction
        return text[:1000]

def create_comprehensive_study_guide(pdf_file, detail_level="Maximum Detail"):
    if pdf_file is None:
        return "⚠️ Please upload a PDF file first."

    try:
        # Extract text
        yield "πŸ“„ Extracting text from PDF..."
        text = ""
        with fitz.open(pdf_file.name) as doc:
            total_pages = len(doc)
            for page_num, page in enumerate(doc, 1):
                text += page.get_text() + "\n\n"
                if page_num % 3 == 0:
                    yield f"πŸ“„ Reading pages... {page_num}/{total_pages}"

        if not text.strip():
            yield "❌ PDF is empty or contains no readable text."
            return

        # Clean text
        yield "🧹 Cleaning and processing text..."
        text = clean_text(text)
        word_count = len(text.split())

        # Split into chunks for AI analysis
        chunk_size = 2000  # Tokens for AI to analyze
        words = text.split()
        chunks = []

        for i in range(0, len(words), chunk_size):
            chunk = ' '.join(words[i:i + chunk_size])
            if len(chunk.strip()) > 100:
                chunks.append(chunk)

        total_chunks = len(chunks)
        yield f"πŸ“Š Divided into {total_chunks} sections for detailed AI analysis..."

        # Analyze each chunk with AI
        detailed_sections = []

        for i, chunk in enumerate(chunks, 1):
            yield f"πŸ€– AI analyzing section {i}/{total_chunks} - creating detailed explanations..."

            # Get AI analysis
            ai_analysis = analyze_and_explain_section(chunk, i, total_chunks)

            # Extract key points from original
            sentences = re.split(r'(?<=[.!?])\s+', chunk)
            key_points = []
            definitions = []

            for sent in sentences:
                sent = sent.strip()
                if len(sent) > 20:
                    # Check for definitions
                    if ' is ' in sent or ' are ' in sent or ' means ' in sent or ':' in sent:
                        definitions.append(sent)
                    # Check for important points
                    elif any(kw in sent.lower() for kw in ['important', 'key', 'must', 'critical', 'main', 'essential']):
                        key_points.append(sent)

            detailed_sections.append({
                'number': i,
                'ai_analysis': ai_analysis,
                'definitions': definitions[:5],
                'key_points': key_points[:5],
                'original': chunk[:500]  # Keep some original context
            })

        # Create the study guide
        yield "✨ Assembling your comprehensive study guide..."

        study_guide = f"""# πŸ“š AI-POWERED COMPREHENSIVE STUDY GUIDE

**πŸ“„ Document:** {os.path.basename(pdf_file.name)}
**πŸ“– Total Pages:** {total_pages}
**πŸ“Š Original Word Count:** {word_count:,} words
**πŸ€– AI Model:** {"FLAN-T5 (Instruction-tuned)" if use_flan else "T5"}
**🎯 Detail Level:** {detail_level}
**πŸ“ Sections Analyzed:** {total_chunks}

---

## πŸ“– DETAILED STUDY SECTIONS

*Each section below has been analyzed by AI to create detailed study notes with explanations*

"""

        # Add all detailed sections
        for section in detailed_sections:
            study_guide += f"""
### πŸ“Œ SECTION {section['number']} of {total_chunks}

#### πŸ€– AI DETAILED ANALYSIS:
{section['ai_analysis']}

"""

            if section['definitions']:
                study_guide += """
#### πŸ“– KEY DEFINITIONS IN THIS SECTION:
"""
                for j, definition in enumerate(section['definitions'], 1):
                    study_guide += f"{j}. {definition}\n\n"

            if section['key_points']:
                study_guide += """
#### ⭐ IMPORTANT POINTS:
"""
                for j, point in enumerate(section['key_points'], 1):
                    study_guide += f"β€’ {point}\n\n"

            study_guide += "\n---\n"

        # Add study methodology
        study_guide += """

## 🎯 HOW TO USE THIS STUDY GUIDE FOR 100% SUCCESS

### PHASE 1: UNDERSTANDING (First Read - 2-3 hours)
1. Read through all **AI DETAILED ANALYSIS** sections carefully
2. Understand every **KEY DEFINITION**
3. Pay special attention to **IMPORTANT POINTS**
4. Don't rush - comprehension first!

### PHASE 2: DEEP LEARNING (Second Read - 3-4 hours)
1. Go section by section, slowly
2. For each concept: Can you explain it in your own words?
3. For each definition: Can you give an example?
4. Create your own notes and summaries
5. Test yourself on key definitions

### PHASE 3: ACTIVE RECALL (Third Read - 2-3 hours)
1. Cover the guide and try to recall main points
2. Explain each section out loud as if teaching
3. Write down what you remember, then check
4. Focus extra time on weak areas
5. Create flashcards for difficult concepts

### πŸ’― EXAM PREPARATION TIMELINE

**1 Week Before:**
- Complete Phase 1 (Understanding)
- Start Phase 2 (Deep Learning)
- Create comprehensive notes

**3 Days Before:**
- Finish Phase 2
- Start Phase 3 (Active Recall)
- Review entire guide 2-3 times

**1 Day Before:**
- Quick review of all sections
- Focus on definitions and important points
- Test yourself without looking

**Morning of Exam:**
- Quick skim of key concepts
- Stay confident - you've studied well!

---

## βœ… PRE-EXAM CHECKLIST

Before your exam, make sure you can:

- [ ] Explain each AI analysis section in your own words
- [ ] Define all key terms without looking
- [ ] Recall all important points
- [ ] Apply concepts to new examples
- [ ] Teach the material to someone else

*If yes to all - you're ready for 100%! πŸ’ͺ*

---

## πŸ“Š STUDY GUIDE STATISTICS

**AI Processing:**
- Sections Analyzed: {total_chunks}
- AI Model: {"FLAN-T5 (Best for explanations)" if use_flan else "T5"}
- Total Definitions Extracted: {sum(len(s['definitions']) for s in detailed_sections)}
- Total Important Points: {sum(len(s['key_points']) for s in detailed_sections)}

**Quality:**
- βœ… AI-generated detailed explanations
- βœ… Structured for exam preparation
- βœ… Key concepts highlighted
- βœ… Comprehensive coverage

---

*πŸ€– AI-powered detailed analysis for maximum understanding*
*πŸŽ“ Designed for 100% exam success - Good luck!*
"""

        yield study_guide

    except Exception as e:
        yield f"❌ Error: {str(e)}\n\nPlease try uploading the PDF again."

# Create interface
with gr.Blocks(title="AI Study Guide Generator", theme=gr.themes.Soft()) as demo:
    gr.Markdown("""
    # πŸ€– AI-POWERED STUDY GUIDE GENERATOR
    ## Let AI Analyze & Create Detailed Study Notes! 🎯

    **AI analyzes your PDF and creates detailed explanations**
    """)

    with gr.Row():
        with gr.Column(scale=1):
            pdf_input = gr.File(
                label="πŸ“„ Upload Your Study Material (PDF)",
                file_types=[".pdf"]
            )

            detail_level = gr.Radio(
                choices=["Maximum Detail"],
                value="Maximum Detail",
                label="πŸ“Š Detail Level",
                info="AI creates detailed explanations for each section"
            )

            generate_btn = gr.Button(
                "πŸš€ Generate AI Study Guide",
                variant="primary",
                size="lg"
            )

            gr.Markdown("""
            ### πŸ€– AI Model:
            - **FLAN-T5**: Instruction-tuned for explanations
            - Creates detailed study notes
            - Explains concepts clearly
            - Identifies key definitions

            ### ⏱️ Processing Time:
            - Small (< 20 pages): 2-3 min
            - Medium (20-50 pages): 4-6 min
            - Large (50+ pages): 6-10 min

            *100% FREE - Using free AI models!*
            """)

        with gr.Column(scale=2):
            output = gr.Textbox(
                label="πŸ“š Your AI-Generated Study Guide",
                lines=30,
                max_lines=50,
                placeholder="Your detailed AI study guide will appear here...\n\nπŸ€– AI Features:\nβ€’ Detailed explanations of concepts\nβ€’ Key definitions extracted\nβ€’ Important points highlighted\nβ€’ Structured for exam prep\n\nAI analyzes and explains everything! 🎯"
            )

    generate_btn.click(
        fn=create_comprehensive_study_guide,
        inputs=[pdf_input, detail_level],
        outputs=output
    )

    gr.Markdown("""
    ---
    ## 🎯 What Makes This Different:

    ### πŸ€– AI-Powered Analysis:
    - βœ… AI reads and understands your content
    - βœ… Creates detailed explanations
    - βœ… Identifies key concepts automatically
    - βœ… Structures information for learning

    ### πŸ“š Perfect For:
    - πŸŽ“ Exam preparation (Get 100%!)
    - πŸ“– Understanding complex topics
    - 🧠 Creating study notes
    - ⚑ Quick revision guides

    ---

    **πŸ€– AI-powered. Detailed analysis. 100% success!**
    """)

if __name__ == "__main__":
    demo.queue()
    demo.launch()