Multimodal Math Mentor

A 13-node LangGraph tutor for JEE maths: multimodal input, hybrid retrieval, sandboxed SymPy and three human checkpoints.

LLM / RAG2026All work

Fig. 01Math Mentor on Hugging Face Spaces, multimodal input with the agent pipeline.
Math Mentor on Hugging Face Spaces, multimodal input with the agent pipeline.

What it is

What it does

A tutoring agent that takes maths problems as text, photo or voice and walks students to JEE-level solutions. The interesting part isn't the solving, it's the supervision around it: a LangGraph state machine routes every problem through extraction, guardrails, retrieval, symbolic computation, verification and explanation, pausing for a human at exactly the moments the system doesn't trust itself.

Why it exists

I watched LLMs confidently botch 3-step JEE maths. In a classroom that costs trust. I didn't want a bigger model. I wanted a checker, a calculator that can't lie, and a system that knows when to stop and ask a human.

13
Graph nodes
3
Human checkpoints
25
Eval problems

Repo Amit-kr26/Multimodal_Math_MentorDemo huggingface.co/spaces/Amit-kr26/Multimodal_Math_MentorStack LangGraph · Gradio · FAISS · BM25 · EasyOCR · Whisper · SymPy

Runs on a free Hugging Face Space, so it may need to restart on first load, which can take a minute or two.

Build9decisions

How it is built

13-node graph

agents/graph.py wires extract → guardrail → parse → route → retrieve_context/retrieve_memory → solve → verify → explain → save_memory, branching on the extraction review, the guardrail, the parser and the verifier, and compiled with MemorySaver so threads resume mid-solution.

Three human checkpoints

interrupt_before fires on hitl_extraction / hitl_clarification / hitl_verification, triggered when OCR or ASR confidence < 0.6, parsing stays ambiguous, or the verifier scores < 0.7.

Retry with feedback

The verifier can bounce the work back to the solver up to twice, passing its issues and suggestion straight into the next solver prompt.

Hybrid retrieval with RRF

FAISS (all-MiniLM-L6-v2) and BM25Okapi each return 10 candidates over 20 markdown knowledge-base docs (chunked 500/50 along headings), fused with reciprocal rank fusion k=60 down to the top 5.

Sandboxed SymPy

COMPUTE: lines emitted by the solver run in an isolated subprocess (timeout 10s) that replies with JSON {result, latex, error}. The verifier's checks go through the same box.

Voice that speaks maths

Whisper base on CPU post-processes phrases like 'square root of' → sqrt() and Greek names → symbols, balancing parentheses before parsing.

Guardrail against injection

A dedicated agent rejects role-play and prompt-injection attempts, including attacks smuggled in through OCR'd images, base64 or leetspeak obfuscation.

Self-learning memory

Problem and solution pairs append to JSONL; retrieval is cosine similarity over a cached embedding matrix (top 3, minimum similarity 0.3), rebuilt only when the record count changes.

Eval harness

eval/run_eval.py pushes 25 curated problems (algebra 7, calculus 7, probability 6, linear algebra 5) through the whole graph and emits timestamped JSON + Markdown reports with per-topic accuracy, confidence and timing.

How the pieces connect

Input and safety Checkpoints Retrieve, solve, verify Tools, review and memory Text · OCR · Whisper confidence scored Extractor text + confidence Guardrail reject ends it Parser structured problem Router topic, strategy, tools HITL extract low confidence Checkpointer MemorySaver HITL clarify needs clarity Hybrid RAG FAISS + BM25, RRF Memory recall cosine, top 3 Solver context + memory Verifier notes feed the retry Explainer step-by-step Web search DuckDuckGo, top 3 SymPy restricted subprocess Plotter matplotlib PNG HITL verify not confident Memory JSONL appended not correct, retry HITL extract HITL clarify HITL verify