joshuarauh commited on
Commit
1871af1
·
verified ·
1 Parent(s): c113d70

added user choices of type and level

Browse files
Files changed (1) hide show
  1. app.py +102 -25
app.py CHANGED
@@ -80,14 +80,36 @@ def save_to_temp_file(content, filename):
80
  logger.error(f"Error saving temporary file: {str(e)}")
81
  raise
82
 
83
- def generate_test(subject):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
  """Generate a math test"""
85
  try:
 
 
 
 
 
 
 
86
  if not os.environ.get('ANTHROPIC_API_KEY'):
87
  logger.error("Anthropic API key not found")
88
  return "Error: Anthropic API key not configured", None, None
89
 
90
- logger.debug(f"Generating test for subject: {subject}")
91
 
92
  # Check rate limit
93
  now = datetime.now()
@@ -106,18 +128,35 @@ def generate_test(subject):
106
 
107
  selected_topics = random.sample(topics.get(subject, ["general"]), min(3, len(topics.get(subject, ["general"]))))
108
  logger.debug(f"Selected topics: {selected_topics}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
109
 
110
  system_prompt = f"""You will write math exam questions. Follow these requirements EXACTLY:
111
  1. Write exactly 3 university-level questions focusing on these specific topics: {', '.join(selected_topics)}
112
- 2. Include one of each of the following three types of question: computation, proof, application
113
- 3. For LaTeX math formatting:
 
 
114
  - Use $ for simple inline math
115
  - For equations and solution steps, use $$ on separate lines
116
  - For multi-step solutions, put each step on its own line in $$ $$
117
  - DO NOT use \\begin{{aligned}} or any other environments
118
- 4. Number each question as 1), 2), 3)
119
- 5. Include detailed solutions after each question
120
- 6. Keep formatting simple and clear"""
121
 
122
  logger.debug("Sending request to Anthropic API")
123
  message = anthropic.messages.create(
@@ -160,23 +199,55 @@ with gr.Blocks() as interface:
160
  Each test features different topics and difficulty levels. Limited to 25 requests per day.""")
161
 
162
  with gr.Row():
163
- subject_dropdown = gr.Dropdown(
164
- choices=[
165
- "Single Variable Calculus",
166
- "Multivariable Calculus",
167
- "Linear Algebra",
168
- "Differential Equations",
169
- "Real Analysis",
170
- "Complex Analysis",
171
- "Abstract Algebra",
172
- "Probability Theory",
173
- "Numerical Analysis",
174
- "Topology"
175
- ],
176
- label="Select Mathematics Subject",
177
- info="Choose a subject for the exam questions"
178
- )
179
- generate_btn = gr.Button("Generate Test")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
180
 
181
  output_text = gr.Markdown(
182
  label="Generated Test Preview",
@@ -192,7 +263,13 @@ with gr.Blocks() as interface:
192
 
193
  generate_btn.click(
194
  generate_test,
195
- inputs=[subject_dropdown],
 
 
 
 
 
 
196
  outputs=[output_text, questions_file, full_file]
197
  )
198
 
 
80
  logger.error(f"Error saving temporary file: {str(e)}")
81
  raise
82
 
83
+ def validate_question_counts(computation, proof, application):
84
+ """Validate that question counts sum to 3"""
85
+ try:
86
+ comp = int(computation)
87
+ prf = int(proof)
88
+ app = int(application)
89
+
90
+ if comp + prf + app != 3:
91
+ return False, "Total number of questions must equal 3"
92
+ if any(x < 0 for x in [comp, prf, app]):
93
+ return False, "Question counts cannot be negative"
94
+ return True, ""
95
+ except ValueError:
96
+ return False, "Please enter valid numbers"
97
+
98
+ def generate_test(subject, difficulty, computation_count, proof_count, application_count):
99
  """Generate a math test"""
100
  try:
101
+ # Validate inputs
102
+ is_valid, error_message = validate_question_counts(
103
+ computation_count, proof_count, application_count
104
+ )
105
+ if not is_valid:
106
+ return error_message, None, None
107
+
108
  if not os.environ.get('ANTHROPIC_API_KEY'):
109
  logger.error("Anthropic API key not found")
110
  return "Error: Anthropic API key not configured", None, None
111
 
112
+ logger.debug(f"Generating test for subject: {subject} at difficulty level: {difficulty}")
113
 
114
  # Check rate limit
115
  now = datetime.now()
 
128
 
129
  selected_topics = random.sample(topics.get(subject, ["general"]), min(3, len(topics.get(subject, ["general"]))))
130
  logger.debug(f"Selected topics: {selected_topics}")
131
+
132
+ # Create question type list based on user input
133
+ question_types = (
134
+ ["computation"] * int(computation_count) +
135
+ ["proof"] * int(proof_count) +
136
+ ["application"] * int(application_count)
137
+ )
138
+
139
+ difficulty_descriptions = {
140
+ 1: "introductory undergraduate level",
141
+ 2: "early undergraduate level",
142
+ 3: "advanced undergraduate level",
143
+ 4: "graduate level",
144
+ 5: "advanced graduate level"
145
+ }
146
 
147
  system_prompt = f"""You will write math exam questions. Follow these requirements EXACTLY:
148
  1. Write exactly 3 university-level questions focusing on these specific topics: {', '.join(selected_topics)}
149
+ 2. Include the following question types in this exact order:
150
+ {', '.join(question_types)}
151
+ 3. Make all questions {difficulty_descriptions[difficulty]} difficulty
152
+ 4. For LaTeX math formatting:
153
  - Use $ for simple inline math
154
  - For equations and solution steps, use $$ on separate lines
155
  - For multi-step solutions, put each step on its own line in $$ $$
156
  - DO NOT use \\begin{{aligned}} or any other environments
157
+ 5. Number each question as 1), 2), 3)
158
+ 6. Include detailed solutions after each question
159
+ 7. Keep formatting simple and clear"""
160
 
161
  logger.debug("Sending request to Anthropic API")
162
  message = anthropic.messages.create(
 
199
  Each test features different topics and difficulty levels. Limited to 25 requests per day.""")
200
 
201
  with gr.Row():
202
+ with gr.Column():
203
+ subject_dropdown = gr.Dropdown(
204
+ choices=[
205
+ "Single Variable Calculus",
206
+ "Multivariable Calculus",
207
+ "Linear Algebra",
208
+ "Differential Equations",
209
+ "Real Analysis",
210
+ "Complex Analysis",
211
+ "Abstract Algebra",
212
+ "Probability Theory",
213
+ "Numerical Analysis",
214
+ "Topology"
215
+ ],
216
+ label="Select Mathematics Subject",
217
+ info="Choose a subject for the exam questions"
218
+ )
219
+
220
+ difficulty_slider = gr.Slider(
221
+ minimum=1,
222
+ maximum=5,
223
+ step=1,
224
+ value=3,
225
+ label="Difficulty Level",
226
+ info="1: Introductory Undergraduate, 3: Advanced Undergraduate, 5: Advanced Graduate"
227
+ )
228
+
229
+ with gr.Row():
230
+ with gr.Column():
231
+ computation_count = gr.Number(
232
+ value=1,
233
+ label="Number of Computation Questions",
234
+ info="Enter the number of computation questions (total must be 3)",
235
+ precision=0
236
+ )
237
+ proof_count = gr.Number(
238
+ value=1,
239
+ label="Number of Proof Questions",
240
+ info="Enter the number of proof questions (total must be 3)",
241
+ precision=0
242
+ )
243
+ application_count = gr.Number(
244
+ value=1,
245
+ label="Number of Application Questions",
246
+ info="Enter the number of application questions (total must be 3)",
247
+ precision=0
248
+ )
249
+
250
+ generate_btn = gr.Button("Generate Test")
251
 
252
  output_text = gr.Markdown(
253
  label="Generated Test Preview",
 
263
 
264
  generate_btn.click(
265
  generate_test,
266
+ inputs=[
267
+ subject_dropdown,
268
+ difficulty_slider,
269
+ computation_count,
270
+ proof_count,
271
+ application_count
272
+ ],
273
  outputs=[output_text, questions_file, full_file]
274
  )
275