Joash2024 commited on
Commit
344dad8
·
1 Parent(s): ced6298

fix: use working test space code with A100

Browse files
Files changed (2) hide show
  1. README.md +5 -18
  2. app.py +83 -115
README.md CHANGED
@@ -1,5 +1,5 @@
1
  ---
2
- title: Mathematics Problem Solver
3
  emoji: 🧮
4
  colorFrom: blue
5
  colorTo: green
@@ -18,29 +18,16 @@ python_packages:
18
  - "numpy>=1.21.0"
19
  ---
20
 
21
- # Mathematics Problem Solver
22
 
23
- This Space demonstrates our fine-tuned math model for solving various mathematical problems, with a focus on derivatives. Compare solutions between:
24
 
25
  1. Base Model: HuggingFaceTB/SmolLM2-1.7B-Instruct
26
  2. Our Fine-tuned Model: Joash2024/Math-SmolLM2-1.7B
27
 
28
  ## Features
29
 
30
- - Side-by-side comparison of base and fine-tuned models
31
- - Performance monitoring:
32
- - Response times
33
- - Success rates
34
- - Problem type distribution
35
- - Support for various problems:
36
- - Derivatives
37
- - Addition
38
- - Roots
39
- - Custom problems
40
-
41
- ## Technical Details
42
-
43
  - A100 GPU acceleration
44
  - Float16 precision for efficient inference
45
- - LaTeX notation support
46
- - Real-time performance tracking
 
1
  ---
2
+ title: Mathematics Derivative Solver
3
  emoji: 🧮
4
  colorFrom: blue
5
  colorTo: green
 
18
  - "numpy>=1.21.0"
19
  ---
20
 
21
+ # Mathematics Derivative Solver
22
 
23
+ This Space demonstrates our fine-tuned math model for solving derivatives. We use:
24
 
25
  1. Base Model: HuggingFaceTB/SmolLM2-1.7B-Instruct
26
  2. Our Fine-tuned Model: Joash2024/Math-SmolLM2-1.7B
27
 
28
  ## Features
29
 
30
+ - Step-by-step derivative solutions
31
+ - LaTeX notation support
 
 
 
 
 
 
 
 
 
 
 
32
  - A100 GPU acceleration
33
  - Float16 precision for efficient inference
 
 
app.py CHANGED
@@ -1,151 +1,119 @@
1
  import gradio as gr
2
- from transformers import pipeline
3
  import torch
4
- import numpy as np
5
- from monitoring import PerformanceMonitor, measure_time
6
 
7
- # Model IDs
8
- MODEL_OPTIONS = {
9
- "Base Model": "HuggingFaceTB/SmolLM2-1.7B-Instruct",
10
- "Fine-tuned Model": "Joash2024/Math-SmolLM2-1.7B"
11
- }
12
 
13
- # Initialize performance monitor
14
- monitor = PerformanceMonitor()
 
15
 
16
- def format_prompt(problem):
17
- """Format the input problem according to the model's expected format"""
18
- return f"Given a mathematical function, find its derivative.\n\nFunction: {problem}\nThe derivative of this function is:"
 
 
 
19
 
20
- @measure_time
21
- def get_model_response(problem, model_id):
22
- """Get response from a specific model"""
23
- try:
24
- # Initialize pipeline for each request
25
- pipe = pipeline(
26
- "text-generation",
27
- model=model_id,
28
- torch_dtype=torch.float16,
29
- device_map="auto",
30
- model_kwargs={"low_cpu_mem_usage": True}
31
- )
32
-
33
- # Format prompt and generate response
34
- prompt = format_prompt(problem)
35
- response = pipe(
36
- prompt,
37
- max_new_tokens=50, # Shorter response
38
- temperature=0.1,
39
- do_sample=False, # Deterministic
40
- num_return_sequences=1,
41
- return_full_text=False # Only return new text
42
- )[0]["generated_text"]
43
-
44
- return response.strip()
45
- except Exception as e:
46
- return f"Error: {str(e)}"
47
 
48
- def solve_problem(problem, problem_type, model_type):
49
- """Solve a math problem using the selected model"""
50
- if not problem:
51
- return "Please enter a problem", None
52
 
53
- # Record problem type
54
- monitor.record_problem_type(problem_type)
55
 
56
- # Add problem type context if provided
57
- if problem_type != "Custom":
58
- problem = f"{problem_type}: {problem}"
 
 
 
 
 
 
 
59
 
60
- # Get response from selected model
61
- model_id = MODEL_OPTIONS[model_type]
62
- response, time_taken = get_model_response(problem, model_id)
63
 
64
- # Format response with steps
65
- output = f"""Solution: {response}
66
 
67
- Let's verify this step by step:
68
- 1. Starting with f(x) = {problem}
69
- 2. Applying differentiation rules
70
- 3. We get f'(x) = {response}"""
71
 
72
- # Record metrics
73
- monitor.record_response_time(model_type, time_taken)
74
- monitor.record_success(model_type, not response.startswith("Error"))
75
 
76
- # Get updated statistics
77
- stats = monitor.get_statistics()
78
-
79
- # Format statistics for display
80
- stats_display = f"""
81
- ### Performance Metrics
82
-
83
- #### Response Times (seconds)
84
- - {model_type}: {stats.get(f'{model_type}_avg_response_time', 0):.2f} avg
85
-
86
- #### Success Rates
87
- - {model_type}: {stats.get(f'{model_type}_success_rate', 0):.1f}%
88
 
89
- #### Problem Types Used
90
- """
91
- for ptype, percentage in stats.get('problem_type_distribution', {}).items():
92
- stats_display += f"- {ptype}: {percentage:.1f}%\n"
93
 
94
- return output, stats_display
95
 
96
  # Create Gradio interface
97
- with gr.Blocks(title="Mathematics Problem Solver") as demo:
98
- gr.Markdown("# Mathematics Problem Solver")
99
- gr.Markdown("Test our models on mathematical problems")
100
 
101
  with gr.Row():
102
  with gr.Column():
103
- problem_type = gr.Dropdown(
104
- choices=["Addition", "Root Finding", "Derivative", "Custom"],
105
- value="Derivative",
106
- label="Problem Type"
107
  )
108
- model_type = gr.Dropdown(
109
- choices=list(MODEL_OPTIONS.keys()),
110
- value="Fine-tuned Model",
111
- label="Model to Use"
112
- )
113
- problem_input = gr.Textbox(
114
- label="Enter your math problem",
115
- placeholder="Example: x^2 + 3x"
116
- )
117
- solve_btn = gr.Button("Solve", variant="primary")
118
 
119
  with gr.Row():
120
- solution_output = gr.Textbox(label="Solution", lines=5)
121
-
122
- # Performance metrics display
123
- with gr.Row():
124
- metrics_display = gr.Markdown("### Performance Metrics\n*Solve a problem to see metrics*")
125
 
126
- # Example problems
127
  gr.Examples(
128
  examples=[
129
- ["x^2 + 3x", "Derivative", "Fine-tuned Model"],
130
- ["144", "Root Finding", "Fine-tuned Model"],
131
- ["235 + 567", "Addition", "Fine-tuned Model"],
132
- ["\\sin{\\left(x\\right)}", "Derivative", "Fine-tuned Model"],
133
- ["e^x", "Derivative", "Fine-tuned Model"],
134
- ["\\frac{1}{x}", "Derivative", "Fine-tuned Model"],
135
- ["x^3 + 2x", "Derivative", "Fine-tuned Model"],
136
- ["\\cos{\\left(x^2\\right)}", "Derivative", "Fine-tuned Model"]
137
  ],
138
- inputs=[problem_input, problem_type, model_type],
139
- outputs=[solution_output, metrics_display],
140
- fn=solve_problem,
141
  cache_examples=True,
142
  )
143
 
144
  # Connect the interface
145
  solve_btn.click(
146
- fn=solve_problem,
147
- inputs=[problem_input, problem_type, model_type],
148
- outputs=[solution_output, metrics_display]
149
  )
150
 
151
  if __name__ == "__main__":
 
1
  import gradio as gr
 
2
  import torch
3
+ from transformers import AutoModelForCausalLM, AutoTokenizer
4
+ from peft import PeftModel
5
 
6
+ # Model configurations
7
+ BASE_MODEL = "HuggingFaceTB/SmolLM2-1.7B-Instruct" # Base model
8
+ ADAPTER_MODEL = "Joash2024/Math-SmolLM2-1.7B" # Our LoRA adapter
 
 
9
 
10
+ print("Loading tokenizer...")
11
+ tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL)
12
+ tokenizer.pad_token = tokenizer.eos_token
13
 
14
+ print("Loading base model...")
15
+ model = AutoModelForCausalLM.from_pretrained(
16
+ BASE_MODEL,
17
+ device_map="auto",
18
+ torch_dtype=torch.float16
19
+ )
20
 
21
+ print("Loading LoRA adapter...")
22
+ model = PeftModel.from_pretrained(model, ADAPTER_MODEL)
23
+ model.eval()
24
+
25
+ def format_prompt(function: str) -> str:
26
+ """Format input prompt for the model"""
27
+ return f"""Given a mathematical function, find its derivative.
28
+
29
+ Function: {function}
30
+ The derivative of this function is:"""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
+ def generate_derivative(function: str, max_length: int = 200) -> str:
33
+ """Generate derivative for a given function"""
34
+ # Format the prompt
35
+ prompt = format_prompt(function)
36
 
37
+ # Tokenize
38
+ inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
39
 
40
+ # Generate
41
+ with torch.no_grad():
42
+ outputs = model.generate(
43
+ **inputs,
44
+ max_length=max_length,
45
+ num_return_sequences=1,
46
+ temperature=0.1,
47
+ do_sample=True,
48
+ pad_token_id=tokenizer.eos_token_id
49
+ )
50
 
51
+ # Decode and extract derivative
52
+ generated = tokenizer.decode(outputs[0], skip_special_tokens=True)
53
+ derivative = generated[len(prompt):].strip()
54
 
55
+ return derivative
 
56
 
57
+ def solve_derivative(function: str) -> str:
58
+ """Solve derivative and format output"""
59
+ if not function:
60
+ return "Please enter a function"
61
 
62
+ print(f"\nGenerating derivative for: {function}")
63
+ derivative = generate_derivative(function)
 
64
 
65
+ # Format output with step-by-step explanation
66
+ output = f"""Generated derivative: {derivative}
 
 
 
 
 
 
 
 
 
 
67
 
68
+ Let's verify this step by step:
69
+ 1. Starting with f(x) = {function}
70
+ 2. Applying differentiation rules
71
+ 3. We get f'(x) = {derivative}"""
72
 
73
+ return output
74
 
75
  # Create Gradio interface
76
+ with gr.Blocks(title="Mathematics Derivative Solver") as demo:
77
+ gr.Markdown("# Mathematics Derivative Solver")
78
+ gr.Markdown("Using our fine-tuned model to solve derivatives")
79
 
80
  with gr.Row():
81
  with gr.Column():
82
+ function_input = gr.Textbox(
83
+ label="Enter a function",
84
+ placeholder="Example: x^2, sin(x), e^x"
 
85
  )
86
+ solve_btn = gr.Button("Find Derivative", variant="primary")
 
 
 
 
 
 
 
 
 
87
 
88
  with gr.Row():
89
+ output = gr.Textbox(
90
+ label="Solution with Steps",
91
+ lines=6
92
+ )
 
93
 
94
+ # Example functions
95
  gr.Examples(
96
  examples=[
97
+ ["x^2"],
98
+ ["\\sin{\\left(x\\right)}"],
99
+ ["e^x"],
100
+ ["\\frac{1}{x}"],
101
+ ["x^3 + 2x"],
102
+ ["\\cos{\\left(x^2\\right)}"],
103
+ ["\\log{\\left(x\\right)}"],
104
+ ["x e^{-x}"]
105
  ],
106
+ inputs=function_input,
107
+ outputs=output,
108
+ fn=solve_derivative,
109
  cache_examples=True,
110
  )
111
 
112
  # Connect the interface
113
  solve_btn.click(
114
+ fn=solve_derivative,
115
+ inputs=[function_input],
116
+ outputs=[output]
117
  )
118
 
119
  if __name__ == "__main__":