A production-ready Python API designed to solve calculus expressions represented in SLaNg AST format. Refactored for seamless deployment on Vercel Serverless Functions.
- Dual Execution Modes:
- Neural Mode: Automatically activated if a PyTorch model checkpoint is detected (see checkpoint resolution priority below). Fits full transformer-based inference.
- Fallback Mode: Automatically active by default. Runs a deterministic, pure-Python polynomial solver (supports
diff,partial,integrate,gradient, andtangent_lineoperations). Requires zero heavy dependencies, making it extremely fast and lightweight.
- Pure Python Architecture:
- Re-implemented SLaNg AST validation, algebraic verification, and LaTeX conversion in 100% pure Python.
- Zero Node.js subprocesses. No dependency on local Node installations.
- Vercel Serverless Optimization:
- Organized as standalone serverless handlers under
api/using Vercel's nativeBaseHTTPRequestHandlerpython runtime. - Supercharged build speeds with minimized dependencies (~50MB package limitation compliant).
- Organized as standalone serverless handlers under
CalculusSolver/
├── api/
│ ├── _shared.py # Shared solver loading, LaTeX formatting & CORS helpers
│ ├── index.py # GET /api (Health Check & Active Mode info)
│ ├── solve.py # POST /api/solve (Calculus solving handler)
│ └── validate.py # POST /api/validate (SLaNg AST structure validator)
│ └── app.py # Starlette server (for local dev)
│
├── inference/
│ ├── beam_search.py # Pure-Python search logic
│ ├── fallback_solver.py # Deterministic polynomial solver
│ ├── solve.py # Neural model loader & solver pipeline
│ └── verifier.py # Pure-Python math verifier & test-point evaluator
│
├── model/
│ ├── architecture.py # PyTorch model definitions
│ └── ... # Tree encoder, tree decoder, rule heads
│
├── tokenizer/
│ ├── slang_serializer.py # Pure-Python SLaNg AST serializer/deserializer
│ └── vocab.json # SLaNg vocabulary mappings
│
├── requirements.txt # Production-only/local dev dependencies (Starlette, Uvicorn)
├── requirements-neural.txt # Optional dependencies for neural weights (PyTorch, Numpy)
├── vercel.json # Vercel configuration for Python serverless builds
└── README.md
For development, testing, and standard Vercel deployments, install the lightweight dependencies:
pip install -r requirements.txtRun the local development server:
uvicorn api.app:app --reload --port 8000If you want to run neural inference locally with model weights, install the neural requirements:
pip install -r requirements-neural.txtPlace your trained model weight checkpoint (best.pt) in one of the priority locations:
checkpoints/final/best.ptcheckpoints/sft/best.ptcheckpoints/pretrain/best.pt- Or set the
MODEL_PATHenvironment variable pointing to your.ptfile.
Returns active solver configuration and health status. Response:
{
"status": "ok",
"solver_mode": "fallback",
"solver_loaded": true,
"checkpoint_error": "No checkpoint found..."
}Solves a calculus operation on the given SLaNg AST expression. Request Body:
{
"input": {
"op": "diff",
"var": "x",
"expr": {
"numi": {
"terms": [{"coeff": 3, "var": {"x": 2}}]
},
"deno": 1
}
}
}Response:
{
"status": "solved",
"expr": {
"numi": {
"terms": [{"coeff": 6, "var": {"x": 1}}]
},
"deno": 1
},
"steps": [
{
"rule": "power_rule",
"description": "Differentiated with respect to x using the power rule.",
"before": "3x^{2}",
"after": "6x"
}
],
"latex": "6x",
"confidence": 1.0,
"verified": true,
"warning": "Fallback mode — no neural checkpoint loaded.",
"rule": "power_rule",
"mode": "fallback"
}Validates that the provided SLaNg AST node or structure can be serialized. Request Body:
{
"expression": {
"numi": {
"terms": [{"coeff": 3, "var": {"x": 2}}]
},
"deno": 1
}
}Response:
{
"valid": true
}Deploying the CalculusSolver API to Vercel is simple. You can use the Vercel CLI:
vercelOr connect your GitHub repository containing this codebase directly to your Vercel dashboard. Vercel will automatically read vercel.json, build the serverless functions using @vercel/python, and expose the endpoints.