File size: 6,038 Bytes
e46d575
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# ๐Ÿงฎ 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

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

### 2. Configure Environment

```bash
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](https://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

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

Verify: open [http://localhost:8000/api/health](http://localhost:8000/api/health)  
You should see: `{"status": "ok"}`

### 5. Run the Frontend

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

Open [http://localhost:8501](http://localhost:8501) in your browser.

---

## Environment Variables

Create a `.env` file in the `mathmind/` root:

```bash
# 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](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