Khoi1234210 commited on
Commit
415b778
·
verified ·
1 Parent(s): 4eac01b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +191 -57
app.py CHANGED
@@ -6,6 +6,7 @@ import random
6
  import re
7
 
8
  # Load math datasets for sample problems
 
9
  fw = load_dataset("HuggingFaceFW/fineweb-edu", name="sample-10BT", split="train", streaming=True)
10
  ds = load_dataset("HuggingFaceH4/ultrachat_200k", streaming=True)
11
 
@@ -18,26 +19,28 @@ def load_sample_problems():
18
  samples.append(item["question"])
19
  if i >= 50:
20
  break
 
21
  return samples
22
- except:
 
23
  return [
24
  "What is the derivative of f(x) = 3x² + 2x - 1?",
25
  "A triangle has sides of length 5, 12, and 13. What is its area?",
26
  "If log₂(x) + log₂(x+6) = 4, find the value of x.",
27
- "Find the limit: lim(x0) (sin(x)/x)",
28
  "Solve the system: x + 2y = 7, 3x - y = 4"
29
  ]
30
 
31
  math_samples = load_sample_problems()
32
 
33
  def create_math_system_message():
34
- """Create specialized system prompt for mathematics"""
35
- return """You are Mathetics AI, an advanced mathematics tutor and problem solver.
36
 
37
  🧮 **Your Expertise:**
38
  - Step-by-step problem solving with clear explanations
39
  - Multiple solution approaches when applicable
40
- - Proper mathematical notation and terminology
41
  - Verification of answers through different methods
42
 
43
  📐 **Problem Domains:**
@@ -50,34 +53,74 @@ def create_math_system_message():
50
  💡 **Teaching Style:**
51
  1. **Understand the Problem** - Identify what's being asked
52
  2. **Plan the Solution** - Choose the appropriate method
53
- 3. **Execute Step-by-Step** - Show all work clearly
54
  4. **Verify the Answer** - Check if the result makes sense
55
  5. **Alternative Methods** - Mention other possible approaches
56
 
 
 
 
 
 
 
 
 
 
57
  Always be precise, educational, and encourage mathematical thinking."""
58
 
59
  def render_latex(text):
60
- """Format math expressions for LaTeX rendering"""
61
- if not text: # Safety check
62
  return text
63
- # Wrap inline math: x^2 → $x^2$
64
- text = re.sub(r'\b([a-zA-Z]+)\^?(\d*)\b', r'$\1^{\2}$', text)
65
- # Wrap functions: sin(x) $\sin(x)$
66
- text = re.sub(r'([a-zA-Z]+)\(([^)]+)\)', r'$\1(\2)$', text)
67
- # Wrap limits: lim(x→0) $\lim_{x \to 0}$
68
- text = re.sub(r'lim\(([a-z])→([0-9.]+)\)', r'$\\lim_{ \1 \\to \2}$', text)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
  return text
70
 
71
  def respond(message, history, system_message, max_tokens, temperature, top_p):
 
72
  # Show initial thinking
73
- yield "🤔 Thinking for the suitable answer..."
74
 
75
  # Use Qwen Math model
76
  client = InferenceClient(model="Qwen/Qwen2.5-Math-7B-Instruct")
77
 
78
- # Build messages
79
- messages = [{"role": "system", "content": system_message}]
80
- messages.extend(history)
 
 
 
 
 
 
 
 
 
81
  messages.append({"role": "user", "content": message})
82
 
83
  response = ""
@@ -90,24 +133,26 @@ def respond(message, history, system_message, max_tokens, temperature, top_p):
90
  stream=True,
91
  temperature=temperature,
92
  top_p=top_p,
 
93
  ):
94
  choices = message_chunk.choices
95
  if len(choices) and choices[0].delta.content:
96
  token = choices[0].delta.content
97
  response += token
98
- # Stream raw during generation, format at end
99
- if "```" in response or len(response) > 500: # Format after substantial response
 
100
  formatted = render_latex(response)
101
  yield formatted
102
  else:
103
  yield response
104
 
105
  # Final formatted response
106
- final_formatted = render_latex(response)
107
  yield final_formatted
108
 
109
  except Exception as e:
110
- error_msg = f"❌ **Error**: {str(e)}\n\n💡 **Troubleshooting**:\n- Make sure you're logged in with Hugging Face\n- Check if the model is accessible\n- Try a simpler problem first"
111
  yield error_msg
112
 
113
  def get_random_sample():
@@ -119,19 +164,25 @@ def get_random_sample():
119
  def insert_sample_to_chat(difficulty):
120
  """Insert random sample into chat input"""
121
  sample = get_random_sample()
122
- return "", [None, sample] # Clear input, add sample to chat
123
 
124
  def show_help():
125
- return """**Math Help Tips:**
126
-
127
- 1. **Be Specific**: "Find the derivative of..." instead of "Help with calculus"
128
- 2. **Show Your Work**: "I got x=5, is this correct?"
129
- 3. **Ask for Steps**: "Show me step-by-step how to solve..."
130
- 4. **Request Verification**: "Check my solution to this problem"
131
- 5. **Alternative Methods**: "What's another way to solve this?"
132
- """
 
 
 
 
 
 
133
 
134
- # Create ChatInterface
135
  chatbot = gr.ChatInterface(
136
  respond,
137
  type="messages",
@@ -140,42 +191,108 @@ chatbot = gr.ChatInterface(
140
  **Powered by Qwen 2.5-Math** | **Specialized for Mathematical Problem Solving**
141
 
142
  ✨ **Capabilities**: Algebra • Geometry • Calculus • Statistics • Competition Math
143
- 📚 **Features**: Step-by-step solutions • Multiple approaches • Clear explanations
144
  """,
145
  additional_inputs=[
146
  gr.Textbox(
147
  value=create_math_system_message(),
148
  label="🧠 System Message (Math Tutor Personality)",
149
- lines=3,
150
- max_lines=10
151
  ),
152
- gr.Slider(minimum=256, maximum=2048, value=768, step=64, label="📝 Max Tokens"),
153
- gr.Slider(minimum=0.1, maximum=1.0, value=0.3, step=0.1, label="🎯 Temperature"),
154
- gr.Slider(minimum=0.1, maximum=1.0, value=0.85, step=0.05, label="🔍 Top-p"),
155
  ],
156
  examples=[
157
- ["What is the derivative of f(x) = 3x² + 2x - 1?"],
158
- ["A triangle has sides of length 5, 12, and 13. What is its area?"],
159
- ["Find the limit: lim(x0) (sin(x)/x)"],
160
- ["Solve the system: x + 2y = 7, 3x - y = 4"],
161
- ["What is the integral of ∫(2x³ - 5x + 3)dx?"]
162
  ],
163
  cache_examples=False,
164
- concurrency_limit=10,
 
 
 
165
  )
166
 
167
- # Main interface
168
  with gr.Blocks(
169
  title="🧮 Mathetics AI",
170
  theme=gr.themes.Soft(),
171
  css="""
172
- .math-highlight { background-color: #f0f8ff; padding: 10px; border-left: 4px solid #4CAF50; margin: 10px 0; border-radius: 5px; }
173
- .difficulty-selector { background-color: #fff3e0; padding: 15px; border-radius: 10px; margin: 10px 0; }
174
- .markdown math { font-size: 1.1em; }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
175
  """
176
  ) as demo:
177
 
178
- gr.Markdown("# 🧮 **Mathematics AI** - Advanced Mathematics Solver\n**Your Personal AI Math Tutor**")
 
 
 
 
 
 
179
 
180
  with gr.Row():
181
  with gr.Column(scale=4):
@@ -190,29 +307,46 @@ with gr.Blocks(
190
  elem_classes=["difficulty-selector"]
191
  )
192
 
193
- sample_btn = gr.Button("🎯 Get Sample Problem", variant="secondary")
194
- help_btn = gr.Button("❓ Math Help Tips", variant="secondary")
195
 
196
  gr.Markdown("### 🔧 **Quick Tools**")
197
- gr.Markdown("- **Algebra**: Equations, inequalities, factoring\n- **Geometry**: Area, volume, trigonometry\n- **Calculus**: Derivatives, integrals, limits\n- **Statistics**: Probability, distributions\n- **Number Theory**: Prime factorization, GCD/LCM")
 
 
 
 
 
 
198
 
 
199
  gr.Markdown("""
200
  ---
201
- **🔧 Technical Details:** Qwen/Qwen2.5-Math-7B-Instruct | Real-time streaming responses
 
 
202
 
203
- **💡 Usage Tips:** Be specific • Request step-by-step • Ask for verification
 
 
 
 
204
  """)
205
 
206
  # Event handlers
 
 
 
 
207
  sample_btn.click(
208
- insert_sample_to_chat,
209
  inputs=[difficulty_preset],
210
- outputs=[chatbot] # Actually feeds into chat now!
211
  )
212
 
213
  help_btn.click(
214
  show_help,
215
- outputs=gr.Markdown() # Could show in a separate output
216
  )
217
 
218
  if __name__ == "__main__":
 
6
  import re
7
 
8
  # Load math datasets for sample problems
9
+ print("🔄 Loading datasets...")
10
  fw = load_dataset("HuggingFaceFW/fineweb-edu", name="sample-10BT", split="train", streaming=True)
11
  ds = load_dataset("HuggingFaceH4/ultrachat_200k", streaming=True)
12
 
 
19
  samples.append(item["question"])
20
  if i >= 50:
21
  break
22
+ print(f"✅ Loaded {len(samples)} GSM8K samples")
23
  return samples
24
+ except Exception as e:
25
+ print(f"⚠️ Dataset error: {e}, using fallback")
26
  return [
27
  "What is the derivative of f(x) = 3x² + 2x - 1?",
28
  "A triangle has sides of length 5, 12, and 13. What is its area?",
29
  "If log₂(x) + log₂(x+6) = 4, find the value of x.",
30
+ "Find the limit: lim(x->0) (sin(x)/x)",
31
  "Solve the system: x + 2y = 7, 3x - y = 4"
32
  ]
33
 
34
  math_samples = load_sample_problems()
35
 
36
  def create_math_system_message():
37
+ """Create specialized system prompt for mathematics with LaTeX"""
38
+ return """You are Mathetics AI, an advanced mathematics tutor and problem solver.
39
 
40
  🧮 **Your Expertise:**
41
  - Step-by-step problem solving with clear explanations
42
  - Multiple solution approaches when applicable
43
+ - Proper mathematical notation and terminology using LaTeX
44
  - Verification of answers through different methods
45
 
46
  📐 **Problem Domains:**
 
53
  💡 **Teaching Style:**
54
  1. **Understand the Problem** - Identify what's being asked
55
  2. **Plan the Solution** - Choose the appropriate method
56
+ 3. **Execute Step-by-Step** - Show all work clearly with LaTeX formatting
57
  4. **Verify the Answer** - Check if the result makes sense
58
  5. **Alternative Methods** - Mention other possible approaches
59
 
60
+ **LaTeX Guidelines:**
61
+ - Use $...$ for inline math: $x^2 + y^2 = z^2$
62
+ - Use $$...$$ or \[...\] for display math
63
+ - Box final answers: \boxed{answer}
64
+ - Fractions: \frac{numerator}{denominator}
65
+ - Limits: \lim_{x \to 0}
66
+ - Derivatives: \frac{d}{dx} or f'(x)
67
+ - Integrals: \int f(x) \, dx
68
+
69
  Always be precise, educational, and encourage mathematical thinking."""
70
 
71
  def render_latex(text):
72
+ """Enhanced LaTeX rendering - fixes raw code output"""
73
+ if not text or len(text) < 5:
74
  return text
75
+
76
+ try:
77
+ # Fix common LaTeX patterns from Qwen
78
+ # Ensure proper inline math delimiters
79
+ text = re.sub(r'(?<!\\)\$([^\$]+)\$(?!\$)', r'$\1$', text)
80
+
81
+ # Convert display math
82
+ text = re.sub(r'\$\$([^\$]+)\$\$', r'$$\1$$', text)
83
+ text = re.sub(r'\\\[([^\\]+)\\\]', r'$$\1$$', text)
84
+ text = re.sub(r'\\\(([^\\]+)\\\)', r'$\1$', text)
85
+
86
+ # Fix escaped LaTeX commands
87
+ text = re.sub(r'\\(lim|frac|sqrt|int|sum|prod|partial|nabla|infty|to|le|ge|neq|approx|cdot|times|div|deg|prime|log|ln|log|sin|cos|tan|cot|sec|csc|arcsin|arccos|arctan|sinh|cosh)', r'\1', text)
88
+
89
+ # Ensure boxed answers render
90
+ text = re.sub(r'\\boxed\{([^}]+)\}', r'$$\boxed{\1}$$', text)
91
+
92
+ # Fix fractions
93
+ text = re.sub(r'\\frac\{([^}]+)\}\{([^}]+)\}', r'$\frac{\1}{\2}$', text)
94
+
95
+ # Clean up extra spaces
96
+ text = re.sub(r'\s*([\$\\])\s*', r'\1', text)
97
+
98
+ except Exception as e:
99
+ print(f"⚠️ LaTeX formatting error: {e}")
100
+ # Return original if processing fails
101
+
102
  return text
103
 
104
  def respond(message, history, system_message, max_tokens, temperature, top_p):
105
+ """Enhanced response with proper LaTeX streaming"""
106
  # Show initial thinking
107
+ yield "🤔 Thinking step-by-step..."
108
 
109
  # Use Qwen Math model
110
  client = InferenceClient(model="Qwen/Qwen2.5-Math-7B-Instruct")
111
 
112
+ # Build messages properly
113
+ messages = []
114
+ if system_message:
115
+ messages.append({"role": "system", "content": system_message})
116
+
117
+ # Add conversation history
118
+ for msg in history:
119
+ if msg.get("role") == "user":
120
+ messages.append({"role": "user", "content": msg["content"]})
121
+ elif msg.get("role") == "assistant":
122
+ messages.append({"role": "assistant", "content": msg["content"]})
123
+
124
  messages.append({"role": "user", "content": message})
125
 
126
  response = ""
 
133
  stream=True,
134
  temperature=temperature,
135
  top_p=top_p,
136
+ timeout=60 # Add timeout for stability
137
  ):
138
  choices = message_chunk.choices
139
  if len(choices) and choices[0].delta.content:
140
  token = choices[0].delta.content
141
  response += token
142
+
143
+ # Stream with formatting every few tokens
144
+ if len(response) % 50 == 0 or token.strip() in ['.', '!', '?', '\n']:
145
  formatted = render_latex(response)
146
  yield formatted
147
  else:
148
  yield response
149
 
150
  # Final formatted response
151
+ final_formatted = render_latex(response.strip())
152
  yield final_formatted
153
 
154
  except Exception as e:
155
+ error_msg = f"❌ **Error**: {str(e)[:100]}...\n\n💡 **Troubleshooting**:\n Try a simpler problem\n Check your connection\n Wait a moment and retry"
156
  yield error_msg
157
 
158
  def get_random_sample():
 
164
  def insert_sample_to_chat(difficulty):
165
  """Insert random sample into chat input"""
166
  sample = get_random_sample()
167
+ return sample, "" # Return sample for input, clear status
168
 
169
  def show_help():
170
+ return """**🧮 Math Help Tips:**
171
+
172
+ 1. **Be Specific**: "Find the derivative of x² + 3x" instead of "help with calculus"
173
+ 2. **Request Steps**: "Show me step-by-step how to solve..."
174
+ 3. **Ask for Verification**: "Check if my answer x=5 is correct"
175
+ 4. **Alternative Methods**: "What's another way to solve this integral?"
176
+ 5. **Use Clear Notation**: "lim(x->0)" instead of arrows for mobile
177
+
178
+ **LaTeX Examples:**
179
+ - Inline: $x^2 + y^2 = z^2$
180
+ - Display: $$\int x^2 \, dx = \frac{x^3}{3} + C$$
181
+ - Boxed: $$\boxed{x = 5}$$
182
+
183
+ **Pro Tip**: Crank tokens to 1500+ for competition problems!"""
184
 
185
+ # Enhanced ChatInterface with better LaTeX support
186
  chatbot = gr.ChatInterface(
187
  respond,
188
  type="messages",
 
191
  **Powered by Qwen 2.5-Math** | **Specialized for Mathematical Problem Solving**
192
 
193
  ✨ **Capabilities**: Algebra • Geometry • Calculus • Statistics • Competition Math
194
+ 📚 **Features**: Step-by-step solutions • Multiple approaches • Beautiful LaTeX rendering
195
  """,
196
  additional_inputs=[
197
  gr.Textbox(
198
  value=create_math_system_message(),
199
  label="🧠 System Message (Math Tutor Personality)",
200
+ lines=4,
201
+ max_lines=12
202
  ),
203
+ gr.Slider(minimum=256, maximum=2048, value=1024, step=128, label="📝 Max Tokens (Longer = More Detail)"),
204
+ gr.Slider(minimum=0.1, maximum=1.0, value=0.3, step=0.1, label="🎯 Temperature (Creativity vs Precision)"),
205
+ gr.Slider(minimum=0.1, maximum=1.0, value=0.85, step=0.05, label="🔍 Top-p (Response Diversity)"),
206
  ],
207
  examples=[
208
+ ["Find the derivative of f(x) = 3x² + 2x - 1"],
209
+ ["A triangle has sides 5, 12, and 13. What is its area?"],
210
+ ["Solve: lim(x->0) (sin(x)/x)"],
211
+ ["What is ∫(2x³ - 5x + 3) dx?"],
212
+ ["Solve the system: x + 2y = 7, 3x - y = 4"]
213
  ],
214
  cache_examples=False,
215
+ concurrency_limit=5, # Reduce for stability
216
+ retry_btn="🔄 Retry",
217
+ undo_btn="↶ Undo",
218
+ clear_btn="🗑️ Clear"
219
  )
220
 
221
+ # Main interface with enhanced LaTeX CSS
222
  with gr.Blocks(
223
  title="🧮 Mathetics AI",
224
  theme=gr.themes.Soft(),
225
  css="""
226
+ /* Enhanced math rendering */
227
+ .markdown-body {
228
+ font-family: 'Times New Roman', Georgia, serif;
229
+ line-height: 1.6;
230
+ }
231
+
232
+ /* Katex math rendering */
233
+ .katex {
234
+ font-size: 1.1em !important;
235
+ color: #2c3e50;
236
+ }
237
+ .katex-display {
238
+ font-size: 1.3em !important;
239
+ text-align: center;
240
+ margin: 1em 0;
241
+ padding: 10px;
242
+ background: #f8f9fa;
243
+ border-radius: 8px;
244
+ }
245
+
246
+ /* Boxed answers */
247
+ .katex .mord.text { font-weight: bold; }
248
+
249
+ /* Chat message styling */
250
+ .message {
251
+ margin: 10px 0;
252
+ padding: 12px;
253
+ border-radius: 8px;
254
+ }
255
+ .user {
256
+ background: linear-gradient(135deg, #e3f2fd 0%, #bbdefb 100%);
257
+ border-left: 4px solid #2196f3;
258
+ }
259
+ .assistant {
260
+ background: linear-gradient(135deg, #f5f5f5 0%, #eeeeee 100%);
261
+ border-left: 4px solid #4caf50;
262
+ }
263
+
264
+ /* Sidebar styling */
265
+ .difficulty-selector {
266
+ background: linear-gradient(135deg, #fff3e0 0%, #ffe0b2 100%);
267
+ padding: 15px;
268
+ border-radius: 10px;
269
+ margin: 10px 0;
270
+ border: 1px solid #ffcc80;
271
+ }
272
+
273
+ .math-highlight {
274
+ background: linear-gradient(135deg, #e8f5e8 0%, #c8e6c9 100%);
275
+ padding: 12px;
276
+ border-left: 4px solid #4caf50;
277
+ margin: 10px 0;
278
+ border-radius: 8px;
279
+ }
280
+
281
+ /* Responsive design */
282
+ @media (max-width: 768px) {
283
+ .katex { font-size: 1em !important; }
284
+ .katex-display { font-size: 1.1em !important; }
285
+ }
286
  """
287
  ) as demo:
288
 
289
+ gr.Markdown("""
290
+ # 🧮 **Mathetics AI** - Advanced Mathematics Solver
291
+
292
+ **Your Personal AI Math Tutor** | Specialized in step-by-step problem solving with beautiful LaTeX rendering
293
+
294
+ ---
295
+ """)
296
 
297
  with gr.Row():
298
  with gr.Column(scale=4):
 
307
  elem_classes=["difficulty-selector"]
308
  )
309
 
310
+ sample_btn = gr.Button("🎯 Get Sample Problem", variant="secondary", size="sm")
311
+ help_btn = gr.Button("❓ Math Help Tips", variant="secondary", size="sm")
312
 
313
  gr.Markdown("### 🔧 **Quick Tools**")
314
+ gr.Markdown("""
315
+ - **Algebra**: Equations, inequalities, factoring
316
+ - **Geometry**: Area, volume, trigonometry
317
+ - **Calculus**: Derivatives, integrals, limits
318
+ - **Statistics**: Probability, distributions
319
+ - **Number Theory**: Prime factorization, GCD/LCM
320
+ """)
321
 
322
+ # Footer with enhanced info
323
  gr.Markdown("""
324
  ---
325
+ **🔧 Technical Details:**
326
+ - **Model**: Qwen/Qwen2.5-Math-7B-Instruct (Specialized for Mathematics)
327
+ - **Features**: Real-time streaming • LaTeX rendering • Step-by-step solutions
328
 
329
+ **💡 Usage Tips:**
330
+ - Be specific about what you want to solve
331
+ - Request step-by-step solutions explicitly
332
+ - Use "lim(x->0)" for limits (mobile-friendly)
333
+ - Crank tokens to 1500+ for complex problems
334
  """)
335
 
336
  # Event handlers
337
+ def handle_sample(difficulty):
338
+ sample = get_random_sample()
339
+ return sample
340
+
341
  sample_btn.click(
342
+ handle_sample,
343
  inputs=[difficulty_preset],
344
+ outputs=gr.Textbox(visible=False) # This will trigger the chat input
345
  )
346
 
347
  help_btn.click(
348
  show_help,
349
+ outputs=gr.Markdown(label="Help", visible=True)
350
  )
351
 
352
  if __name__ == "__main__":