Joash2024's picture
feat: add monitoring and problem types
dcd4f06
raw
history blame
5.17 kB
import gradio as gr
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
from peft import PeftModel
from monitoring import PerformanceMonitor, measure_time
# Model configurations
BASE_MODEL = "HuggingFaceTB/SmolLM2-1.7B-Instruct" # Base model
ADAPTER_MODEL = "Joash2024/Math-SmolLM2-1.7B" # Our LoRA adapter
# Initialize performance monitor
monitor = PerformanceMonitor()
print("Loading tokenizer...")
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL)
tokenizer.pad_token = tokenizer.eos_token
print("Loading base model...")
model = AutoModelForCausalLM.from_pretrained(
BASE_MODEL,
device_map="auto",
torch_dtype=torch.float16
)
print("Loading LoRA adapter...")
model = PeftModel.from_pretrained(model, ADAPTER_MODEL)
model.eval()
def format_prompt(problem: str, problem_type: str) -> str:
"""Format input prompt for the model"""
if problem_type == "Derivative":
return f"""Given a mathematical function, find its derivative.
Function: {problem}
The derivative of this function is:"""
elif problem_type == "Addition":
return f"""Solve this addition problem.
Problem: {problem}
The solution is:"""
else: # Roots or Custom
return f"""Find the derivative of this function.
Function: {problem}
The derivative is:"""
@measure_time
def generate_derivative(problem: str, problem_type: str, max_length: int = 200) -> str:
"""Generate derivative for a given function"""
# Format the prompt
prompt = format_prompt(problem, problem_type)
# Tokenize
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
# Generate
with torch.no_grad():
outputs = model.generate(
**inputs,
max_length=max_length,
num_return_sequences=1,
temperature=0.1,
do_sample=True,
pad_token_id=tokenizer.eos_token_id
)
# Decode and extract derivative
generated = tokenizer.decode(outputs[0], skip_special_tokens=True)
derivative = generated[len(prompt):].strip()
return derivative
def solve_problem(problem: str, problem_type: str) -> tuple:
"""Solve problem and format output"""
if not problem:
return "Please enter a problem", None
# Record problem type
monitor.record_problem_type(problem_type)
# Generate solution
print(f"\nGenerating solution for: {problem}")
solution, time_taken = generate_derivative(problem, problem_type)
# Record metrics
monitor.record_response_time("model", time_taken)
monitor.record_success("model", not solution.startswith("Error"))
# Format output with step-by-step explanation
output = f"""Generated solution: {solution}
Let's verify this step by step:
1. Starting with f(x) = {problem}
2. Applying differentiation rules
3. We get f'(x) = {solution}"""
# Get updated statistics
stats = monitor.get_statistics()
# Format statistics for display
stats_display = f"""
### Performance Metrics
#### Response Times
- Average: {stats.get('model_avg_response_time', 0):.2f} seconds
#### Success Rate
- {stats.get('model_success_rate', 0):.1f}%
#### Problem Types Used
"""
for ptype, percentage in stats.get('problem_type_distribution', {}).items():
stats_display += f"- {ptype}: {percentage:.1f}%\n"
return output, stats_display
# Create Gradio interface
with gr.Blocks(title="Mathematics Problem Solver") as demo:
gr.Markdown("# Mathematics Problem Solver")
gr.Markdown("Using our fine-tuned model to solve mathematical problems")
with gr.Row():
with gr.Column():
problem_type = gr.Dropdown(
choices=["Addition", "Root Finding", "Derivative", "Custom"],
value="Derivative",
label="Problem Type"
)
problem_input = gr.Textbox(
label="Enter your problem",
placeholder="Example: x^2 + 3x"
)
solve_btn = gr.Button("Solve", variant="primary")
with gr.Row():
solution_output = gr.Textbox(
label="Solution with Steps",
lines=6
)
# Performance metrics display
with gr.Row():
metrics_display = gr.Markdown("### Performance Metrics\n*Solve a problem to see metrics*")
# Example problems
gr.Examples(
examples=[
["x^2 + 3x", "Derivative"],
["144", "Root Finding"],
["235 + 567", "Addition"],
["\\sin{\\left(x\\right)}", "Derivative"],
["e^x", "Derivative"],
["\\frac{1}{x}", "Derivative"],
["x^3 + 2x", "Derivative"],
["\\cos{\\left(x^2\\right)}", "Derivative"]
],
inputs=[problem_input, problem_type],
outputs=[solution_output, metrics_display],
fn=solve_problem,
cache_examples=True,
)
# Connect the interface
solve_btn.click(
fn=solve_problem,
inputs=[problem_input, problem_type],
outputs=[solution_output, metrics_display]
)
if __name__ == "__main__":
demo.launch()