Spaces:
Sleeping
Sleeping
fix: use working test space code with A100
Browse files
README.md
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
---
|
2 |
-
title: Mathematics
|
3 |
emoji: 🧮
|
4 |
colorFrom: blue
|
5 |
colorTo: green
|
@@ -18,29 +18,16 @@ python_packages:
|
|
18 |
- "numpy>=1.21.0"
|
19 |
---
|
20 |
|
21 |
-
# Mathematics
|
22 |
|
23 |
-
This Space demonstrates our fine-tuned math model for solving
|
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 |
-
-
|
31 |
-
-
|
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
|
5 |
-
from
|
6 |
|
7 |
-
# Model
|
8 |
-
|
9 |
-
|
10 |
-
"Fine-tuned Model": "Joash2024/Math-SmolLM2-1.7B"
|
11 |
-
}
|
12 |
|
13 |
-
|
14 |
-
|
|
|
15 |
|
16 |
-
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
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
|
49 |
-
"""
|
50 |
-
|
51 |
-
|
52 |
|
53 |
-
#
|
54 |
-
|
55 |
|
56 |
-
#
|
57 |
-
|
58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
-
#
|
61 |
-
|
62 |
-
|
63 |
|
64 |
-
|
65 |
-
output = f"""Solution: {response}
|
66 |
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
|
72 |
-
|
73 |
-
|
74 |
-
monitor.record_success(model_type, not response.startswith("Error"))
|
75 |
|
76 |
-
#
|
77 |
-
|
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 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
|
94 |
-
return output
|
95 |
|
96 |
# Create Gradio interface
|
97 |
-
with gr.Blocks(title="Mathematics
|
98 |
-
gr.Markdown("# Mathematics
|
99 |
-
gr.Markdown("
|
100 |
|
101 |
with gr.Row():
|
102 |
with gr.Column():
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
label="Problem Type"
|
107 |
)
|
108 |
-
|
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 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
metrics_display = gr.Markdown("### Performance Metrics\n*Solve a problem to see metrics*")
|
125 |
|
126 |
-
# Example
|
127 |
gr.Examples(
|
128 |
examples=[
|
129 |
-
["x^2
|
130 |
-
["
|
131 |
-
["
|
132 |
-
["\\
|
133 |
-
["
|
134 |
-
["\\
|
135 |
-
["x
|
136 |
-
["
|
137 |
],
|
138 |
-
inputs=
|
139 |
-
outputs=
|
140 |
-
fn=
|
141 |
cache_examples=True,
|
142 |
)
|
143 |
|
144 |
# Connect the interface
|
145 |
solve_btn.click(
|
146 |
-
fn=
|
147 |
-
inputs=[
|
148 |
-
outputs=[
|
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__":
|