Joash2024 commited on
Commit
44245fd
·
1 Parent(s): c1a3f6d

fix: revert to working version

Browse files
Files changed (1) hide show
  1. app.py +38 -89
app.py CHANGED
@@ -2,15 +2,11 @@ import gradio as gr
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
@@ -26,29 +22,17 @@ print("Loading LoRA adapter...")
26
  model = PeftModel.from_pretrained(model, ADAPTER_MODEL)
27
  model.eval()
28
 
29
- def format_prompt(problem: str, problem_type: str) -> str:
30
  """Format input prompt for the model"""
31
- if problem_type == "Derivative":
32
- return f"""Given a mathematical function, find its derivative.
33
 
34
- Function: {problem}
35
  The derivative of this function is:"""
36
- elif problem_type == "Addition":
37
- return f"""Solve this addition problem.
38
-
39
- Problem: {problem}
40
- The solution is:"""
41
- else: # Roots or Custom
42
- return f"""Find the derivative of this function.
43
 
44
- Function: {problem}
45
- The derivative is:"""
46
-
47
- @measure_time
48
- def generate_derivative(problem: str, problem_type: str, max_length: int = 200) -> str:
49
  """Generate derivative for a given function"""
50
  # Format the prompt
51
- prompt = format_prompt(problem, problem_type)
52
 
53
  # Tokenize
54
  inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
@@ -70,101 +54,66 @@ def generate_derivative(problem: str, problem_type: str, max_length: int = 200)
70
 
71
  return derivative
72
 
73
- def solve_problem(problem: str, problem_type: str) -> tuple:
74
- """Solve problem and format output"""
75
- if not problem:
76
- return "Please enter a problem", None
77
-
78
- # Record problem type
79
- monitor.record_problem_type(problem_type)
80
 
81
- # Generate solution
82
- print(f"\nGenerating solution for: {problem}")
83
- solution, time_taken = generate_derivative(problem, problem_type)
84
-
85
- # Record metrics
86
- monitor.record_response_time("model", time_taken)
87
- monitor.record_success("model", not solution.startswith("Error"))
88
 
89
  # Format output with step-by-step explanation
90
- output = f"""Generated solution: {solution}
91
 
92
  Let's verify this step by step:
93
- 1. Starting with f(x) = {problem}
94
  2. Applying differentiation rules
95
- 3. We get f'(x) = {solution}"""
96
-
97
- # Get updated statistics
98
- stats = monitor.get_statistics()
99
 
100
- # Format statistics for display
101
- stats_display = f"""
102
- ### Performance Metrics
103
-
104
- #### Response Times
105
- - Average: {stats.get('model_avg_response_time', 0):.2f} seconds
106
-
107
- #### Success Rate
108
- - {stats.get('model_success_rate', 0):.1f}%
109
-
110
- #### Problem Types Used
111
- """
112
- for ptype, percentage in stats.get('problem_type_distribution', {}).items():
113
- stats_display += f"- {ptype}: {percentage:.1f}%\n"
114
-
115
- return output, stats_display
116
 
117
  # Create Gradio interface
118
- with gr.Blocks(title="Mathematics Problem Solver") as demo:
119
- gr.Markdown("# Mathematics Problem Solver")
120
- gr.Markdown("Using our fine-tuned model to solve mathematical problems")
121
 
122
  with gr.Row():
123
  with gr.Column():
124
- problem_type = gr.Dropdown(
125
- choices=["Addition", "Root Finding", "Derivative", "Custom"],
126
- value="Derivative",
127
- label="Problem Type"
128
  )
129
- problem_input = gr.Textbox(
130
- label="Enter your problem",
131
- placeholder="Example: x^2 + 3x"
132
- )
133
- solve_btn = gr.Button("Solve", variant="primary")
134
 
135
  with gr.Row():
136
- solution_output = gr.Textbox(
137
  label="Solution with Steps",
138
  lines=6
139
  )
140
 
141
- # Performance metrics display
142
- with gr.Row():
143
- metrics_display = gr.Markdown("### Performance Metrics\n*Solve a problem to see metrics*")
144
-
145
- # Example problems
146
  gr.Examples(
147
  examples=[
148
- ["x^2 + 3x", "Derivative"],
149
- ["144", "Root Finding"],
150
- ["235 + 567", "Addition"],
151
- ["\\sin{\\left(x\\right)}", "Derivative"],
152
- ["e^x", "Derivative"],
153
- ["\\frac{1}{x}", "Derivative"],
154
- ["x^3 + 2x", "Derivative"],
155
- ["\\cos{\\left(x^2\\right)}", "Derivative"]
156
  ],
157
- inputs=[problem_input, problem_type],
158
- outputs=[solution_output, metrics_display],
159
- fn=solve_problem,
160
  cache_examples=True,
161
  )
162
 
163
  # Connect the interface
164
  solve_btn.click(
165
- fn=solve_problem,
166
- inputs=[problem_input, problem_type],
167
- outputs=[solution_output, metrics_display]
168
  )
169
 
170
  if __name__ == "__main__":
 
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
 
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)
 
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__":