Joash2024 commited on
Commit
8df9fb2
·
0 Parent(s):

feat: initial commit with working test code

Browse files
Files changed (3) hide show
  1. README.md +41 -0
  2. app.py +120 -0
  3. requirements.txt +6 -0
README.md ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Mathematics Derivative Solver V2
3
+ emoji: 🧮
4
+ colorFrom: blue
5
+ colorTo: green
6
+ sdk: gradio
7
+ sdk_version: 4.8.0
8
+ app_file: app.py
9
+ pinned: false
10
+ hardware:
11
+ accelerator: a100
12
+ gpu: true
13
+ python_packages:
14
+ - "torch>=2.0.0"
15
+ - "transformers>=4.30.0"
16
+ - "accelerate>=0.20.0"
17
+ - "peft==0.5.0"
18
+ - "numpy>=1.21.0"
19
+ ---
20
+
21
+ # Mathematics Derivative Solver V2
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
34
+
35
+ ## Supported Functions
36
+
37
+ - Polynomials (x^2, x^3 + 2x)
38
+ - Trigonometric (sin(x), cos(x))
39
+ - Exponential (e^x)
40
+ - Logarithmic (log(x))
41
+ - Combinations (x e^{-x})
app.py ADDED
@@ -0,0 +1,120 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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__":
120
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ torch>=2.0.0
2
+ transformers>=4.30.0
3
+ accelerate>=0.20.0
4
+ peft==0.5.0
5
+ gradio>=4.8.0
6
+ numpy>=1.21.0