File size: 31,646 Bytes
48c03b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cfb8648
 
 
 
 
 
 
 
 
 
48c03b7
 
 
cfb8648
 
48c03b7
 
cfb8648
 
48c03b7
 
 
 
 
cfb8648
48c03b7
 
 
 
 
cc8ea38
 
 
 
 
 
 
 
 
 
cfb8648
48c03b7
cfb8648
 
 
 
 
 
 
 
 
 
 
48c03b7
 
 
 
 
cfb8648
 
48c03b7
 
 
 
 
 
 
 
cfb8648
48c03b7
 
 
cfb8648
48c03b7
 
 
 
 
cc8ea38
 
 
 
 
 
 
 
 
 
cfb8648
48c03b7
 
 
 
 
cfb8648
48c03b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fdd1055
 
48c03b7
 
 
 
 
 
 
 
 
fdd1055
 
 
 
 
48c03b7
fdd1055
48c03b7
fdd1055
 
48c03b7
 
 
 
 
 
 
 
 
 
 
fdd1055
 
 
 
 
48c03b7
fdd1055
48c03b7
 
 
 
 
 
 
 
 
 
 
 
 
 
cc8ea38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48c03b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18f3fb8
 
 
48c03b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18f3fb8
48c03b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cc8ea38
 
 
 
 
 
 
 
 
 
 
 
 
 
48c03b7
fdd1055
 
48c03b7
 
 
cfb8648
fdd1055
 
48c03b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cc8ea38
eb2bf1b
cc8ea38
 
eb2bf1b
 
cc8ea38
 
48c03b7
 
 
 
 
 
 
 
 
 
 
 
 
cc8ea38
48c03b7
 
 
451973f
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
import gradio as gr
import os
import warnings
from pathlib import Path
import fitz  # PyMuPDF
from langchain_community.embeddings import HuggingFaceEmbeddings
from langchain_community.vectorstores import Chroma
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain
import anthropic
import base64
from PIL import Image
import io
import re
import random
from dotenv import load_dotenv

# Suppress deprecation warnings
warnings.filterwarnings("ignore", category=DeprecationWarning)

# Load environment variables from .env file
load_dotenv()

# --- Code Practice Assistant ---

class CodePracticeAssistant:
    def __init__(self, curriculum_assistant):
        self.curriculum_assistant = curriculum_assistant
        self.anthropic_client = None
        self._setup_llm()
        
    def _setup_llm(self):
        """Setup Claude LLM for code practice"""
        try:
            self.anthropic_client = anthropic.Anthropic(
                api_key=os.environ.get("ANTHROPIC_KEY")
            )
            print("βœ… Code Practice LLM setup successful!")
        except Exception as e:
            print(f"❌ Error setting up Code Practice LLM: {e}")
            self.anthropic_client = None

    def generate_practice_problem(self, topic, problem_type):
        """Generate a practice problem based on topic and curriculum content"""
        if not self.anthropic_client:
            return "LLM not available. Please check your API key.", ""
        
        # First, find relevant curriculum content for the topic
        curriculum_content = self._find_curriculum_content(topic)
        
        # Map dropdown choices to internal problem types
        problem_type_mapping = {
            "Create Practice Problems": "create",
            "Debug - Identify Error Type": "debug_error_type", 
            "Debug - Explain Error Reason": "debug_error_reason",
            "Debug - Fix the Error": "debug_fix",
            "Optimize Code Performance": "optimize"
        }
        
        internal_type = problem_type_mapping.get(problem_type, "create")
        
        problem_types = {
            "create": "Create a coding problem where students need to write code from scratch",
            "debug_error_type": "Create a coding problem with a bug where students need to identify what type of error it is",
            "debug_error_reason": "Create a coding problem with a bug where students need to explain why the error occurs",
            "debug_fix": "Create a coding problem with a bug where students need to fix the code",
            "optimize": "Create a coding problem where students need to optimize/improve the code performance"
        }
        
        # Check if we have curriculum content or need to use generic approach
        if "No specific curriculum content found" in curriculum_content or "could not be retrieved" in curriculum_content:
            # Use generic prompt without curriculum context
            # Add randomization for diversity
            difficulty_levels = ["beginner", "intermediate", "challenging"]
            problem_styles = [
                "real-world scenario", "mathematical calculation", "data processing", 
                "game logic", "text manipulation", "pattern recognition", "algorithm implementation"
            ]
            
            selected_difficulty = random.choice(difficulty_levels)
            selected_style = random.choice(problem_styles)
            
            prompt = f"""Create a programming practice problem for a student learning {topic}.

Problem Type: {problem_types.get(internal_type, internal_type)}
Difficulty: {selected_difficulty}
Style: {selected_style}

Requirements:
- Make it appropriate for {selected_difficulty} level
- Use a {selected_style} approach
- Include clear instructions
- Provide a specific, focused problem
- If it's a debug problem, include the buggy code
- If it's an optimization problem, provide the original code
- Make it engaging and educational
- Be creative and diverse - avoid repetitive problems

Format your response as:
PROBLEM: [The problem description and requirements]
CODE: [Any starter code if applicable, or "Write your code here:"]

IMPORTANT: If you include example outputs, format them as code blocks like this:
```
Example Output:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
```

Keep it concise but clear and make it different from typical textbook problems."""
        else:
            # Use curriculum-based prompt with diversity
            # Add randomization for diversity even with curriculum
            difficulty_levels = ["beginner", "intermediate", "challenging"]
            problem_styles = [
                "real-world scenario", "mathematical calculation", "data processing", 
                "game logic", "text manipulation", "pattern recognition", "algorithm implementation"
            ]
            
            selected_difficulty = random.choice(difficulty_levels)
            selected_style = random.choice(problem_styles)
            
            prompt = f"""Create a programming practice problem for a student learning {topic}.

Curriculum Context: {curriculum_content}

Problem Type: {problem_types.get(internal_type, internal_type)}
Difficulty: {selected_difficulty}
Style: {selected_style}

Requirements:
- Base the problem on the curriculum content provided above
- Match the difficulty level of what's taught in the curriculum
- Use the same concepts, terminology, and examples from the curriculum
- Make it appropriate for the skill level shown in the curriculum
- Include clear instructions that reference the curriculum concepts
- Provide a specific, focused problem that reinforces what they learned
- Use a {selected_style} approach to make it diverse
- If it's a debug problem, include the buggy code
- If it's an optimization problem, provide the original code
- Make it engaging and educational
- Be creative and diverse - avoid repetitive problems

Format your response as:
PROBLEM: [The problem description and requirements]
CODE: [Any starter code if applicable, or "Write your code here:"]

IMPORTANT: If you include example outputs, format them as code blocks like this:
```
Example Output:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
```

Keep it concise but clear and aligned with the curriculum difficulty while being creative and diverse."""

        try:
            response = self.anthropic_client.messages.create(
                model="claude-3-5-haiku-20241022",
                max_tokens=1000,
                temperature=0.9,  # Higher temperature for more diversity
                messages=[{"role": "user", "content": prompt}]
            )
            
            result = response.content[0].text.strip()
            
            # Parse the response to separate problem and code
            if "PROBLEM:" in result and "CODE:" in result:
                parts = result.split("CODE:")
                problem = parts[0].replace("PROBLEM:", "").strip()
                code = parts[1].strip() if len(parts) > 1 else ""
            else:
                problem = result
                code = ""
            
            return problem, code
            
        except Exception as e:
            return f"Error generating problem: {str(e)}", ""

    def _find_curriculum_content(self, topic):
        """Find relevant curriculum content for the given topic"""
        try:
            # Use the curriculum assistant's vector search to find relevant content
            results = self.curriculum_assistant.vector_db.similarity_search(topic, k=3)
            
            if not results:
                return f"No specific curriculum content found for '{topic}'. Please check the topic name."
            
            # Combine the most relevant curriculum content
            curriculum_content = []
            for i, result in enumerate(results):
                filename = result.metadata['filename']
                page_num = result.metadata['page_number']
                content = result.page_content[:500]  # Limit content length
                curriculum_content.append(f"Slide {i+1} ({filename} - Page {page_num}):\n{content}")
            
            return "\n\n".join(curriculum_content)
            
        except Exception as e:
            print(f"Error finding curriculum content: {e}")
            return f"Curriculum content for '{topic}' could not be retrieved."

    def analyze_student_code(self, topic, problem_type, problem_description, student_code):
        """Analyze student's code and provide feedback based on curriculum"""
        if not self.anthropic_client:
            return "LLM not available. Please check your API key."
        
        # Get curriculum content for context
        curriculum_content = self._find_curriculum_content(topic)
        
        # Map dropdown choices to internal problem types
        problem_type_mapping = {
            "Create Practice Problems": "create",
            "Debug - Identify Error Type": "debug_error_type", 
            "Debug - Explain Error Reason": "debug_error_reason",
            "Debug - Fix the Error": "debug_fix",
            "Optimize Code Performance": "optimize"
        }
        
        internal_type = problem_type_mapping.get(problem_type, "create")
        
        analysis_types = {
            "create": "Evaluate the code for correctness, completeness, and best practices",
            "debug_error_type": "Identify what type of error the code has and explain it",
            "debug_error_reason": "Explain why the error occurs in the code",
            "debug_fix": "Provide the corrected code and explain the fixes",
            "optimize": "Suggest optimizations and explain how they improve performance"
        }
        
        # Check if we have curriculum content or need to use generic approach
        if "No specific curriculum content found" in curriculum_content or "could not be retrieved" in curriculum_content:
            # Use generic analysis prompt - Tutor style
            prompt = f"""You are a helpful programming tutor. Analyze this student's code and provide step-by-step guidance.

Problem Type: {problem_type}
Problem Description: {problem_description}

Student's Code:
{student_code}

Analysis Type: {analysis_types.get(internal_type, "General analysis")}

Provide a CONCISE, step-by-step analysis:
1. **What's Working** (1-2 sentences)
2. **Areas to Improve** (2-3 specific points)
3. **Step-by-Step Solution** (guide them through the process, don't give complete code)
4. **Key Takeaway** (1 sentence)

IMPORTANT: Guide them to figure out the solution themselves. Don't provide complete working code unless they're completely stuck. Focus on hints and direction."""
        else:
            # Use curriculum-based analysis prompt - Tutor style
            prompt = f"""You are a helpful programming tutor. Analyze this student's code and provide step-by-step guidance.

Curriculum Context: {curriculum_content}

Problem Type: {problem_type}
Problem Description: {problem_description}

Student's Code:
{student_code}

Analysis Type: {analysis_types.get(internal_type, "General analysis")}

Provide a CONCISE, step-by-step analysis:
1. **What's Working** (1-2 sentences)
2. **Areas to Improve** (2-3 specific points)
3. **Step-by-Step Solution** (guide them through the process, don't give complete code)
4. **Key Takeaway** (1 sentence)

IMPORTANT: Guide them to figure out the solution themselves. Don't provide complete working code unless they're completely stuck. Focus on hints and direction based on curriculum concepts."""

        try:
            response = self.anthropic_client.messages.create(
                model="claude-3-5-haiku-20241022",
                max_tokens=1500,
                temperature=0.7,
                messages=[{"role": "user", "content": prompt}]
            )
            
            return response.content[0].text.strip()
            
        except Exception as e:
            return f"Error analyzing code: {str(e)}"

    def execute_code(self, code):
        """Execute the student's code and return the output"""
        try:
            import subprocess
            import sys
            from io import StringIO
            import contextlib
            
            # Create a safe execution environment
            local_vars = {}
            output_buffer = StringIO()
            
            # Capture stdout and stderr
            with contextlib.redirect_stdout(output_buffer), contextlib.redirect_stderr(output_buffer):
                try:
                    # Execute the code
                    exec(code, {"__builtins__": __builtins__}, local_vars)
                    output = output_buffer.getvalue()
                    
                    if output.strip():
                        return f"$ python code.py\n{output}"
                    else:
                        return f"$ python code.py\n# Code executed successfully but no output produced"
                        
                except Exception as e:
                    error_output = output_buffer.getvalue()
                    if error_output.strip():
                        return f"$ python code.py\n{error_output}\nError: {str(e)}"
                    else:
                        return f"$ python code.py\nError: {str(e)}"
                    
        except Exception as e:
            return f"$ python code.py\nError: Could not execute code - {str(e)}"

# --- LLM-Powered Curriculum Assistant ---

class LLMCurriculumAssistant:
    def __init__(self, slides_dir="Slides"):
        self.pdf_pages = {}  # {filename: {page_num: text}}
        self.pdf_files = {}  # {filename: path}
        self.chunks = []
        self.chunk_metadata = []
        self.vector_db = None
        self.embeddings = None
        self.llm = None
        self.content_selection_chain = None
        self.answer_chain = None
        
        # Setup
        self._process_pdfs(slides_dir)
        self._build_vector_db()
        self._setup_llm()
        
    def _process_pdfs(self, slides_dir):
        """Process PDFs and extract text"""
        slides_path = Path(slides_dir)
        pdf_files = list(slides_path.glob("*.pdf"))
        
        for pdf_file in pdf_files:
            self.pdf_files[pdf_file.name] = str(pdf_file)
            doc = fitz.open(str(pdf_file))
            pages = {}
            
            for page_num in range(len(doc)):
                page = doc[page_num]
                text = page.get_text()
                if text.strip():
                    pages[page_num + 1] = text.strip()
            
            self.pdf_pages[pdf_file.name] = pages
            doc.close()
            
            # Add each page as a chunk
            for page_num, text in pages.items():
                self.chunks.append(text)
                self.chunk_metadata.append({
                    "filename": pdf_file.name,
                    "page_number": page_num
                })
        
        print(f"βœ… Processed {len(pdf_files)} PDF files with {len(self.chunks)} total pages")

    def _build_vector_db(self):
        """Build vector database for semantic search"""
        self.embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
        self.vector_db = Chroma.from_texts(
            texts=self.chunks,
            embedding=self.embeddings,
            metadatas=self.chunk_metadata,
            persist_directory="./chroma_db"
        )
        print("βœ… Vector database built successfully")

    def _setup_llm(self):
        """Setup Claude LLM"""
        try:
            # Initialize Claude client
            self.anthropic_client = anthropic.Anthropic(
                api_key=os.environ.get("ANTHROPIC_KEY")
            )
            
            # Create content selection prompt
            content_selection_template = """Hi! I'm helping a student find the best curriculum slide for their question. 
The student asked: "{question}"
Here are some slides that might be relevant:
{slide_contents}
Could you help me pick the slide that best answers their specific question? Look for:
- Slides that specifically mention what they're asking about
- Slides with clear explanations and examples
- Slides that match the exact terms they used (like "for loops" vs just "loops")
Just respond with the slide number (1, 2, 3, etc.) that you think is most helpful. If none really fit, say "0".
Thanks! Slide number:"""
            
            self.content_selection_prompt = PromptTemplate(
                input_variables=["question", "slide_contents"],
                template=content_selection_template
            )
            
            # Create answer generation prompt
            answer_template = """Hey there! I'm helping a student understand a programming concept. They asked:
"{question}"
Here's what the curriculum slide says about it:
{slide_content}
Could you help me explain this to them in a friendly, educational way? I'd like you to:
- Break it down in simple terms
- Use examples if the slide has them
- Make it step-by-step and easy to follow
- Add some helpful context if the slide is brief
- Use bullet points or lists to make it clear
- Make sure your answer directly addresses what they asked
- Include code examples when relevant
- Provide detailed explanations with examples
- Be comprehensive and educational
Thanks for your help! Here's what I'd tell the student:"""
            
            self.answer_prompt = PromptTemplate(
                input_variables=["question", "slide_content"],
                template=answer_template
            )
            
            print("βœ… LLM setup successful!")
            
        except Exception as e:
            print(f"❌ Error setting up LLM: {e}")
            self.anthropic_client = None
            self.content_selection_prompt = None
            self.answer_prompt = None

    def get_pdf_page_image(self, pdf_path, page_num):
        """Get PDF page as image"""
        try:
            doc = fitz.open(pdf_path)
            if page_num <= len(doc):
                page = doc[page_num - 1]
                mat = fitz.Matrix(1.5, 1.5)
                pix = page.get_pixmap(matrix=mat)
                img_data = pix.tobytes("png")
                img = Image.open(io.BytesIO(img_data))
                if img.mode != 'RGB':
                    img = img.convert('RGB')
                doc.close()
                return img
            doc.close()
            return None
        except Exception as e:
            print(f"Error rendering PDF page: {str(e)}")
            return None

    def chat(self, query):
        """Main chat function with LLM-powered content selection and answer generation"""
        print(f"\nπŸ” Processing query: {query}")
        
        # Step 1: Vector search to find relevant content
        results = self.vector_db.similarity_search(query, k=5)
        
        if not results:
            return "I couldn't find any relevant content in the curriculum for your question.", [], None, None
        
        print(f"πŸ“š Found {len(results)} relevant slides from vector search")
        
        # Step 2: LLM content selection
        selected_content = None
        selected_result = None
        
        if self.anthropic_client and self.content_selection_prompt:
            try:
                # Prepare slide contents for LLM analysis
                slide_contents = []
                for i, result in enumerate(results):
                    filename = result.metadata['filename']
                    page_num = result.metadata['page_number']
                    content = result.page_content[:800]
                    slide_contents.append(f"Slide {i+1} ({filename} - Page {page_num}):\n{content}")
                
                slide_contents_text = "\n\n".join(slide_contents)
                
                print("πŸ€– Using LLM to select most relevant content...")
                
                # Format the prompt
                prompt = self.content_selection_prompt.format(
                    question=query, 
                    slide_contents=slide_contents_text
                )
                
                # Get LLM's selection
                response = self.anthropic_client.messages.create(
                    model="claude-3-5-haiku-20241022",
                    max_tokens=1500,
                    temperature=0.7,
                    messages=[{"role": "user", "content": prompt}]
                )
                
                selection_response = response.content[0].text
                print(f"LLM Selection Response: {selection_response}")
                
                # Parse the selection
                try:
                    numbers = re.findall(r'\d+', selection_response)
                    if numbers:
                        selected_index = int(numbers[0]) - 1
                        if 0 <= selected_index < len(results):
                            selected_result = results[selected_index]
                            selected_content = selected_result.page_content
                            print(f"βœ… LLM selected slide {selected_index + 1}")
                        else:
                            print(f"⚠️ LLM selection out of range: {selected_index + 1}")
                            selected_result = results[0]
                            selected_content = selected_result.page_content
                    else:
                        print("⚠️ No number found in LLM response, using first result")
                        selected_result = results[0]
                        selected_content = selected_result.page_content
                        
                except Exception as e:
                    print(f"Error parsing LLM selection: {e}")
                    selected_result = results[0]
                    selected_content = selected_result.page_content
                    
            except Exception as e:
                print(f"Error in LLM content selection: {e}")
                selected_result = results[0]
                selected_content = selected_result.page_content
        else:
            # Fallback to first result
            selected_result = results[0]
            selected_content = selected_result.page_content
        
        # Step 3: LLM answer generation
        answer = ""
        if self.anthropic_client and self.answer_prompt and selected_content:
            try:
                print("πŸ€– Generating LLM answer...")
                
                # Format the prompt
                prompt = self.answer_prompt.format(
                    question=query, 
                    slide_content=selected_content
                )
                
                # Get LLM's answer
                response = self.anthropic_client.messages.create(
                    model="claude-3-5-haiku-20241022",
                    max_tokens=2000,
                    temperature=0.7,
                    messages=[{"role": "user", "content": prompt}]
                )
                
                answer = response.content[0].text.strip()
                print(f"βœ… LLM answer generated: {answer[:100]}...")
                
            except Exception as e:
                print(f"Error generating LLM answer: {e}")
                answer = f"Based on the curriculum slide:\n\n{selected_content}\n\nThis slide contains relevant information about your question."
        else:
            answer = f"Based on the curriculum slide:\n\n{selected_content}\n\nThis slide contains relevant information about your question."
        
        # Step 4: Get relevant slides for display
        relevant_slides = []
        if selected_result:
            filename = selected_result.metadata["filename"]
            page_number = selected_result.metadata["page_number"]
            
            if filename in self.pdf_files:
                pdf_path = self.pdf_files[filename]
                doc = fitz.open(pdf_path)
                total_pages = len(doc)
                doc.close()
                
                # Get the selected page and neighboring pages
                start_page = max(1, page_number - 2)
                end_page = min(total_pages, page_number + 2)
                
                for page_num in range(start_page, end_page + 1):
                    img = self.get_pdf_page_image(pdf_path, page_num)
                    if img:
                        if page_num == page_number:
                            label = f"πŸ“Œ {filename} - Page {page_num} (Most Relevant)"
                        else:
                            label = f"{filename} - Page {page_num}"
                        relevant_slides.append((img, label))
                
                recommended_slide = relevant_slides[0][0] if relevant_slides else None
                recommended_label = relevant_slides[0][1] if relevant_slides else None
            else:
                recommended_slide = None
                recommended_label = None
        else:
            recommended_slide = None
            recommended_label = None
        
        return answer, relevant_slides, recommended_slide, recommended_label

# --- Gradio UI ---
assistant = LLMCurriculumAssistant()
practice_assistant = CodePracticeAssistant(assistant)

def gradio_chat(query):
    """Gradio chat interface"""
    answer, relevant_slides, recommended_slide, recommended_label = assistant.chat(query)
    return answer, relevant_slides

def generate_problem(topic, problem_type):
    """Generate a practice problem"""
    problem, code = practice_assistant.generate_practice_problem(topic, problem_type)
    return problem, code

def analyze_code(topic, problem_type, problem_description, student_code):
    """Analyze student's code and execute it"""
    try:
        # Execute the code to get terminal output
        terminal_output = practice_assistant.execute_code(student_code)
    except Exception as e:
        terminal_output = f"$ python code.py\nError: Could not execute code - {str(e)}"
    
    try:
        # Get LLM analysis (this should always run regardless of execution errors)
        analysis = practice_assistant.analyze_student_code(topic, problem_type, problem_description, student_code)
    except Exception as e:
        analysis = f"Error getting LLM analysis: {str(e)}"
    
    return terminal_output, analysis



with gr.Blocks(title="LLM Curriculum Assistant", theme=gr.themes.Soft()) as demo:
    gr.Markdown("# πŸ€– LLM Curriculum Assistant\nYour AI programming tutor with LLM-powered content selection and code practice!")
    

    
    with gr.Tabs() as tabs:
        # Tab 1: Chat Assistant
        with gr.Tab("πŸ’¬ Chat Assistant"):
            with gr.Row():
                # Left Column - Chatbot Interface
                with gr.Column(scale=1):
                    gr.Markdown("### πŸ’¬ Chatbot")
                    gr.Markdown("**Ask questions about programming concepts:**")
                    
                    question = gr.Textbox(
                        label="Question Input", 
                        placeholder="e.g., What are for loops? How do variables work? Explain functions...", 
                        lines=3
                    )
                    submit = gr.Button("πŸ€– Ask AI", variant="primary", size="lg")
                    answer = gr.Markdown(label="LLM Generated Answer")
                
                # Right Column - Slides Display
                with gr.Column(scale=1):
                    gr.Markdown("### πŸ“„ Most Relevant Slides")
                    gallery = gr.Gallery(
                        label="Curriculum Slides", 
                        columns=1, 
                        rows=3, 
                        height="600px", 
                        object_fit="contain",
                        show_label=False
                    )
            
            # Event handlers for chat
            submit.click(fn=gradio_chat, inputs=[question], outputs=[answer, gallery])
            question.submit(fn=gradio_chat, inputs=[question], outputs=[answer, gallery])
        
        # Tab 2: Code Practice
        with gr.Tab("πŸ’» Code Practice"):
            gr.Markdown("### 🎯 Practice Programming Skills")
            gr.Markdown("Choose a topic and problem type to get started!")
            
            with gr.Row():
                # Left Column - Problem Setup
                with gr.Column(scale=1):
                    gr.Markdown("#### πŸ“ Problem Setup")
                    
                    topic_input = gr.Textbox(
                        label="Topic to Practice",
                        placeholder="e.g., for loops, functions, variables, arrays, recursion...",
                        lines=2
                    )
                    
                    problem_type = gr.Dropdown(
                        label="Problem Type",
                        choices=[
                            "Create Practice Problems",
                            "Debug - Identify Error Type",
                            "Debug - Explain Error Reason", 
                            "Debug - Fix the Error",
                            "Optimize Code Performance"
                        ],
                        value="Create Practice Problems"
                    )
                    
                    generate_btn = gr.Button("🎲 Generate Problem", variant="primary", size="lg")
                    
                    gr.Markdown("#### πŸ“‹ Problem Description")
                    problem_description = gr.Markdown(label="Problem will appear here...")
                    
                    gr.Markdown("#### πŸ’» Starter Code (if applicable)")
                    starter_code = gr.Code(
                        label="Code Editor",
                        language="python",
                        lines=10,
                        value="# Write your code here..."
                    )
                
                # Right Column - Student Work & Analysis
                with gr.Column(scale=1):
                    gr.Markdown("#### ✍️ Your Solution")
                    
                    student_code = gr.Code(
                        label="Your Code",
                        language="python",
                        lines=15,
                        value="# Write your solution here..."
                    )
                    
                    analyze_btn = gr.Button("πŸ” Analyze My Code", variant="secondary", size="lg")
                    
                    gr.Markdown("#### πŸ’» Code Execution Output")
                    terminal_output = gr.Textbox(
                        label="Terminal Output",
                        lines=8,
                        value="# Code execution output will appear here...",
                        interactive=False
                    )
                    
                    gr.Markdown("#### πŸ“Š AI Analysis")
                    analysis_output = gr.Markdown(label="Analysis will appear here...")
            
            # Event handlers for practice
            generate_btn.click(
                fn=generate_problem,
                inputs=[topic_input, problem_type],
                outputs=[problem_description, starter_code]
            )
            
            analyze_btn.click(
                fn=analyze_code,
                inputs=[topic_input, problem_type, problem_description, student_code],
                outputs=[terminal_output, analysis_output]
            )

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