Spaces:
Sleeping
Sleeping
fix: revert to working version
Browse files
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(
|
30 |
"""Format input prompt for the model"""
|
31 |
-
|
32 |
-
return f"""Given a mathematical function, find its derivative.
|
33 |
|
34 |
-
Function: {
|
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 |
-
|
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(
|
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
|
74 |
-
"""Solve
|
75 |
-
if not
|
76 |
-
return "Please enter a
|
77 |
-
|
78 |
-
# Record problem type
|
79 |
-
monitor.record_problem_type(problem_type)
|
80 |
|
81 |
-
|
82 |
-
|
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
|
91 |
|
92 |
Let's verify this step by step:
|
93 |
-
1. Starting with f(x) = {
|
94 |
2. Applying differentiation rules
|
95 |
-
3. We get f'(x) = {
|
96 |
-
|
97 |
-
# Get updated statistics
|
98 |
-
stats = monitor.get_statistics()
|
99 |
|
100 |
-
|
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
|
119 |
-
gr.Markdown("# Mathematics
|
120 |
-
gr.Markdown("Using our fine-tuned model to solve
|
121 |
|
122 |
with gr.Row():
|
123 |
with gr.Column():
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
label="Problem Type"
|
128 |
)
|
129 |
-
|
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 |
-
|
137 |
label="Solution with Steps",
|
138 |
lines=6
|
139 |
)
|
140 |
|
141 |
-
#
|
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
|
149 |
-
["
|
150 |
-
["
|
151 |
-
["\\
|
152 |
-
["
|
153 |
-
["\\
|
154 |
-
["x
|
155 |
-
["
|
156 |
],
|
157 |
-
inputs=
|
158 |
-
outputs=
|
159 |
-
fn=
|
160 |
cache_examples=True,
|
161 |
)
|
162 |
|
163 |
# Connect the interface
|
164 |
solve_btn.click(
|
165 |
-
fn=
|
166 |
-
inputs=[
|
167 |
-
outputs=
|
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__":
|