File size: 36,524 Bytes
bfc1cf6
 
 
 
 
2f3d61a
6439bc9
64d6a1c
 
56c0514
d29ccbb
 
6439bc9
 
64d6a1c
 
 
 
6439bc9
bfc1cf6
2f3d61a
516fc47
 
 
 
 
2f3d61a
516fc47
 
d29ccbb
 
 
b56d8f3
 
389c70e
 
d29ccbb
389c70e
 
 
 
 
 
d29ccbb
389c70e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
543c39f
d29ccbb
 
 
 
 
 
 
543c39f
d29ccbb
 
 
 
 
 
 
 
 
 
543c39f
d29ccbb
 
 
 
 
 
 
 
 
 
543c39f
d29ccbb
 
543c39f
5e14f9b
 
86bebd0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5e14f9b
 
 
 
 
6ec21c9
5e14f9b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d29ccbb
3646962
 
add2681
3646962
 
 
 
 
 
 
406c0d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3646962
 
 
 
406c0d6
3646962
 
406c0d6
 
 
 
3646962
 
406c0d6
 
 
 
 
 
 
 
 
 
 
 
 
3646962
 
 
 
 
 
 
406c0d6
 
 
 
3646962
 
 
406c0d6
 
 
 
 
3646962
406c0d6
3646962
 
 
406c0d6
3646962
 
 
 
406c0d6
3646962
406c0d6
 
 
3646962
 
406c0d6
 
3646962
 
 
406c0d6
 
 
 
3646962
406c0d6
3646962
 
 
 
 
406c0d6
 
 
 
3646962
2841710
 
 
 
 
 
 
 
 
 
 
 
2171ced
2841710
 
 
 
 
 
 
 
 
 
2171ced
2841710
 
 
 
 
 
 
 
 
 
2171ced
2841710
 
 
 
 
 
 
 
 
 
2171ced
2841710
 
 
 
 
 
 
 
 
 
 
 
 
63cea89
2841710
 
 
 
 
9682d40
 
6439bc9
 
6b1a461
 
 
6e9b3c0
6b1a461
 
6439bc9
 
 
6e9b3c0
 
 
6439bc9
 
 
 
 
 
 
9682d40
64d6a1c
 
 
 
 
 
 
 
 
 
 
 
 
ac1b43b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c515f30
ac1b43b
c515f30
ac1b43b
 
 
 
 
 
 
 
6e9b3c0
02551fc
1871af1
6439bc9
 
 
 
6e9b3c0
6439bc9
 
 
 
 
 
6b1a461
e6857f6
6439bc9
bfc1cf6
2f3d61a
 
9682d40
 
2429022
 
 
 
 
 
 
2f3d61a
 
6e9b3c0
 
1871af1
2841710
ac1b43b
22e8e0a
d29ccbb
2429022
 
 
 
 
 
ac1b43b
 
2429022
 
 
9eb3a05
2429022
 
 
 
 
c8ed281
8191966
 
 
 
 
 
 
63ad224
c8ed281
 
d29ccbb
 
acfcd3f
f6e4af4
 
 
 
 
3646962
 
 
 
 
6439bc9
bfc1cf6
2841710
2a474e7
6b1a461
bfc1cf6
 
6e9b3c0
bfc1cf6
 
 
6439bc9
 
 
 
d04b940
6439bc9
7748dcf
 
4a59421
bbf54a8
7748dcf
bbf54a8
 
02551fc
 
 
 
c8ed281
02551fc
 
 
bbf54a8
 
6439bc9
9814205
 
 
6439bc9
64d6a1c
6e9b3c0
 
9814205
64d6a1c
9814205
64d6a1c
fbc3848
9223a59
 
 
 
4a59421
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
543c39f
 
fab49e1
543c39f
 
5e14f9b
543c39f
 
 
 
 
 
 
 
 
 
 
5e14f9b
543c39f
d29ccbb
543c39f
 
 
 
fab49e1
543c39f
4a59421
8975086
4a59421
 
5e14f9b
 
 
4a59421
 
5e14f9b
 
bbf54a8
 
18205a7
02551fc
bbf54a8
 
5e14f9b
 
 
c8ed281
 
5e14f9b
 
 
 
 
 
 
7983613
 
 
c8ed281
18205a7
bbf54a8
 
18205a7
5e14f9b
 
 
 
bbf54a8
 
 
 
 
f6e4af4
bbf54a8
 
 
 
 
 
f6e4af4
 
 
 
18205a7
f6e4af4
02551fc
 
7983613
 
 
 
 
 
 
 
 
02551fc
7983613
fbc3848
7983613
 
 
 
 
 
 
 
6f7bf4c
7983613
fbc3848
7983613
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fbc3848
 
7983613
 
02551fc
 
f6e4af4
bbf54a8
 
02551fc
 
 
 
 
 
86bebd0
02551fc
 
 
 
3e6b08c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c8ed281
3e6b08c
02551fc
 
 
 
 
 
 
 
 
 
 
 
 
 
f6e4af4
02551fc
 
 
 
 
 
 
 
 
7983613
91aaf8c
9682d40
6e9b3c0
 
 
9682d40
 
1871af1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6e9b3c0
1871af1
 
 
 
 
 
 
 
2841710
1871af1
6e9b3c0
 
 
 
 
 
1871af1
 
6e9b3c0
9682d40
 
6e9b3c0
91aaf8c
 
 
 
9682d40
 
 
6e9b3c0
 
9682d40
 
6e9b3c0
1871af1
 
 
6e9b3c0
1871af1
6b1a461
9682d40
91aaf8c
 
6439bc9
2f3d61a
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
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
import os
import gradio as gr
from anthropic import Anthropic
from datetime import datetime, timedelta
from collections import deque
import random
import logging
import tempfile
from pathlib import Path
from sympy import *
import json
from pathlib import Path

# Set up logging
logging.basicConfig(
    level=logging.DEBUG,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)

# Initialize Anthropic client
anthropic = Anthropic(
    api_key=os.environ.get('ANTHROPIC_API_KEY')
)

# Request tracking
MAX_REQUESTS_PER_DAY = 25
request_history = deque(maxlen=1000)

SYMPY_GUIDELINES = """
When writing SymPy code to verify solutions:

NOTE: For eigenvalue problems, use 'lam = Symbol('lam')' instead of importing from sympy.abc

1. Variable Declaration and Expressions:
   - ALWAYS create symbolic expressions instead of literal numbers when working with mathematical operations:
     ```python
     # CORRECT:
     x = Symbol('x')
     expr = x + 1  # Creates a symbolic expression
     
     # INCORRECT:
     expr = 1  # This is just a number, can't be differentiated
     ```
   - For polynomials and functions:
     ```python
     # CORRECT:
     x = Symbol('x')
     p = x**2 + 2*x + 1  # Creates a polynomial expression
     
     # INCORRECT:
     p = 1  # This won't work for operations like diff()
     ```
   - When verifying operator actions:
     ```python
     # CORRECT:
     x = Symbol('x')
     def verify_operator(p):
         x = Symbol('x')  # Always use Symbol inside functions too
         return p.subs(x, 1)  # Substitute values after creating expression
     
     # INCORRECT:
     def verify_operator(p):
         return p  # Passing raw numbers won't work
     ```
   - For integration bounds:
     ```python
     # CORRECT:
     t = Symbol('t')
     expr = t**2
     result = integrate(expr, (t, 0, 1))
     
     # INCORRECT:
     result = integrate(2, (t, 0, 1))  # Can't integrate a number
     ```
     
2. Solving and Computing:
   - Never use strings in solve() or other SymPy functions:
     CORRECT:   solve(eq, x)
     INCORRECT: solve(eq, 'x')
   - Define equations symbolically:
     CORRECT:   eq = 2*sqrt(h) - sqrt(12) + 5*k
     INCORRECT: eq = 2*sqrt('h') - sqrt(12) + 5*k

3. Printing and Output:
   - Include print statements for ALL calculations and results
   - Print intermediate steps and final answers
   - Print variable values after they are computed
   - Use simple print statements instead of f-strings for SymPy expressions
   - Print expressions with labels on separate lines:
     ```python
     print("Expression label:")
     print(expression)
     ```

4. Numeric Calculations:
   - Use Float() for decimal numbers in calculations
   - Use float() for final printing of results
   - Avoid evalf() as it may cause errors
   - For numeric results:
     ```python
     result = expression.evalf()
     print("Result:")
     print(float(result))
     ```

5. Working with Series and Sequences:
   - Use Float() for sequence terms
   - Convert sums to float() before printing
   - For series calculations, print intermediate terms   

6. Matrix Operations and Systems of Equations:
   - Never use symbolic variables as matrix indices:
     ```python
     # CORRECT:
     i, j = 0, 1  # Use integers for indexing
     M = Matrix([[1, 2], [3, 4]])
     element = M[i, j]
     
     # INCORRECT:
     x = Symbol('x')
     element = M[x, 0]  # This will raise an error
     ```

   - For matrix analysis, always convert equations to Matrix form:
     ```python
     # CORRECT:
     A = Matrix([[1, 2], [3, 4]])
     eigenvals = A.eigenvals()
     
     # For system of equations:
     x, y = symbols('x y')
     system = Matrix([[2, 1], [1, -1]])
     b = Matrix([5, 1])
     solution = system.solve(b)
     ```
     
   - For matrix operations with variables:
     ```python
     # CORRECT:
     x = Symbol('x')
     M = Matrix([[x, 1], [2, 3]])
     result = M * M  # Matrix multiplication
     
     # INCORRECT:
     M[Symbol('i'), Symbol('j')] = x  # Don't use symbolic indices     
     ```
   - For systems of equations that might be linearly dependent, use row reduction instead of matrix inversion. Here's the template for handling such systems:

```python
from sympy import Matrix, symbols, solve

def analyze_system(A, b):
    'Analyze a system Ax = b using row reduction. Returns whether solution exists and if it's unique.'
    # Augmented matrix [A|b]
    aug = Matrix(A.row_join(b))
    
    # Get row echelon form
    rref, pivots = aug.rref()
    
    print("Row reduced augmented matrix:")
    print(rref)
    print("\\nPivot columns:", pivots)
    
    # Get rank of coefficient matrix and augmented matrix
    rank_A = Matrix(A).rank()
    rank_aug = aug.rank()
    
    print(f"\\nRank of coefficient matrix: {rank_A}")
    print(f"Rank of augmented matrix: {rank_aug}")
    
    if rank_aug > rank_A:
        print("\\nNo solution exists")
        return None
    elif rank_A < A.cols:
        print("\\nInfinitely many solutions exist")
        return "infinite"
    else:
        print("\\nUnique solution exists")
        return "unique"

# When solving a system Ax = b:
A = Matrix([[...], [...], [...]])  # coefficient matrix
b = Matrix([[...], [...], [...]])  # right-hand side

# Analyze system
result = analyze_system(A, b)

if result == "infinite":
    # Get parametric form of solution
    aug = Matrix(A.row_join(b))
    rref, pivots = aug.rref()
    
    # Get free variables
    vars = symbols('x y z')  # adjust variable names as needed
    free_vars = [var for i, var in enumerate(vars) if i not in pivots]
    
    print("\\nParametric solution (t is free parameter):")
    for i, var in enumerate(vars):
        if i in pivots:
            row = pivots.index(i)
            expr = rref[row, -1]
            for j, free_var in enumerate(free_vars):
                expr -= rref[row, pivots[-1] + 1 + j] * free_var
            print(f"{var} = {expr}")
        else:
            print(f"{var} = t")  # use different parameter names for multiple free variables
```
Always use this template when working with systems of equations to handle potential linear dependence correctly. """

def load_proof_repository():
    """Load the proof repository from the repository file"""
    repo_path = Path("Lebl-theorems-all.json")
    try:
        with open(repo_path, "r") as f:
            return json.load(f)
    except Exception as e:
        logger.error(f"Error loading proof repository: {str(e)}")
        return None

TOPIC_MAPPINGS = {
    "integration": ["integral", "integrable", "riemann", "integrate", "antiderivative"],
    "continuity": ["continuous", "discontinuous", "discontinuity", "uniformly continuous"],
    "sequences": ["sequence", "convergent", "divergent", "monotone", "subsequence"],
    "series": ["series", "sum", "convergent series", "power series"],
    "differentiation": ["derivative", "differentiable", "differential"],
    "limits": ["limit", "cluster point", "accumulation"],
    "functions": ["function", "mapping", "surjective", "injective", "bijective"],
    "bounded": ["bound", "bounded above", "bounded below", "supremum", "infimum"]
}

def get_related_terms(topic):
    """Get all related terms for a given topic"""
    # Get direct mappings
    related = TOPIC_MAPPINGS.get(topic.lower(), [])
    # Add the original topic
    related.append(topic.lower())
    # Remove duplicates while preserving order
    return list(dict.fromkeys(related))

def matches_topic(text, topic_terms):
    """Check if any topic terms appear in the text"""
    text_lower = text.lower()
    return any(term in text_lower for term in topic_terms)

def get_relevant_proofs(topic):
    """Get relevant proofs from repository based on topic, randomly selecting examples"""
    repository = load_proof_repository()
    if not repository:
        logger.error("Failed to load proof repository")
        return []
    
    logger.debug(f"Searching for proofs related to topic: {topic}")
    topic_terms = get_related_terms(topic)
    logger.debug(f"Related terms: {topic_terms}")
    
    relevant_proofs = []
    for theorem in repository.get("dataset", {}).get("theorems", []):
        # Check categories
        categories = theorem.get("categories", [])
        category_match = any(matches_topic(cat, topic_terms) for cat in categories)
        
        # Check contents
        contents = theorem.get("contents", [])
        content_match = any(matches_topic(content, topic_terms) for content in contents)
        
        # Check title
        title = theorem.get("title", "")
        title_match = matches_topic(title, topic_terms)
        
        if (category_match or content_match or title_match):
            if theorem.get("contents") and theorem.get("proofs"):
                proof_content = {
                    "title": theorem.get("title", ""),
                    "contents": theorem.get("contents", []),
                    "proofs": [p.get("contents", []) for p in theorem.get("proofs", [])]
                }
                relevant_proofs.append(proof_content)
                logger.debug(f"Found matching proof: {proof_content['title']}")
                logger.debug(f"Matched via: {'categories' if category_match else 'contents' if content_match else 'title'}")

    logger.debug(f"Found {len(relevant_proofs)} relevant proofs before sampling")
    
    # Randomly select 3 proofs if we have more than 3
    if len(relevant_proofs) > 3:
        selected = random.sample(relevant_proofs, 3)
        logger.debug("Selected proofs for enhancement:")
        for proof in selected:
            logger.debug(f"- {proof['title']}")
        return selected
    return relevant_proofs
    
def enhance_prompt_with_proofs(system_prompt, subject, topic):
    """Enhance the system prompt with relevant proofs if subject is Real Analysis"""
    if subject != "Real Analysis":
        logger.debug("Skipping proof enhancement - not Real Analysis")
        return system_prompt
        
    relevant_proofs = get_relevant_proofs(topic)
    if not relevant_proofs:
        logger.debug(f"No relevant proofs found for topic: {topic}")
        return system_prompt
    
    logger.debug(f"Enhancing prompt with {len(relevant_proofs)} proofs")
    
    # Add proof examples to the prompt
    proof_examples = "\n\nReference these proof examples for style and approach:\n"
    for proof in relevant_proofs:
        logger.debug(f"Adding proof: {proof['title']}")
        proof_examples += f"\nTheorem: {proof['title']}\n"
        proof_examples += "Statement: " + " ".join(proof['contents']) + "\n"
        if proof['proofs']:
            first_proof = " ".join(proof['proofs'][0])
            logger.debug(f"Proof length: {len(first_proof)} characters")
            proof_examples += "Proof: " + first_proof + "\n"
    
    # Add specific instructions for using the examples
    enhanced_prompt = f"""{system_prompt}
ADDITIONAL PROOF GUIDELINES:
1. Consider the following proof examples from established textbooks
2. Maintain similar level of rigor and detail
3. Use similar proof techniques where applicable
4. Follow similar notation and presentation style
{proof_examples}"""
    
    return enhanced_prompt
    

def get_difficulty_parameters(difficulty_level):
    """Return specific parameters and constraints based on difficulty level"""
    parameters = {
        1: {  # Very Easy
            "description": "very easy, suitable for beginners",
            "constraints": [
                "Use only basic concepts and straightforward calculations",
                "Break complex problems into smaller, guided steps",
                "Provide hints within the question when needed",
                "Use simple numbers and avoid complex algebraic expressions"
            ],
            "example_style": "Similar to standard homework problems",
            "model": "claude-3-5-sonnet-20241022"
        },
        2: {  # Easy
            "description": "easy, but requiring some thought",
            "constraints": [
                "Use basic concepts with minor complications",
                "Include two-step problems",
                "Minimal guidance provided",
                "Use moderately complex numbers or expressions"
            ],
            "example_style": "Similar to quiz questions",
            "model": "claude-3-5-sonnet-20241022"
        },
        3: {  # Intermediate
            "description": "intermediate difficulty, testing deeper understanding",
            "constraints": [
                "Combine 2-3 related concepts",
                "Include some non-obvious solution paths",
                "Require multi-step reasoning",
                "Use moderate algebraic complexity"
            ],
            "example_style": "Similar to midterm exam questions",
            "model": "claude-3-5-sonnet-20241022"
        },
        4: {  # Difficult
            "description": "challenging, requiring strong mathematical maturity",
            "constraints": [
                "Combine multiple concepts creatively",
                "Require insight and deep understanding",
                "Include non-standard approaches",
                "Use sophisticated mathematical reasoning"
            ],
            "example_style": "Similar to final exam questions",
            "model": "claude-3-5-sonnet-20241022"
        },
        5: {  # Very Difficult
            "description": "very challenging, testing mastery and creativity at a graduate level",
            "constraints": [
                "Create novel applications of theoretical concepts",
                "Require graduate-level mathematical reasoning",
                "Combine multiple advanced topics in unexpected ways",
                "Demand creative problem-solving approaches",
                "Include rigorous proof construction",
                "Require synthesis across mathematical domains",
                "Test deep theoretical understanding"
            ],
            "example_style": "Similar to graduate qualifying exams or advanced competition problems",
            "model": "claude-3-5-sonnet-20241022"
        }
    }
    
    return parameters.get(difficulty_level)

def create_latex_document(content, questions_only=False):
    """Create a complete LaTeX document"""
    try:
        latex_header = r"""\documentclass{article}
\usepackage{amsmath,amssymb}
\usepackage[margin=1in]{geometry}
\begin{document}
\title{Mathematics Question}
\maketitle
"""
        latex_footer = r"\end{document}"
        
        if questions_only:
            # Modified to handle single question
            processed_content = content.split('Solution:')[0]
            content = processed_content
        
        full_document = f"{latex_header}\n{content}\n{latex_footer}"
        logger.debug(f"Created {'questions-only' if questions_only else 'full'} LaTeX document")
        return full_document
    except Exception as e:
        logger.error(f"Error creating LaTeX document: {str(e)}")
        raise

def save_to_temp_file(content, filename):
    """Save content to a temporary file and return the path"""
    try:
        temp_dir = Path(tempfile.gettempdir()) / "math_test_files"
        temp_dir.mkdir(exist_ok=True)
        file_path = temp_dir / filename
        file_path.write_text(content, encoding='utf-8')
        logger.debug(f"Saved content to temporary file: {file_path}")
        return str(file_path)
    except Exception as e:
        logger.error(f"Error saving temporary file: {str(e)}")
        raise

def get_problem_type_addition(question_type):
    """Return specific requirements based on problem type"""
    problem_type_additions = {
        "application": """
The application question MUST:
- Present a real-world scenario or practical problem
- Require modeling the situation mathematically
- Connect abstract mathematical concepts to concrete situations
- Include realistic context and data
- Require students to:
    1. Identify relevant mathematical concepts
    2. Translate the practical problem into mathematical terms
    3. Solve using appropriate mathematical techniques
    4. Interpret the results in the context of the original problem
Example contexts might include:
- Physics applications (motion, forces, work)
- Engineering scenarios (optimization, rates of change)
- Economics problems (cost optimization, growth models)
- Biological systems (population growth, reaction rates)
- Business applications (profit maximization, inventory management)
- Social science applications (demographic models, social network analysis)
- Data science applications (regression, statistical analysis)
""",
        "proof": """
The proof question MUST:
- Require a formal mathematical proof
- Focus on demonstrating logical reasoning
- Require justification for each step
- Emphasize theoretical understanding
The proof question MAY NOT:
- Include Real-world applications or scenarios
- Include Pure computation problems
- Ask only for numerical answers
""",
        "computation": """
The computation question MUST:
- Require specific algebraic calculations
- Focus on mathematical techniques
- Have concrete answers in the form of algebraic expressions (about half of questions) or numbers (about half of questions)
- Test procedural knowledge
The computation question MAY NOT:
- Include extended real-world applications or scenarios
- Ask for a proof
"""
    }
    return problem_type_additions.get(question_type, "")

def generate_question(subject, difficulty, question_type):
    """Generate a single math question with additional verification for difficulty 5"""
    try:
        if not os.environ.get('ANTHROPIC_API_KEY'):
            logger.error("Anthropic API key not found")
            return "Error: Anthropic API key not configured", None, None
        
        logger.debug(f"Generating {question_type} question for subject: {subject} at difficulty level: {difficulty}")
        
        # Check rate limit
        now = datetime.now()
        while request_history and (now - request_history[0]) > timedelta(days=1):
            request_history.popleft()
        if len(request_history) >= MAX_REQUESTS_PER_DAY:
            return "Daily request limit reached. Please try again tomorrow.", None, None
        
        request_history.append(now)
        
        topics = {
            "Single Variable Calculus": ["limits", "derivatives", "integrals", "series", "applications"],
            "Multivariable Calculus": ["partial derivatives", "multiple integrals", "vector fields", "optimization"],
            "Linear Algebra": ["matrices", "vector spaces", "eigenvalues", "linear transformations"],
            "Differential Equations": ["first order equations", "second order equations", "systems", "stability analysis"],
            "Real Analysis": ["sequences", "series", "continuity", "differentiation", "integration"],
            "Complex Analysis": ["complex functions", "analyticity", "contour integration", "residues"],
            "Abstract Algebra": ["groups", "rings", "fields", "homomorphisms"],
            "Probability Theory": ["probability spaces", "random variables", "distributions", "limit theorems"],
            "Numerical Analysis": ["approximation", "interpolation", "numerical integration", "error analysis"],
            "Topology": ["metric spaces", "continuity", "compactness", "connectedness"]
        }
        
        selected_topic = random.choice(topics.get(subject, ["general"]))
        logger.debug(f"Selected topic: {selected_topic}")
        
        difficulty_params = get_difficulty_parameters(difficulty)
        problem_type_addition = get_problem_type_addition(question_type)

        system_prompt = f"""You are an expert mathematics professor creating a {difficulty_params['description']} exam question.
STRICT REQUIREMENTS:
1. Write exactly 1 {question_type} question on {subject} covering {selected_topic}.
2. Difficulty Level Guidelines:
   {difficulty_params['description'].upper()}
   Follow these specific constraints:
   {chr(10).join(f'   - {c}' for c in difficulty_params['constraints'])}
   
   {problem_type_addition}
3. Style Reference:
   Question should be {difficulty_params['example_style']}
4. For LaTeX formatting:
   - Make sure that the question statement uses proper LaTeX math mode 
   - Use $ for inline math
   - Use $$ on separate lines for equations and solutions
   - Put each solution step on its own line in $$ $$
   - DO NOT use \\begin{{aligned}} or similar environments
5. Include a detailed solution
   - Begin the solution with a heading in bold and larger font size: Original Solution
   - If the question involves geometry make sure to identify any general geometric formulas that apply, For example:
        * Areas/volumes of common shapes and solids
        * Cross-sectional areas of geometric figures
        * Arc lengths and sector areas
   - When setting up differential equations either in calculus or differential equation applications
        * carefully consider the direction of change in variables
        * ensure integration bounds align with the physical direction of the process being modeled
6. Maintain clear formatting
7. At the end of the solution output, print SymPy code that you would use to solve or verify the main equations in the question 
   and give this section a heading in bold and larger font size: SymPy Verification
8. Observe the folloiwng SymPy Guidelines
{SYMPY_GUIDELINES}"""

#Consider
   #When writing SymPy code:
   #- Use FiniteSet(1, 2, 3) instead of Set([1, 2, 3]) for finite sets
   #- Import specific functions instead of using 'from sympy import *'
   #- Print results of each calculation step

   # Enhance the prompt with proof examples if applicable
        if subject == "Real Analysis" and question_type == "proof":
            system_prompt = enhance_prompt_with_proofs(system_prompt, subject, selected_topic)
    
        logger.debug("Sending request to Anthropic API")
        message = anthropic.messages.create(
            model=difficulty_params['model'],
            max_tokens=4096,
            temperature=0.7,
            messages=[{
                "role": "user",
                "content": f"{system_prompt}\n\nWrite a question for {subject}."
            }]
        )
        
        if not hasattr(message, 'content') or not message.content:
            logger.error("No content received from Anthropic API")
            return "Error: No content received from API", None, None
        
        response_text = message.content[0].text
        logger.debug("Successfully received response from Anthropic API")

        # Execute SymPy code and append results
        sympy_output = extract_and_run_sympy_code_simple(response_text)

        if sympy_output:
            # Check if SymPy ran successfully
            if "Error" not in sympy_output:
                resolution_text, has_discrepancy, revised_solution = check_and_resolve_discrepancy(response_text, sympy_output)
                response_text = f"{response_text}\n\nSymPy Verification Results:\n```\n{sympy_output}\n```\n\nVerification Analysis:\n{resolution_text}"
                
                # For difficulty level 5 AND when there's a discrepancy with a revised solution
                if has_discrepancy and revised_solution:
                    logger.debug("Performing final verification for difficulty 5 problem with discrepancy")
                    final_verification = perform_final_verification(revised_solution)
                    response_text += "\n\nFinal Expert Verification:\n" + final_verification
            else:
                response_text += f"\n\nSymPy Verification Results:\n```\n{sympy_output}\n```"
        
        # Create LaTeX content
        questions_latex = create_latex_document(response_text, questions_only=True)
        full_latex = create_latex_document(response_text, questions_only=False)
        
        # Save to temporary files
        questions_path = save_to_temp_file(questions_latex, "question.tex")
        full_path = save_to_temp_file(full_latex, "full_question.tex")
        
        logger.debug("Successfully created temporary files")
        
        return response_text, questions_path, full_path
            
    except Exception as e:
        logger.error(f"Error generating question: {str(e)}")
        return f"Error: {str(e)}", None, None
 
def extract_and_run_sympy_code_simple(response_text):
    """
    Extract SymPy code from the response and execute it.
    """
    try:
        # Extract code
        sympy_start = response_text.find('```python')
        if sympy_start == -1:
            return "No SymPy code found in the response."
        
        code_start = response_text.find('\n', sympy_start) + 1
        code_end = response_text.find('```', code_start)
        if code_end == -1:
            return "Malformed SymPy code block."
        
        sympy_code = response_text[code_start:code_end].strip()
        
        # Import SymPy at the module level
        import sympy
        
        # Create globals dict with all SymPy functions
        globals_dict = {}
        globals_dict.update(vars(sympy))
        globals_dict.update({
            'print': print,
            'float': float,
            'Symbol': sympy.Symbol,
            'symbols': sympy.symbols,
            'solve': sympy.solve,
            'sqrt': sympy.sqrt,
            'pi': sympy.pi,
            'diff': sympy.diff,
            'integrate': sympy.integrate,
            'simplify': sympy.simplify,
            'Matrix': sympy.Matrix
        })
        
        # Remove the sympy import line from the code if present
        lines = sympy_code.split('\n')
        filtered_lines = [line for line in lines if not line.strip().startswith('from sympy import') and not line.strip().startswith('import sympy')]
        modified_code = '\n'.join(filtered_lines)
        
        # Capture output
        import io
        from contextlib import redirect_stdout
        
        output_buffer = io.StringIO()
        with redirect_stdout(output_buffer):
            exec(modified_code, globals_dict)
        return output_buffer.getvalue().strip() or "No output produced"
    
    except Exception as e:
        return f"Error executing SymPy code: {str(e)}"

def check_and_resolve_discrepancy(initial_response, sympy_output):
    """
    Compare the SymPy output with the initial response and resolve any discrepancies.
    Returns tuple of (resolution_text, has_discrepancy, revised_solution)
    """
    try:
        resolution_prompt = f"""Here is a mathematics question with two answers.
        The first, called Original solution, is a complete solution.
        The second, called SymPy Verification, will only provide the final answer.

        Begin this section with a heading in bold and larger font size: Comparison of SymPy Verifiation with Original Solution
        
        If the SymPy Verification answer is consistent with the final answer Original solution,
        then please say that they are consistent and briefly explain why. 
        
        If the two answers are inconsistent with each other then please:
        1. Identify which solution is correct
        2. Explain the error in the incorrect solution
        3. Write "Here is the revised complete solution:" and then write out the ENTIRE solution from beginning 
           to end, including all parts that were correct and the corrections for any incorrect parts. 
           Do not refer to the original solution or say things like "the rest remains the same" - write 
           out everything in full. Begin this section with a heading in bold and larger font size: Revised Complete Solution

Original solution:
{initial_response}

SymPy Verification Results:
{sympy_output}

Please maintain the same LaTeX formatting as the original solution."""

        # Make API call for resolution
        message = anthropic.messages.create(
            model="claude-3-5-sonnet-20241022",
            max_tokens=4096,
            temperature=0.2,
            messages=[{
                "role": "user",
                "content": resolution_prompt
            }]
        )
        
        resolution_text = message.content[0].text
        
        # Check if resolution contains new SymPy code
        if "```python" in resolution_text:
            new_sympy_output = extract_and_run_sympy_code_simple(resolution_text)
            resolution_text += "\n\nNew SymPy Verification Results:\n```\n" + new_sympy_output + "\n```"
        
        # Determine if there was a discrepancy that required a revised solution
        # Check for any indication of inconsistency or error
        inconsistency_phrases = [
            "inconsistent", "inconsistency", "incorrect", "error", "wrong",
            "differs", "different", "discrepancy", "mistaken", "mistake"
        ]
        has_discrepancy = any(phrase in resolution_text.lower() for phrase in inconsistency_phrases)
        
        # Look for the required marker phrase and extract the solution after it
        marker = "Here is the revised complete solution:"
        revised_solution = None
        
        if has_discrepancy:
            # Split at the marker
            if marker in resolution_text:
                parts = resolution_text.split(marker, maxsplit=1)
                if len(parts) > 1:
                    revised_solution = parts[1].strip()
                    # If the solution seems too short (might be partial), don't accept it
                    if len(revised_solution) < 100:  # Rough minimum length for a complete solution
                        revised_solution = None
            
            # If we didn't find a complete solution, force a recheck
            if not revised_solution:
                logger.debug("Initial solution extraction failed, requesting a complete solution")
                # Make a new API call specifically requesting a complete solution
                complete_solution_prompt = f"""The previous solution had inconsistencies. Please provide a complete solution 
                from beginning to end. Start your response with exactly this phrase:
                "Here is the revised complete solution:"
                Then write out the entire solution, including all parts both correct and corrected.
                Do not refer to the original solution or say any parts remain the same.
                
                Original problem and verification results:
                {initial_response}
                
                SymPy Results:
                {sympy_output}"""
                
                try:
                    message = anthropic.messages.create(
                        model="claude-3-5-sonnet-20241022",
                        max_tokens=4096,
                        temperature=0.2,
                        messages=[{"role": "user", "content": complete_solution_prompt}]
                    )
                    
                    new_response = message.content[0].text
                    if marker in new_response:
                        parts = new_response.split(marker, maxsplit=1)
                        if len(parts) > 1:
                            revised_solution = parts[1].strip()
                except Exception as e:
                    logger.error(f"Error in solution recheck: {str(e)}")
        
        return resolution_text, has_discrepancy, revised_solution
            
    except Exception as e:
        logger.error(f"Error in discrepancy resolution: {str(e)}")
        return f"Error in resolution: {str(e)}", False, None

def perform_final_verification(revised_solution):
    """
    Perform a final verification of the revised solution for difficulty level 5 problems.
    """
    verification_prompt = f"""As an expert mathematician, please carefully verify this revised solution to an advanced mathematics problem.

Revised Solution to Verify:
{revised_solution}

Please follow these steps exactly:

1. First, analyze the solution for:
   - Mathematical correctness
   - Missing cases or assumptions
   - Completeness and rigor
   - Necessary conditions and edge cases
   - Any subtle errors or oversights

2. After your analysis, write exactly this phrase:
   "Here is the complete verified solution:"

3. Then write out the ENTIRE solution from beginning to end, including:
   - All correct parts from the original solution
   - All needed corrections
   - All additional cases and verifications
   - Any missing steps or assumptions
   - Any necessary additional proofs or derivations

Your complete solution must:
- Be completely self-contained
- Not refer to the original solution
- Show every step of the calculation
- Include all necessary verifications
- Maintain proper LaTeX formatting with $ for inline math and $ on separate lines
- Include any relevant SymPy code for verification

Begin this section with a heading in bold and larger font size: Final Verified Solution
Remember to write out the complete solution even if you only need to add a few steps - the goal is to have a single, complete, verified solution that includes everything necessary for full mathematical rigor."""

    try:
        # Make API call for final verification
        message = anthropic.messages.create(
            model="claude-3-5-sonnet-20241022",
            max_tokens=4096,
            temperature=0.2,
            messages=[{
                "role": "user",
                "content": verification_prompt
            }]
        )
        
        verification_result = message.content[0].text
        
        # If verification includes new SymPy code, run it
        if "```python" in verification_result:
            new_sympy_output = extract_and_run_sympy_code_simple(verification_result)
            verification_result += "\n\nFinal SymPy Verification:\n```\n" + new_sympy_output + "\n```"
        
        return verification_result
    except Exception as e:
        logger.error(f"Error in final verification: {str(e)}")
        return f"Error in final verification: {str(e)}"
        
# Create Gradio interface
with gr.Blocks() as interface:
    gr.Markdown("# Advanced Mathematics Question Generator")
    gr.Markdown("""Generates a unique university-level mathematics question with solution using Claude 3.
    Each question features different topics and difficulty levels. Limited to 25 requests per day.""")
    
    with gr.Row():
        with gr.Column():
            subject_dropdown = gr.Dropdown(
                choices=[
                    "Single Variable Calculus",
                    "Multivariable Calculus", 
                    "Linear Algebra",
                    "Differential Equations",
                    "Real Analysis",
                    "Complex Analysis",
                    "Abstract Algebra",
                    "Probability Theory",
                    "Numerical Analysis",
                    "Topology"
                ],
                label="Select Mathematics Subject",
                info="Choose a subject for the question"
            )
            
            difficulty_slider = gr.Slider(
                minimum=1,
                maximum=5,
                step=1,
                value=3,
                label="Difficulty Level",
                info="1: Very Easy, 2: Easy, 3: Moderate, 4: Difficult, 5: Very Difficult"
            )
            
            question_type = gr.Radio(
                choices=["computation", "proof", "application"],
                label="Question Type",
                info="Select the type of question you want",
                value="computation"
            )
    
    generate_btn = gr.Button("Generate Question")
    
    output_text = gr.Markdown(
        label="Generated Question Preview",
        latex_delimiters=[
            {"left": "$$", "right": "$$", "display": True},
            {"left": "$", "right": "$", "display": False}
        ]
    )
    
    with gr.Row():
        questions_file = gr.File(label="Question Only (LaTeX)")
        full_file = gr.File(label="Question with Solution (LaTeX)")
    
    generate_btn.click(
        generate_question,
        inputs=[
            subject_dropdown,
            difficulty_slider,
            question_type
        ],
        outputs=[output_text, questions_file, full_file]
    )

if __name__ == "__main__":
    logger.info("Starting application")
    interface.launch()