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()