Spaces:
Sleeping
Sleeping
aladhefafalquran
Fix: Use FLAN-T5 for AI analysis and detailed explanations (NOT summarization)
dbabd5b
| 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() | |