Kamocodes commited on
Commit
4e7551b
·
verified ·
1 Parent(s): 0faa11e

Upload 3 files

Browse files
Files changed (3) hide show
  1. README.md +44 -12
  2. app.py +64 -310
  3. requirements.txt +3 -0
README.md CHANGED
@@ -1,12 +1,44 @@
1
- ---
2
- title: Math
3
- emoji: 🌍
4
- colorFrom: indigo
5
- colorTo: green
6
- sdk: gradio
7
- sdk_version: 5.29.0
8
- app_file: app.py
9
- pinned: false
10
- ---
11
-
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Mathematics Question Generator
3
+ emoji: 🧮
4
+ colorFrom: blue
5
+ colorTo: green
6
+ sdk: gradio
7
+ sdk_version: 4.16.0
8
+ app_file: app.py
9
+ pinned: false
10
+ ---
11
+
12
+ # Mathematics Question Generator
13
+
14
+ This application generates customized mathematics questions on any topic you specify. Simply enter a mathematics chapter or concept you want to practice, and the AI will create questions with step-by-step solutions.
15
+
16
+ ## Features
17
+
18
+ - Generate mathematics questions on any topic
19
+ - Choose the number of questions (1-10)
20
+ - Select difficulty level (elementary to advanced)
21
+ - View detailed step-by-step solutions
22
+
23
+ ## How to Use
24
+
25
+ 1. Enter a mathematics topic (e.g., "Linear Algebra: Eigenvalues", "Calculus: Integration")
26
+ 2. Adjust the number of questions and difficulty level as needed
27
+ 3. Click "Generate Questions"
28
+ 4. Study the generated questions and their solutions
29
+
30
+ ## Setup Requirements
31
+
32
+ This application requires a Hugging Face API token with access to the DeepSeek models. To set up your token:
33
+
34
+ 1. Generate a Hugging Face API token with read access from [https://huggingface.co/settings/tokens](https://huggingface.co/settings/tokens)
35
+ 2. Add this token to your Space as a secret with the key `HUGGINGFACE_TOKEN`
36
+ - Go to your Space settings
37
+ - Click on "Secrets"
38
+ - Add a new secret with name `HUGGINGFACE_TOKEN` and your token as the value
39
+
40
+ ## About
41
+
42
+ This application uses DeepSeek's mathematics-focused language model to generate high-quality mathematics questions tailored to your learning needs.
43
+
44
+ Created by Kamagelo Mosia
app.py CHANGED
@@ -1,345 +1,99 @@
1
  import gradio as gr
2
  import os
 
3
  from huggingface_hub import InferenceClient
4
 
5
- # Define calculus topics and subtopics
6
- CALCULUS_TOPICS = {
7
- "Limits": [
8
- "Finding limits algebraically",
9
- "One-sided limits",
10
- "Limits at infinity",
11
- "Limits that don't exist",
12
- "Epsilon-delta proofs"
13
- ],
14
- "Derivatives": [
15
- "Basic differentiation rules",
16
- "Power rule",
17
- "Product rule",
18
- "Quotient rule",
19
- "Chain rule",
20
- "Implicit differentiation",
21
- "Logarithmic differentiation",
22
- "Higher-order derivatives"
23
- ],
24
- "Applications of Derivatives": [
25
- "Related rates",
26
- "Mean value theorem",
27
- "Critical points",
28
- "Curve sketching",
29
- "Optimization problems",
30
- "Linear approximation",
31
- "Newton's method"
32
- ],
33
- "Integrals": [
34
- "Indefinite integrals",
35
- "Basic integration rules",
36
- "Substitution method",
37
- "Integration by parts",
38
- "Partial fractions",
39
- "Trigonometric integrals",
40
- "Improper integrals"
41
- ],
42
- "Applications of Integrals": [
43
- "Area between curves",
44
- "Volume of solids of revolution",
45
- "Arc length",
46
- "Surface area",
47
- "Work and energy problems",
48
- "Center of mass"
49
- ],
50
- "Sequences and Series": [
51
- "Convergence tests",
52
- "Power series",
53
- "Taylor and Maclaurin series",
54
- "Radius of convergence"
55
- ],
56
- "Multivariable Calculus": [
57
- "Partial derivatives",
58
- "Directional derivatives",
59
- "Gradient",
60
- "Multiple integrals",
61
- "Line integrals",
62
- "Surface integrals"
63
- ],
64
- "Differential Equations": [
65
- "First-order differential equations",
66
- "Separable equations",
67
- "Second-order differential equations",
68
- "Systems of differential equations"
69
- ]
70
- }
71
-
72
- # Initialize DeepSeek client
73
- def get_deepseek_client():
74
- # You'll need to set up your HuggingFace token as an environment variable
75
- client = InferenceClient(
76
- model="deepseek-ai/deepseek-math-7b-instruct",
77
- token=os.getenv("HUGGINGFACE_TOKEN")
78
- )
79
  return client
80
 
81
- def generate_math_problems(topic, subtopic, difficulty, num_problems=3):
82
- """Generate math problems with step-by-step solutions and explanations"""
83
- client = get_deepseek_client()
84
 
85
- # Create a specific prompt with the selected topic, subtopic and difficulty
86
- prompt = f"""
87
- Create {num_problems} {difficulty} difficulty calculus problems about '{subtopic}' (under the general topic of '{topic}').
88
 
89
- For each problem:
90
- 1. Provide a clear and concise question.
91
- 2. Provide a detailed step-by-step solution that a student can follow.
92
- 3. Include explanations for each step.
93
- 4. Mark the final answer clearly.
94
 
95
- Format:
96
- ## Problem N:
97
- [Problem description]
 
98
 
99
- ### Solution:
100
- [Step-by-step solution]
 
 
101
 
102
- ### Explanation:
103
- [Explanation for the solution steps]
104
 
105
- ### Answer:
106
  [Final answer]
 
 
 
107
  """
108
 
109
  try:
110
  response = client.text_generation(
111
  prompt=prompt,
 
112
  max_new_tokens=2048,
113
  temperature=0.7,
114
- repetition_penalty=1.2,
115
- do_sample=True
116
  )
117
  return response
118
  except Exception as e:
119
- return f"Error generating problems: {str(e)}"
120
-
121
- # Function to update subtopics based on main topic selection
122
- def update_subtopics(topic):
123
- return gr.Dropdown(choices=CALCULUS_TOPICS.get(topic, []), value=CALCULUS_TOPICS.get(topic, [""])[0])
124
-
125
- # Function to handle Gradio input and output
126
- def create_problems(topic, subtopic, difficulty, num_problems):
127
- if not topic or not subtopic:
128
- return "Please select both a topic and subtopic to generate problems."
129
-
130
- try:
131
- num_problems = int(num_problems)
132
- except ValueError:
133
- num_problems = 3 # Default to 3 problems if input is invalid
134
-
135
- return generate_math_problems(topic, subtopic, difficulty, num_problems)
136
-
137
- # Custom CSS with Google Fonts integration
138
- custom_css = """
139
- @import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500&family=JetBrains+Mono&display=swap');
140
-
141
- :root {
142
- --border-color: #e0e0e0;
143
- --bg-color: #ffffff;
144
- --text-color: #333333;
145
- --hover-color: #f0f0f0;
146
- }
147
-
148
- body, .gradio-container {
149
- font-family: 'Inter', sans-serif;
150
- color: var(--text-color);
151
- }
152
-
153
- .header {
154
- text-align: center;
155
- padding: 1.5rem 0;
156
- margin-bottom: 1rem;
157
- border-bottom: 1px solid var(--border-color);
158
- }
159
-
160
- .header h1 {
161
- font-weight: 500;
162
- margin-bottom: 0.5rem;
163
- }
164
-
165
- .header p {
166
- font-weight: 300;
167
- opacity: 0.8;
168
- }
169
-
170
- .card {
171
- border: 1px solid var(--border-color);
172
- border-radius: 4px;
173
- padding: 1.25rem;
174
- margin-bottom: 1rem;
175
- background-color: var(--bg-color);
176
- }
177
-
178
- .card h3 {
179
- font-weight: 500;
180
- margin-bottom: 1rem;
181
- font-size: 1.1rem;
182
- }
183
-
184
- button.generate-btn {
185
- width: 100%;
186
- background-color: var(--bg-color);
187
- border: 1px solid var(--border-color);
188
- border-radius: 4px;
189
- padding: 0.5rem;
190
- cursor: pointer;
191
- transition: background-color 0.2s ease;
192
- font-family: 'Inter', sans-serif;
193
- font-weight: 500;
194
- }
195
-
196
- button.generate-btn:hover {
197
- background-color: var(--hover-color);
198
- }
199
-
200
- .output-box {
201
- font-family: 'JetBrains Mono', monospace;
202
- border: 1px solid var(--border-color);
203
- border-radius: 4px;
204
- padding: 1rem;
205
- white-space: pre-wrap;
206
- line-height: 1.5;
207
- }
208
 
209
- .output-box h2 {
210
- font-weight: 500;
211
- margin-top: 1.5rem;
212
- padding-top: 1rem;
213
- border-top: 1px solid var(--border-color);
214
- }
215
-
216
- .output-box h2:first-child {
217
- margin-top: 0;
218
- padding-top: 0;
219
- border-top: none;
220
- }
221
-
222
- .output-box h3 {
223
- font-weight: 500;
224
- margin-top: 1rem;
225
- }
226
-
227
- /* Customizing Gradio elements */
228
- .gr-form, .gr-panel {
229
- border: none !important;
230
- box-shadow: none !important;
231
- }
232
-
233
- .gr-input, .gr-dropdown, .gr-radio {
234
- border: 1px solid var(--border-color) !important;
235
- border-radius: 4px !important;
236
- }
237
-
238
- .gr-input:focus, .gr-dropdown:focus {
239
- border-color: #999 !important;
240
- box-shadow: none !important;
241
- }
242
-
243
- .gr-slider-thumb {
244
- background-color: #999 !important;
245
- }
246
-
247
- .footer {
248
- text-align: center;
249
- font-size: 0.8rem;
250
- padding: 1rem 0;
251
- border-top: 1px solid var(--border-color);
252
- margin-top: 2rem;
253
- opacity: 0.7;
254
- }
255
- """
256
-
257
- # Define the Gradio interface with custom styling
258
- with gr.Blocks(css=custom_css) as demo:
259
- # Header
260
- gr.HTML(
261
- """
262
- <div class="header">
263
- <h1>Calculus Problem Generator</h1>
264
- <p>Generate problems with step-by-step solutions</p>
265
- </div>
266
- """
267
- )
268
 
269
- # Main content area
270
  with gr.Row():
271
- # Left panel - Problem configuration
272
- with gr.Column(scale=1):
273
- with gr.Box(elem_classes="card"):
274
- gr.Markdown("### Configure Problems")
275
-
276
- # Topic selection
277
- topic_dropdown = gr.Dropdown(
278
- choices=list(CALCULUS_TOPICS.keys()),
279
- label="Topic",
280
- value=list(CALCULUS_TOPICS.keys())[0]
281
- )
282
-
283
- # Subtopic selection (dynamically updated)
284
- subtopic_dropdown = gr.Dropdown(
285
- choices=CALCULUS_TOPICS[list(CALCULUS_TOPICS.keys())[0]],
286
- label="Subtopic",
287
- value=CALCULUS_TOPICS[list(CALCULUS_TOPICS.keys())[0]][0]
288
  )
289
 
290
- # Update subtopics when topic changes
291
- topic_dropdown.change(
292
- fn=update_subtopics,
293
- inputs=topic_dropdown,
294
- outputs=subtopic_dropdown
295
- )
296
-
297
- # Difficulty selection
298
- difficulty_radio = gr.Radio(
299
- choices=["Basic", "Intermediate", "Advanced"],
300
- label="Difficulty",
301
- value="Intermediate"
302
- )
303
-
304
- # Number of problems
305
- num_problems_slider = gr.Slider(
306
- minimum=1,
307
- maximum=5,
308
- value=3,
309
- step=1,
310
- label="Number of Problems"
311
- )
312
-
313
- # Generate button
314
- generate_button = gr.Button("Generate Problems", elem_classes="generate-btn")
315
-
316
- # Right panel - Output display
317
- with gr.Column(scale=2):
318
- with gr.Box(elem_classes="card"):
319
- gr.Markdown("### Problems & Solutions")
320
-
321
- # Output display
322
- output_md = gr.Markdown(
323
- elem_classes="output-box",
324
- value="Select options and click 'Generate Problems' to start."
325
  )
 
 
326
 
327
- # Footer
328
- gr.HTML(
329
- """
330
- <div class="footer">
331
- <p>Calculus Problem Generator | Educational Tool</p>
332
- </div>
333
- """
334
- )
335
 
336
- # Connect the generate button to the function
337
  generate_button.click(
338
- fn=create_problems,
339
- inputs=[topic_dropdown, subtopic_dropdown, difficulty_radio, num_problems_slider],
340
- outputs=output_md
341
  )
 
 
 
342
 
343
  # Launch the app
344
- if __name__ == "__main__":
345
- demo.launch()
 
1
  import gradio as gr
2
  import os
3
+ import requests
4
  from huggingface_hub import InferenceClient
5
 
6
+ # Initialize the Hugging Face Inference client for DeepSeek
7
+ def get_inference_client():
8
+ api_token = os.environ.get("HUGGINGFACE_TOKEN")
9
+ if not api_token:
10
+ return None
11
+
12
+ client = InferenceClient(token=api_token)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  return client
14
 
15
+ def generate_math_questions(chapter, num_questions=5, difficulty="medium"):
16
+ """Generate mathematics questions based on the provided chapter/topic"""
17
+ client = get_inference_client()
18
 
19
+ if not client:
20
+ return "Error: Hugging Face token not configured. Please set the HUGGINGFACE_TOKEN in your space settings."
 
21
 
22
+ prompt = f"""Generate {num_questions} {difficulty}-level mathematics questions about {chapter}.
 
 
 
 
23
 
24
+ For each question:
25
+ 1. Write a clear, well-defined question
26
+ 2. Provide a step-by-step solution
27
+ 3. Include the final answer
28
 
29
+ Format your response as:
30
+
31
+ ## Question 1
32
+ [Question text]
33
 
34
+ ### Solution
35
+ [Step-by-step solution]
36
 
37
+ ### Answer
38
  [Final answer]
39
+
40
+ ## Question 2
41
+ ...and so on
42
  """
43
 
44
  try:
45
  response = client.text_generation(
46
  prompt=prompt,
47
+ model="deepseek-ai/deepseek-math-7b-instruct",
48
  max_new_tokens=2048,
49
  temperature=0.7,
50
+ top_p=0.95,
 
51
  )
52
  return response
53
  except Exception as e:
54
+ return f"Error generating questions: {str(e)}\n\nPlease ensure your Hugging Face token has the necessary permissions."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
 
56
+ # Create the Gradio interface
57
+ with gr.Blocks(title="Mathematics Question Generator") as demo:
58
+ gr.Markdown("# 🧮 Mathematics Question Generator")
59
+ gr.Markdown("Enter a mathematics chapter or topic to generate practice questions with solutions.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
 
 
61
  with gr.Row():
62
+ with gr.Column(scale=3):
63
+ chapter_input = gr.Textbox(
64
+ label="Mathematics Topic/Chapter",
65
+ placeholder="e.g., Calculus: Integration by Parts",
66
+ info="Be specific for better results"
67
+ )
68
+
69
+ with gr.Row():
70
+ num_questions = gr.Slider(
71
+ minimum=1,
72
+ maximum=10,
73
+ value=5,
74
+ step=1,
75
+ label="Number of Questions"
 
 
 
76
  )
77
 
78
+ difficulty = gr.Dropdown(
79
+ choices=["elementary", "easy", "medium", "hard", "advanced"],
80
+ value="medium",
81
+ label="Difficulty Level"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
  )
83
+
84
+ generate_button = gr.Button("Generate Questions", variant="primary")
85
 
86
+ with gr.Row():
87
+ output = gr.Markdown(label="Generated Questions")
 
 
 
 
 
 
88
 
 
89
  generate_button.click(
90
+ fn=generate_math_questions,
91
+ inputs=[chapter_input, num_questions, difficulty],
92
+ outputs=output
93
  )
94
+
95
+ gr.Markdown("---")
96
+ gr.Markdown("Created by Kamagelo Mosia | Powered by DeepSeek and Hugging Face")
97
 
98
  # Launch the app
99
+ demo.launch()
 
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ gradio>=4.0.0
2
+ huggingface_hub>=0.19.0
3
+ requests>=2.31.0