Khoi1234210 commited on
Commit
aaf8367
·
verified ·
1 Parent(s): 819a580

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -12
app.py CHANGED
@@ -1,17 +1,19 @@
1
  import gradio as gr
2
- import os
3
  from huggingface_hub import InferenceClient
4
  from datasets import load_dataset
5
  import random
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
 
13
- def load_sample_problems():
14
- """Load sample problems from math datasets"""
 
 
 
 
 
15
  try:
16
  gsm8k = load_dataset("openai/gsm8k", "main", streaming=True)
17
  samples = []
@@ -20,21 +22,21 @@ def load_sample_problems():
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:**
@@ -144,7 +146,10 @@ def respond(message, history, system_message, max_tokens, temperature, top_p):
144
  yield error_msg
145
 
146
  def get_random_sample():
147
- """Get a random sample problem"""
 
 
 
148
  if math_samples:
149
  return random.choice(math_samples)
150
  return "Solve for x: 2x² + 5x - 3 = 0"
 
1
  import gradio as gr
 
2
  from huggingface_hub import InferenceClient
3
  from datasets import load_dataset
4
  import random
5
  import re
6
 
7
+ # Global datasets - load lazily
8
+ math_samples = None
 
 
9
 
10
+ def load_datasets_lazy():
11
+ """Load datasets only when needed - prevents startup crashes"""
12
+ global math_samples
13
+ if math_samples is not None:
14
+ return math_samples
15
+
16
+ print("🔄 Loading datasets...")
17
  try:
18
  gsm8k = load_dataset("openai/gsm8k", "main", streaming=True)
19
  samples = []
 
22
  if i >= 50:
23
  break
24
  print(f"✅ Loaded {len(samples)} GSM8K samples")
25
+ math_samples = samples
26
  return samples
27
  except Exception as e:
28
  print(f"⚠️ Dataset error: {e}, using fallback")
29
+ math_samples = [
30
  "What is the derivative of f(x) = 3x² + 2x - 1?",
31
  "A triangle has sides of length 5, 12, and 13. What is its area?",
32
  "If log₂(x) + log₂(x+6) = 4, find the value of x.",
33
  "Find the limit: lim(x->0) (sin(x)/x)",
34
  "Solve the system: x + 2y = 7, 3x - y = 4"
35
  ]
36
+ return math_samples
 
37
 
38
  def create_math_system_message():
39
+ """Specialized system prompt for mathematics with LaTeX"""
40
  return """You are Mathetics AI, an advanced mathematics tutor and problem solver.
41
 
42
  🧮 **Your Expertise:**
 
146
  yield error_msg
147
 
148
  def get_random_sample():
149
+ """Get a random sample problem - loads datasets if needed"""
150
+ global math_samples
151
+ if math_samples is None:
152
+ math_samples = load_datasets_lazy()
153
  if math_samples:
154
  return random.choice(math_samples)
155
  return "Solve for x: 2x² + 5x - 3 = 0"