MathTutor / README.md
rsreenivasa's picture
AI MVP
e46d575
|
Raw History Blame Contribute Delete
6.04 kB

๐Ÿงฎ MathMind AI โ€” MVP

AI-powered adaptive math tutor for Grades 6โ€“12.
Built with Streamlit ยท FastAPI ยท Anthropic Claude ยท Supabase.


Architecture

Streamlit (localhost:8501)
    โ””โ”€โ”€ httpx calls โ†’ FastAPI (localhost:8000)
                          โ”œโ”€โ”€ Anthropic Claude API  (AI explanations + tests)
                          โ””โ”€โ”€ Supabase              (Auth + PostgreSQL)

Quick Start

1. Clone & Install

git clone <repo-url>
cd mathmind
pip install -r requirements.txt

2. Configure Environment

cp .env.example .env
# Fill in the values in .env (see Environment Variables section below)

3. Set Up Supabase

  1. Create a free project at supabase.com
  2. Go to SQL Editor and run the scripts in order:
    • database/01_schema.sql
    • database/02_rls.sql
    • database/03_seed.sql
  3. Copy your project URL and keys from Settings โ†’ API

4. Run the Backend

# From the mathmind/ root directory
uvicorn backend.main:app --reload --port 8000

Verify: open http://localhost:8000/api/health
You should see: {"status": "ok"}

5. Run the Frontend

# In a second terminal, from the mathmind/ root directory
streamlit run streamlit_app/app.py

Open http://localhost:8501 in your browser.


Environment Variables

Create a .env file in the mathmind/ root:

# Anthropic โ€” get your key at console.anthropic.com
ANTHROPIC_API_KEY=sk-ant-...

# Supabase โ€” from your project: Settings โ†’ API
SUPABASE_URL=https://xxxx.supabase.co
SUPABASE_ANON_KEY=eyJ...          # "anon" key (public)
SUPABASE_SERVICE_ROLE_KEY=eyJ...  # "service_role" key (secret โ€” never expose to browser)

# FastAPI
ALLOWED_ORIGINS=http://localhost:8501
API_BASE_URL=http://localhost:8000

Project Structure

mathmind/
โ”œโ”€โ”€ .env.example
โ”œโ”€โ”€ README.md
โ”œโ”€โ”€ requirements.txt
โ”‚
โ”œโ”€โ”€ streamlit_app/               # Frontend (Streamlit)
โ”‚   โ”œโ”€โ”€ app.py                   # Landing page + router
โ”‚   โ”œโ”€โ”€ pages/
โ”‚   โ”‚   โ”œโ”€โ”€ 1_Login.py           # Auth (login + signup)
โ”‚   โ”‚   โ”œโ”€โ”€ 2_Tutor.py           # Core learning flow
โ”‚   โ”‚   โ””โ”€โ”€ 3_Dashboard.py      # Badges + streak + history
โ”‚   โ””โ”€โ”€ utils/
โ”‚       โ”œโ”€โ”€ auth.py              # Supabase auth helpers
โ”‚       โ”œโ”€โ”€ api_client.py        # FastAPI HTTP client
โ”‚       โ””โ”€โ”€ state.py             # session_state helpers
โ”‚
โ”œโ”€โ”€ backend/                     # API (FastAPI)
โ”‚   โ”œโ”€โ”€ main.py                  # App entry + CORS
โ”‚   โ”œโ”€โ”€ core/
โ”‚   โ”‚   โ”œโ”€โ”€ config.py            # Env var loading
โ”‚   โ”‚   โ””โ”€โ”€ auth.py              # JWT verification
โ”‚   โ”œโ”€โ”€ routers/
โ”‚   โ”‚   โ”œโ”€โ”€ explain.py           # POST /api/explain
โ”‚   โ”‚   โ”œโ”€โ”€ practice.py          # POST /api/practice
โ”‚   โ”‚   โ”œโ”€โ”€ test.py              # POST /api/test/*
โ”‚   โ”‚   โ””โ”€โ”€ badges.py            # GET/POST /api/badges
โ”‚   โ”œโ”€โ”€ services/
โ”‚   โ”‚   โ”œโ”€โ”€ claude_service.py    # Anthropic SDK wrapper
โ”‚   โ”‚   โ”œโ”€โ”€ supabase_service.py  # Supabase client wrapper
โ”‚   โ”‚   โ””โ”€โ”€ prompt_templates.py  # Grade-level prompts
โ”‚   โ””โ”€โ”€ models/                  # Pydantic request/response models
โ”‚
โ””โ”€โ”€ database/
    โ”œโ”€โ”€ 01_schema.sql            # Table definitions
    โ”œโ”€โ”€ 02_rls.sql               # Row Level Security
    โ”œโ”€โ”€ 03_seed.sql              # Badge definitions
    โ””โ”€โ”€ README.md

API Endpoints

Method Endpoint Description Auth
GET /api/health Health check None
POST /api/explain Stream grade-appropriate explanation JWT
POST /api/practice Generate 3 practice problems JWT
POST /api/test/generate Generate 5-question mini-test JWT
POST /api/test/evaluate Evaluate answers + save result JWT
GET /api/badges Get student's earned badges JWT
POST /api/badges/check Check + award new badges JWT

Interactive docs: http://localhost:8000/docs


Student Flow

Sign Up โ†’ Select Grade โ†’ Ask Question
    โ†’ Streamed Explanation
        โ†’ Practice Problems (3x)
            โ†’ Mini-Test (5 questions)
                โ”œโ”€โ”€ PASS (โ‰ฅ80%) โ†’ Badge Awarded โ†’ Dashboard
                โ””โ”€โ”€ FAIL (<80%) โ†’ See Feedback โ†’ Retry Test

Badge System

Badge Emoji Condition
First Try Champion ๐Ÿ† Pass your first test
Getting Started ๐Ÿš€ Complete 5 sessions
Math Explorer ๐ŸŒŸ Complete 10 sessions
On Fire ๐Ÿ”ฅ 5-day streak
Perfect Score ๐Ÿ’ฏ Score 100% on a test

Tech Stack

Layer Technology
Frontend Streamlit 1.35+
Backend FastAPI + Uvicorn
AI Anthropic Claude claude-sonnet-4-6
Database Supabase (PostgreSQL + Auth + RLS)
HTTP Client httpx (async-capable)
Validation Pydantic v2

Development Notes

  • Backend auto-reloads on file changes with --reload
  • Streamlit auto-reloads on file saves
  • JWT flow: Supabase issues JWT on login โ†’ stored in st.session_state โ†’ sent as Authorization: Bearer <token> to FastAPI โ†’ FastAPI verifies via Supabase auth API
  • Streaming: /api/explain uses FastAPI StreamingResponse + Anthropic streaming SDK โ†’ Streamlit renders chunks live
  • Service role key is used by FastAPI only โ€” bypasses RLS for server-side writes. Never expose to the browser.

Phase 2 Roadmap (not in MVP)

  • Re-engagement email nudges (SendGrid)
  • LMS integration (Google Classroom, Canvas)
  • Parent weekly report emails
  • Adaptive ML difficulty engine
  • Subscription/payment (Stripe)
  • Mobile app