Joash2024 commited on
Commit
ced6298
·
1 Parent(s): 98a6116

feat: switch to single model with dropdown selection

Browse files
Files changed (1) hide show
  1. app.py +37 -47
app.py CHANGED
@@ -5,8 +5,10 @@ import numpy as np
5
  from monitoring import PerformanceMonitor, measure_time
6
 
7
  # Model IDs
8
- BASE_MODEL_ID = "HuggingFaceTB/SmolLM2-1.7B-Instruct" # Base model
9
- FINETUNED_MODEL_ID = "Joash2024/Math-SmolLM2-1.7B" # Our fine-tuned model
 
 
10
 
11
  # Initialize performance monitor
12
  monitor = PerformanceMonitor()
@@ -43,10 +45,10 @@ def get_model_response(problem, model_id):
43
  except Exception as e:
44
  return f"Error: {str(e)}"
45
 
46
- def solve_problem(problem, problem_type):
47
- """Solve a math problem using both models"""
48
  if not problem:
49
- return "Please enter a problem", "Please enter a problem", None
50
 
51
  # Record problem type
52
  monitor.record_problem_type(problem_type)
@@ -55,30 +57,21 @@ def solve_problem(problem, problem_type):
55
  if problem_type != "Custom":
56
  problem = f"{problem_type}: {problem}"
57
 
58
- # Get responses from both models with timing
59
- base_response, base_time = get_model_response(problem, BASE_MODEL_ID)
60
- finetuned_response, finetuned_time = get_model_response(problem, FINETUNED_MODEL_ID)
61
 
62
- # Format responses with steps
63
- base_output = f"""Solution: {base_response}
64
 
65
  Let's verify this step by step:
66
  1. Starting with f(x) = {problem}
67
  2. Applying differentiation rules
68
- 3. We get f'(x) = {base_response}"""
69
-
70
- finetuned_output = f"""Solution: {finetuned_response}
71
-
72
- Let's verify this step by step:
73
- 1. Starting with f(x) = {problem}
74
- 2. Applying differentiation rules
75
- 3. We get f'(x) = {finetuned_response}"""
76
 
77
  # Record metrics
78
- monitor.record_response_time("base", base_time)
79
- monitor.record_response_time("finetuned", finetuned_time)
80
- monitor.record_success("base", not base_response.startswith("Error"))
81
- monitor.record_success("finetuned", not finetuned_response.startswith("Error"))
82
 
83
  # Get updated statistics
84
  stats = monitor.get_statistics()
@@ -88,24 +81,22 @@ Let's verify this step by step:
88
  ### Performance Metrics
89
 
90
  #### Response Times (seconds)
91
- - Base Model: {stats.get('base_avg_response_time', 0):.2f} avg
92
- - Fine-tuned Model: {stats.get('finetuned_avg_response_time', 0):.2f} avg
93
 
94
  #### Success Rates
95
- - Base Model: {stats.get('base_success_rate', 0):.1f}%
96
- - Fine-tuned Model: {stats.get('finetuned_success_rate', 0):.1f}%
97
 
98
  #### Problem Types Used
99
  """
100
  for ptype, percentage in stats.get('problem_type_distribution', {}).items():
101
  stats_display += f"- {ptype}: {percentage:.1f}%\n"
102
 
103
- return base_output, finetuned_output, stats_display
104
 
105
  # Create Gradio interface
106
  with gr.Blocks(title="Mathematics Problem Solver") as demo:
107
  gr.Markdown("# Mathematics Problem Solver")
108
- gr.Markdown("Compare solutions between base and fine-tuned models")
109
 
110
  with gr.Row():
111
  with gr.Column():
@@ -114,6 +105,11 @@ with gr.Blocks(title="Mathematics Problem Solver") as demo:
114
  value="Derivative",
115
  label="Problem Type"
116
  )
 
 
 
 
 
117
  problem_input = gr.Textbox(
118
  label="Enter your math problem",
119
  placeholder="Example: x^2 + 3x"
@@ -121,13 +117,7 @@ with gr.Blocks(title="Mathematics Problem Solver") as demo:
121
  solve_btn = gr.Button("Solve", variant="primary")
122
 
123
  with gr.Row():
124
- with gr.Column():
125
- gr.Markdown("### Base Model")
126
- base_output = gr.Textbox(label="Base Model Solution", lines=5)
127
-
128
- with gr.Column():
129
- gr.Markdown("### Fine-tuned Model")
130
- finetuned_output = gr.Textbox(label="Fine-tuned Model Solution", lines=5)
131
 
132
  # Performance metrics display
133
  with gr.Row():
@@ -136,17 +126,17 @@ with gr.Blocks(title="Mathematics Problem Solver") as demo:
136
  # Example problems
137
  gr.Examples(
138
  examples=[
139
- ["x^2 + 3x", "Derivative"],
140
- ["144", "Root Finding"],
141
- ["235 + 567", "Addition"],
142
- ["\\sin{\\left(x\\right)}", "Derivative"],
143
- ["e^x", "Derivative"],
144
- ["\\frac{1}{x}", "Derivative"],
145
- ["x^3 + 2x", "Derivative"],
146
- ["\\cos{\\left(x^2\\right)}", "Derivative"]
147
  ],
148
- inputs=[problem_input, problem_type],
149
- outputs=[base_output, finetuned_output, metrics_display],
150
  fn=solve_problem,
151
  cache_examples=True,
152
  )
@@ -154,8 +144,8 @@ with gr.Blocks(title="Mathematics Problem Solver") as demo:
154
  # Connect the interface
155
  solve_btn.click(
156
  fn=solve_problem,
157
- inputs=[problem_input, problem_type],
158
- outputs=[base_output, finetuned_output, metrics_display]
159
  )
160
 
161
  if __name__ == "__main__":
 
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()
 
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)
 
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()
 
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():
 
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"
 
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():
 
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
  )
 
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__":