joshuarauh commited on
Commit
406c0d6
·
verified ·
1 Parent(s): 3334031

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -15
app.py CHANGED
@@ -38,18 +38,57 @@ def load_proof_repository():
38
  logger.error(f"Error loading proof repository: {str(e)}")
39
  return None
40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
  def get_relevant_proofs(topic):
42
  """Get relevant proofs from repository based on topic, randomly selecting examples"""
43
  repository = load_proof_repository()
44
  if not repository:
 
45
  return []
46
 
47
- # Extract relevant proofs based on topic
 
 
 
48
  relevant_proofs = []
49
  for theorem in repository.get("dataset", {}).get("theorems", []):
50
- # Add proofs that match the topic
51
- if any(topic.lower() in content.lower() for content in theorem.get("contents", [])):
52
- # Only include if it has both contents and proofs
 
 
 
 
 
 
 
 
 
 
53
  if theorem.get("contents") and theorem.get("proofs"):
54
  proof_content = {
55
  "title": theorem.get("title", ""),
@@ -57,32 +96,46 @@ def get_relevant_proofs(topic):
57
  "proofs": [p.get("contents", []) for p in theorem.get("proofs", [])]
58
  }
59
  relevant_proofs.append(proof_content)
 
 
 
 
60
 
61
  # Randomly select 3 proofs if we have more than 3
62
  if len(relevant_proofs) > 3:
63
- return random.sample(relevant_proofs, 3)
 
 
 
 
64
  return relevant_proofs
65
-
66
  def enhance_prompt_with_proofs(system_prompt, subject, topic):
67
  """Enhance the system prompt with relevant proofs if subject is Real Analysis"""
68
  if subject != "Real Analysis":
 
69
  return system_prompt
70
 
71
  relevant_proofs = get_relevant_proofs(topic)
72
  if not relevant_proofs:
 
73
  return system_prompt
74
-
 
 
75
  # Add proof examples to the prompt
76
  proof_examples = "\n\nReference these proof examples for style and approach:\n"
77
- for proof in relevant_proofs[:3]: # Limit to 3 examples to manage token usage
 
78
  proof_examples += f"\nTheorem: {proof['title']}\n"
79
  proof_examples += "Statement: " + " ".join(proof['contents']) + "\n"
80
  if proof['proofs']:
81
- proof_examples += "Proof: " + " ".join(proof['proofs'][0]) + "\n"
82
-
 
 
83
  # Add specific instructions for using the examples
84
- enhanced_prompt = f"""
85
- {system_prompt}
86
 
87
  ADDITIONAL PROOF GUIDELINES:
88
  1. Consider the following proof examples from established textbooks
@@ -90,9 +143,10 @@ ADDITIONAL PROOF GUIDELINES:
90
  3. Use similar proof techniques where applicable
91
  4. Follow similar notation and presentation style
92
 
93
- {proof_examples}
94
- """
95
- return enhanced_prompt
 
96
 
97
  def get_difficulty_parameters(difficulty_level):
98
  """Return specific parameters and constraints based on difficulty level"""
 
38
  logger.error(f"Error loading proof repository: {str(e)}")
39
  return None
40
 
41
+ TOPIC_MAPPINGS = {
42
+ "integration": ["integral", "integrable", "riemann", "integrate", "antiderivative"],
43
+ "continuity": ["continuous", "discontinuous", "discontinuity", "uniformly continuous"],
44
+ "sequences": ["sequence", "convergent", "divergent", "monotone", "subsequence"],
45
+ "series": ["series", "sum", "convergent series", "power series"],
46
+ "differentiation": ["derivative", "differentiable", "differential"],
47
+ "limits": ["limit", "cluster point", "accumulation"],
48
+ "functions": ["function", "mapping", "surjective", "injective", "bijective"],
49
+ "bounded": ["bound", "bounded above", "bounded below", "supremum", "infimum"]
50
+ }
51
+
52
+ def get_related_terms(topic):
53
+ """Get all related terms for a given topic"""
54
+ # Get direct mappings
55
+ related = TOPIC_MAPPINGS.get(topic.lower(), [])
56
+ # Add the original topic
57
+ related.append(topic.lower())
58
+ # Remove duplicates while preserving order
59
+ return list(dict.fromkeys(related))
60
+
61
+ def matches_topic(text, topic_terms):
62
+ """Check if any topic terms appear in the text"""
63
+ text_lower = text.lower()
64
+ return any(term in text_lower for term in topic_terms)
65
+
66
  def get_relevant_proofs(topic):
67
  """Get relevant proofs from repository based on topic, randomly selecting examples"""
68
  repository = load_proof_repository()
69
  if not repository:
70
+ logger.error("Failed to load proof repository")
71
  return []
72
 
73
+ logger.debug(f"Searching for proofs related to topic: {topic}")
74
+ topic_terms = get_related_terms(topic)
75
+ logger.debug(f"Related terms: {topic_terms}")
76
+
77
  relevant_proofs = []
78
  for theorem in repository.get("dataset", {}).get("theorems", []):
79
+ # Check categories
80
+ categories = theorem.get("categories", [])
81
+ category_match = any(matches_topic(cat, topic_terms) for cat in categories)
82
+
83
+ # Check contents
84
+ contents = theorem.get("contents", [])
85
+ content_match = any(matches_topic(content, topic_terms) for content in contents)
86
+
87
+ # Check title
88
+ title = theorem.get("title", "")
89
+ title_match = matches_topic(title, topic_terms)
90
+
91
+ if (category_match or content_match or title_match):
92
  if theorem.get("contents") and theorem.get("proofs"):
93
  proof_content = {
94
  "title": theorem.get("title", ""),
 
96
  "proofs": [p.get("contents", []) for p in theorem.get("proofs", [])]
97
  }
98
  relevant_proofs.append(proof_content)
99
+ logger.debug(f"Found matching proof: {proof_content['title']}")
100
+ logger.debug(f"Matched via: {'categories' if category_match else 'contents' if content_match else 'title'}")
101
+
102
+ logger.debug(f"Found {len(relevant_proofs)} relevant proofs before sampling")
103
 
104
  # Randomly select 3 proofs if we have more than 3
105
  if len(relevant_proofs) > 3:
106
+ selected = random.sample(relevant_proofs, 3)
107
+ logger.debug("Selected proofs for enhancement:")
108
+ for proof in selected:
109
+ logger.debug(f"- {proof['title']}")
110
+ return selected
111
  return relevant_proofs
112
+
113
  def enhance_prompt_with_proofs(system_prompt, subject, topic):
114
  """Enhance the system prompt with relevant proofs if subject is Real Analysis"""
115
  if subject != "Real Analysis":
116
+ logger.debug("Skipping proof enhancement - not Real Analysis")
117
  return system_prompt
118
 
119
  relevant_proofs = get_relevant_proofs(topic)
120
  if not relevant_proofs:
121
+ logger.debug(f"No relevant proofs found for topic: {topic}")
122
  return system_prompt
123
+
124
+ logger.debug(f"Enhancing prompt with {len(relevant_proofs)} proofs")
125
+
126
  # Add proof examples to the prompt
127
  proof_examples = "\n\nReference these proof examples for style and approach:\n"
128
+ for proof in relevant_proofs:
129
+ logger.debug(f"Adding proof: {proof['title']}")
130
  proof_examples += f"\nTheorem: {proof['title']}\n"
131
  proof_examples += "Statement: " + " ".join(proof['contents']) + "\n"
132
  if proof['proofs']:
133
+ first_proof = " ".join(proof['proofs'][0])
134
+ logger.debug(f"Proof length: {len(first_proof)} characters")
135
+ proof_examples += "Proof: " + first_proof + "\n"
136
+
137
  # Add specific instructions for using the examples
138
+ enhanced_prompt = f"""{system_prompt}
 
139
 
140
  ADDITIONAL PROOF GUIDELINES:
141
  1. Consider the following proof examples from established textbooks
 
143
  3. Use similar proof techniques where applicable
144
  4. Follow similar notation and presentation style
145
 
146
+ {proof_examples}"""
147
+
148
+ return enhanced_prompt
149
+
150
 
151
  def get_difficulty_parameters(difficulty_level):
152
  """Return specific parameters and constraints based on difficulty level"""