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

feat: combine working model loading with comparison features

Browse files
Files changed (1) hide show
  1. app.py +120 -52
app.py CHANGED
@@ -2,37 +2,56 @@ 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)
@@ -41,79 +60,128 @@ def generate_derivative(function: str, max_length: int = 200) -> str:
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__":
 
2
  import torch
3
  from transformers import AutoModelForCausalLM, AutoTokenizer
4
  from peft import PeftModel
5
+ from monitoring import PerformanceMonitor, measure_time
6
 
7
  # Model configurations
8
  BASE_MODEL = "HuggingFaceTB/SmolLM2-1.7B-Instruct" # Base model
9
  ADAPTER_MODEL = "Joash2024/Math-SmolLM2-1.7B" # Our LoRA adapter
10
 
11
+ # Initialize performance monitor
12
+ monitor = PerformanceMonitor()
13
+
14
  print("Loading tokenizer...")
15
  tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL)
16
  tokenizer.pad_token = tokenizer.eos_token
17
 
18
  print("Loading base model...")
19
+ base_model = AutoModelForCausalLM.from_pretrained(
20
  BASE_MODEL,
21
  device_map="auto",
22
  torch_dtype=torch.float16
23
  )
24
 
25
+ print("Loading fine-tuned model...")
26
+ finetuned_model = PeftModel.from_pretrained(base_model, ADAPTER_MODEL)
27
+
28
+ # Set models to eval mode
29
+ base_model.eval()
30
+ finetuned_model.eval()
31
 
32
+ def format_prompt(problem: str, problem_type: str) -> str:
33
  """Format input prompt for the model"""
34
+ if problem_type == "Derivative":
35
+ return f"""Given a mathematical function, find its derivative.
36
 
37
+ Function: {problem}
38
  The derivative of this function is:"""
39
+ elif problem_type == "Addition":
40
+ return f"""Solve this addition problem.
41
+
42
+ Problem: {problem}
43
+ The solution is:"""
44
+ else: # Roots or Custom
45
+ return f"""Find the derivative of this function.
46
 
47
+ Function: {problem}
48
+ The derivative is:"""
49
+
50
+ @measure_time
51
+ def get_model_response(problem: str, problem_type: str, model) -> str:
52
+ """Generate response from a specific model"""
53
+ # Format prompt
54
+ prompt = format_prompt(problem, problem_type)
55
 
56
  # Tokenize
57
  inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
 
60
  with torch.no_grad():
61
  outputs = model.generate(
62
  **inputs,
63
+ max_length=100,
64
  num_return_sequences=1,
65
  temperature=0.1,
66
  do_sample=True,
67
  pad_token_id=tokenizer.eos_token_id
68
  )
69
 
70
+ # Decode and extract response
71
  generated = tokenizer.decode(outputs[0], skip_special_tokens=True)
72
+ response = generated[len(prompt):].strip()
73
 
74
+ return response
75
 
76
+ def solve_problem(problem: str, problem_type: str) -> tuple:
77
+ """Solve a math problem using both models"""
78
+ if not problem:
79
+ return "Please enter a problem", "Please enter a problem", None
80
+
81
+ # Record problem type
82
+ monitor.record_problem_type(problem_type)
83
 
84
+ # Get responses from both models with timing
85
+ base_response, base_time = get_model_response(problem, problem_type, base_model)
86
+ finetuned_response, finetuned_time = get_model_response(problem, problem_type, finetuned_model)
87
 
88
+ # Format responses with steps
89
+ base_output = f"""Solution: {base_response}
90
 
91
  Let's verify this step by step:
92
+ 1. Starting with f(x) = {problem}
93
  2. Applying differentiation rules
94
+ 3. We get f'(x) = {base_response}"""
95
+
96
+ finetuned_output = f"""Solution: {finetuned_response}
97
+
98
+ Let's verify this step by step:
99
+ 1. Starting with f(x) = {problem}
100
+ 2. Applying differentiation rules
101
+ 3. We get f'(x) = {finetuned_response}"""
102
+
103
+ # Record metrics
104
+ monitor.record_response_time("base", base_time)
105
+ monitor.record_response_time("finetuned", finetuned_time)
106
+ monitor.record_success("base", not base_response.startswith("Error"))
107
+ monitor.record_success("finetuned", not finetuned_response.startswith("Error"))
108
+
109
+ # Get updated statistics
110
+ stats = monitor.get_statistics()
111
 
112
+ # Format statistics for display
113
+ stats_display = f"""
114
+ ### Performance Metrics
115
+
116
+ #### Response Times (seconds)
117
+ - Base Model: {stats.get('base_avg_response_time', 0):.2f} avg
118
+ - Fine-tuned Model: {stats.get('finetuned_avg_response_time', 0):.2f} avg
119
+
120
+ #### Success Rates
121
+ - Base Model: {stats.get('base_success_rate', 0):.1f}%
122
+ - Fine-tuned Model: {stats.get('finetuned_success_rate', 0):.1f}%
123
+
124
+ #### Problem Types Used
125
+ """
126
+ for ptype, percentage in stats.get('problem_type_distribution', {}).items():
127
+ stats_display += f"- {ptype}: {percentage:.1f}%\n"
128
+
129
+ return base_output, finetuned_output, stats_display
130
 
131
  # Create Gradio interface
132
+ with gr.Blocks(title="Mathematics Problem Solver") as demo:
133
+ gr.Markdown("# Mathematics Problem Solver")
134
+ gr.Markdown("Compare solutions between base and fine-tuned models")
135
 
136
  with gr.Row():
137
  with gr.Column():
138
+ problem_type = gr.Dropdown(
139
+ choices=["Addition", "Root Finding", "Derivative", "Custom"],
140
+ value="Derivative",
141
+ label="Problem Type"
142
+ )
143
+ problem_input = gr.Textbox(
144
+ label="Enter your math problem",
145
+ placeholder="Example: x^2 + 3x"
146
  )
147
+ solve_btn = gr.Button("Solve", variant="primary")
148
 
149
  with gr.Row():
150
+ with gr.Column():
151
+ gr.Markdown("### Base Model")
152
+ base_output = gr.Textbox(label="Base Model Solution", lines=5)
153
+
154
+ with gr.Column():
155
+ gr.Markdown("### Fine-tuned Model")
156
+ finetuned_output = gr.Textbox(label="Fine-tuned Model Solution", lines=5)
157
+
158
+ # Performance metrics display
159
+ with gr.Row():
160
+ metrics_display = gr.Markdown("### Performance Metrics\n*Solve a problem to see metrics*")
161
 
162
+ # Example problems
163
  gr.Examples(
164
  examples=[
165
+ ["x^2 + 3x", "Derivative"],
166
+ ["144", "Root Finding"],
167
+ ["235 + 567", "Addition"],
168
+ ["\\sin{\\left(x\\right)}", "Derivative"],
169
+ ["e^x", "Derivative"],
170
+ ["\\frac{1}{x}", "Derivative"],
171
+ ["x^3 + 2x", "Derivative"],
172
+ ["\\cos{\\left(x^2\\right)}", "Derivative"]
173
  ],
174
+ inputs=[problem_input, problem_type],
175
+ outputs=[base_output, finetuned_output, metrics_display],
176
+ fn=solve_problem,
177
  cache_examples=True,
178
  )
179
 
180
  # Connect the interface
181
  solve_btn.click(
182
+ fn=solve_problem,
183
+ inputs=[problem_input, problem_type],
184
+ outputs=[base_output, finetuned_output, metrics_display]
185
  )
186
 
187
  if __name__ == "__main__":