diff --git a/.dockerignore b/.dockerignore index e0b835e143b11b50d1370bc7da8b75ebb156e93e..469ff3d93f275020e7451ce3880f42c8aae00e0a 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,5 +1,27 @@ +# Only backend/, requirements.txt and the Dockerfile are needed to build the backend image. +.git +.github +.gitattributes +.vscode +.idea +.DS_Store +.env +.env.* +venv/ +.venv/ +frontend/ node_modules dist +tests/ +**/__pycache__ +*.pyc +*.pyo +.pytest_cache/ +.mypy_cache/ +.ruff_cache/ +.coverage +htmlcov/ +chroma_storage/ +user_notes/ *.log -.env -.git \ No newline at end of file +*.md diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml deleted file mode 100644 index bcd2b9df9dd86736ca61216e847f78dfeef2268f..0000000000000000000000000000000000000000 --- a/.github/workflows/deploy.yml +++ /dev/null @@ -1,52 +0,0 @@ -name: Deploy to GitHub Pages - -on: - push: - branches: [main] - -permissions: - contents: read - pages: write - id-token: write - -concurrency: - group: "pages" - cancel-in-progress: false - -jobs: - build-and-deploy: - runs-on: ubuntu-latest - environment: github-pages - steps: - - name: Checkout - uses: actions/checkout@v4 - - - name: Setup Node - uses: actions/setup-node@v4 - with: - node-version: "20" - - - name: Install and build - working-directory: frontend/my-app - env: - VITE_API_URL: ${{ secrets.VITE_API_URL }} - run: | - if [ -z "$VITE_API_URL" ]; then - echo "VITE_API_URL secret is missing. Add your backend URL (e.g. https://diptidhawade-NoteScanner.hf.space)." >&2 - exit 1 - fi - npm ci - npm run build - - - name: Upload artifact - uses: actions/upload-pages-artifact@v3 - with: - path: frontend/my-app/dist - - deploy: - runs-on: ubuntu-latest - needs: build-and-deploy - environment: github-pages - steps: - - name: Deploy to GitHub Pages - uses: actions/deploy-pages@v4 diff --git a/.gitignore b/.gitignore deleted file mode 100644 index b81d1871ac736d0dbef5578eaba0f587344cdb21..0000000000000000000000000000000000000000 --- a/.gitignore +++ /dev/null @@ -1,48 +0,0 @@ -# Python -__pycache__/ -*.pyc -*.pyo -*.pyd - -# Virtual Environments -venv/ -.venv/ - -# ChromaDB (legacy local storage; app uses Docker Chroma server) -chroma_storage/ - -# User data -user_notes/ - -# Node -node_modules/ -package-lock.json - -# System/Editor -.DS_Store -Thumbs.db -.vscode/ -.idea/ -.env - -# Machine Learning binaries -*.pth -*.pt -*.h5 -*.ckpt -*.joblib -*.pkl -*.npy -*.npz -*.dylib -*.so -*.onnx - -# Logs -*.log - -# ML output folders -runs/ -output/ -checkpoints/ -*.tensorboard/ \ No newline at end of file diff --git a/DEPLOYMENT.md b/DEPLOYMENT.md index c3cb7de2d42b3e621e8d1bcb3a519e72a94f2e01..ec9761144d8777396dcee6a8b0e4caa6cf185678 100644 --- a/DEPLOYMENT.md +++ b/DEPLOYMENT.md @@ -8,9 +8,47 @@ This project is set up for: ## 1) Deploy backend (Hugging Face Space) 1. Open your Space: `diptidhawade/NoteScanner`. -2. In **Files**, upload this repository content (or push from git). -3. Ensure the Space uses the root `Dockerfile` (already configured to run on `${PORT}` with default `7860`). -4. In Space **Settings -> Variables and secrets**, add: +2. Ensure the Space uses the root `Dockerfile` (already configured to run on `${PORT}` with default `7860`). +3. In Space **Settings -> Variables and secrets**, add the settings below. +4. Deploy the code, either automatically (recommended) or by hand: + +### Automatic backend deploy (GitHub Actions) + +The workflow `.github/workflows/deploy-backend.yml` pushes the backend to the Space on every push to `main` +(or when you run it by hand from the **Actions** tab). + +1. On Hugging Face, create a **write** access token: **Settings -> Access Tokens -> New token** (type: Write). +2. In your GitHub repo, go to **Settings -> Secrets and variables -> Actions -> New repository secret** and add: + - `HF_TOKEN` = that token +3. Push to `main`. Without `HF_TOKEN` the workflow skips the deploy and shows a notice instead of failing. + +What it sends to the Space: `backend/`, `Dockerfile`, `.dockerignore`, `requirements.txt`, `DEPLOYMENT.md` and +`README.md`. The frontend isn't needed there (it lives on GitHub Pages), and no binary files are sent +(Spaces reject them unless they use Git LFS). + +### Space README front matter + +Hugging Face reads the Space's settings from YAML at the very top of its `README.md`: + +```yaml +--- +title: NoteScanner +emoji: "πŸ“š" +colorFrom: indigo +colorTo: blue +sdk: docker +app_port: 7860 +pinned: false +--- +``` + +The GitHub `README.md` doesn't carry this block. The deploy workflow adds it to the Space's copy +automatically. If you upload files to the Space by hand, keep this block at the top of the Space's `README.md`. + +### Manual deploy (only if you don't use the workflow) + +In the Space's **Files** tab, upload the same files the workflow sends (`backend/`, `Dockerfile`, +`.dockerignore`, `requirements.txt`, and `README.md` with the front matter above). ### Required Space secrets @@ -24,6 +62,46 @@ This project is set up for: - `SARVAM_DOC_INTEL_LANGUAGE` (optional) - `SARVAM_DOC_INTEL_TIMEOUT` (optional) +### Security settings + +- `CORS_ORIGINS` (only needed if your frontend is not at `https://cherie-dips.github.io`): + the website(s) allowed to call the backend, comma-separated, e.g. `https://.github.io` +- Optional: `SESSION_TTL_DAYS`, `MAX_UPLOAD_MB`, `MAX_CHAT_UPLOAD_CHARS`, `CHAT_CACHE_TTL_SECONDS` +- Request limits (overall per address/browser, logins, uploads, questions, study tools, daily AI caps) + are the `LIMIT_*` values in `backend/settings.py`; see "Request limits" in the README. To loosen or + tighten all of them without a code change, set `RATE_LIMIT_MULTIPLIER` (e.g. `2` doubles every limit). + +### Password-reset emails (optional) + +"Forgot password" emails are only turned on when both `SMTP_HOST` and `SMTP_FROM` are set. +Without them, the app explains that password reset by email isn't available. + +- `FRONTEND_URL`: the address people open the app at, used to build the reset link, + e.g. `https://cherie-dips.github.io/NoteScanner/` +- `SMTP_HOST`: your email provider's SMTP server, e.g. `smtp.gmail.com` +- `SMTP_PORT`: usually `587` +- `SMTP_USERNAME` and `SMTP_PASSWORD`: the login for that server (for Gmail, an app password) +- `SMTP_FROM`: the sender address shown to users, e.g. `NoteScanner ` +- `SMTP_STARTTLS`: `true` (default) for port 587 + +The same email settings send **daily review reminders** to students who turn them on in Account +settings: `REMINDER_HOUR` (default `8`, in `APP_TIMEZONE`, default `Asia/Kolkata`) is the hour after +which the day's emails go out; `REMINDERS_ENABLED=false` turns the feature off. + +### Running a pilot (optional settings) + +- `ADMIN_EMAILS`: comma-separated emails that can open "Usage & feedback" in the profile menu + (active students, questions, uploads, pages read, reviews, ratings, feedback, estimated cost). +- `ALERT_WEBHOOK_URL`: a Slack or Discord incoming-webhook address. Server crashes, failed uploads, + AI outages and failed reminder emails are posted there (each kind at most once per 10 minutes). +- AI cost estimate: `AI_PRICE_PER_PAGE` (Sarvam Vision, per page), `AI_PRICE_PER_1K_INPUT_TOKENS`, + `AI_PRICE_PER_1K_OUTPUT_TOKENS` and `AI_PRICE_CURRENCY` (default `INR`). Copy the prices from + your Sarvam plan; until they're set the admin page shows usage counts but no cost. +- `AI_MONTHLY_BUDGET`: once this month's estimated cost reaches it, reading files, answers and study + tools pause until next month (notes and flashcard reviews keep working). `0` (default) = no limit. + Needs the prices above. +- `STARTER_NOTES=false`: don't give new accounts the "Getting started" folder. + ### Chroma (required for persistent note features) Use one option: @@ -39,6 +117,7 @@ Use one option: 1. Wait for Space build to finish. 2. Copy backend URL, e.g. `https://diptidhawade-NoteScanner.hf.space` +3. Check it's up: open `https://diptidhawade-NoteScanner.hf.space/health` (should show `{"status":"ok"}`). ## 2) Configure GitHub Pages frontend @@ -51,7 +130,17 @@ Use one option: 2. GitHub Pages URL will be: - `https://.github.io/NoteScanner/` -## 3) Verify +The Pages workflow installs exact versions from `frontend/my-app/package-lock.json` (`npm ci`) and runs the +code checker (ESLint) before building, so commit the lockfile whenever you add or update packages. + +## 3) Checks on every push (CI) + +`.github/workflows/ci.yml` runs on every push to `main` and on pull requests: + +- Backend: installs the pinned packages and runs the tests in `tests/` (no AI service or database needed). +- Frontend: runs ESLint and a production build. + +## 4) Verify 1. Open frontend URL. 2. Confirm these work: @@ -60,7 +149,166 @@ Use one option: - explorer upload - study generation +## 5) Shared course library and SDE-Prep (Study AI + Ask AI) + +[SDE-Prep](https://cherie-dips.github.io/SDE-Prep/) uses this backend in two ways: + +- **Study AI tab**: this frontend inside SDE-Prep (`/NoteScanner/?embed=sde`), in SDE-Prep's colours. + Both sites are on `cherie-dips.github.io`, so they share the sign-in. +- **Ask AI** in SDE-Prep's Notes tab: questions, flashcards, quizzes and summaries about the course PDFs + shown there. Those PDFs are read **once** on the server into the shared course library + (`backend/library.py`) and searched by every student; nothing is copied per student. + +`https://cherie-dips.github.io` is already allowed by the default `CORS_ORIGINS`. + +### Index the course PDFs (once) + +From your computer, with `.env` pointing at Chroma Cloud and holding `SARVAM_API_KEY`: + +```bash +python scripts/index_library.py --dry-run # lists the PDFs and how many pieces need OCR, and the cost +python scripts/index_library.py # reads and indexes them +``` + +Handwritten and scanned pages are read with Sarvam Vision (β‰ˆ β‚Ή0.5 per piece at Sarvam's listed price; +`--dry-run` prints the total first). Typed pages are read for free. Without a Sarvam key the script uses +Tesseract instead (free, much weaker on handwriting). Running it again only reads new or changed PDFs. + +Or from the server, as an admin (`ADMIN_EMAILS`): `POST /admin/library/sync` starts a sync in the +background, and `GET /admin/library` shows each PDF's status. + +### Keep it up to date + +- After adding PDFs to the Supabase bucket, run the script again (or the admin sync), or +- set `LIBRARY_SYNC_HOURS` (e.g. `24`) on the Space to check the bucket automatically. + +Library OCR counts towards `AI_MONTHLY_BUDGET`; when the budget is used up, PDFs that need OCR wait for +the next sync (typed PDFs are still indexed). + +### Settings (all optional) + +| Setting | Default | What it does | +|---|---|---| +| `LIBRARY_ENABLED` | `true` | Turn the shared library (and Ask AI's course search) on or off | +| `LIBRARY_SUPABASE_URL`, `LIBRARY_SUPABASE_KEY`, `LIBRARY_BUCKET` | SDE-Prep's bucket | Where the PDFs are (the key is the public, read-only anon key) | +| `LIBRARY_PREFIXES` | `plaksha-university` | Bucket folders to index (comma-separated) | +| `LIBRARY_OCR` | `auto` | `auto` (Sarvam if a key is set, else Tesseract), `sarvam`, `tesseract` or `off` | +| `LIBRARY_SYNC_HOURS` | `0` | Re-check the bucket every N hours (`0` = only when started by hand) | + +### Verify + +1. `GET /library/status` lists the indexed PDFs. +2. In SDE-Prep, open Notes β†’ a course PDF β†’ **Ask AI**: sign in, ask a question, click a source + (the PDF scrolls to that spot). +3. Open the **Study AI** tab: you are already signed in, and a deck saved from Ask AI is under Review. + ## Notes -- Backend on free HF Space may sleep when idle (cold start delay on first request). -- If frontend shows backend/network errors, first check `VITE_API_URL` secret and Space build logs. \ No newline at end of file +- Backend on free HF Space may sleep when idle (cold start delay on first request). The search model is + downloaded while the image is built, so waking up doesn't download it again. +- The backend must run as a single process (one uvicorn worker, as the `Dockerfile` does): the chat-upload + cache, request limits and per-user locks live in that process's memory. +- If frontend shows backend/network errors, first check `VITE_API_URL` secret and Space build logs. +- If the browser console shows a CORS error, add your frontend's address to `CORS_ORIGINS`. +- After this update everyone has to sign in once more (older logins had no expiry date and are now treated as expired). + +## Local development with Docker Compose + +`docker compose up --build` starts a local Chroma server, the backend on `http://localhost:8000` and the +frontend dev server on `http://localhost:5173`. Local Chroma data is kept in the `chroma_data` volume +(mounted at `/data`, where Chroma 1.x stores it). + +## Backups and restore + +Chroma Cloud holds the only copy of every student's account, notes, folders and flashcard decks. +Regular backups, and checking that they can be restored, are how this project protects against +losing that data (a bad deploy, an accidental delete, or a problem with the Chroma account). + +### Weekly automatic backup + +`.github/workflows/backup.yml` runs every Sunday at 03:00 India time (and on demand from the +Actions tab β†’ "Weekly backup" β†’ Run workflow). Add these repository secrets (Settings β†’ Secrets and +variables β†’ Actions β†’ Secrets): + +- `CHROMA_API_KEY`, `CHROMA_TENANT`, `CHROMA_DATABASE`: the same values as on the Space. +- `BACKUP_PASSPHRASE`: a long random passphrase. **Store it somewhere safe outside GitHub** (a + password manager): without it the backups can't be opened. + +Until all four are set, the workflow skips with a notice. Each run saves one encrypted file as a +workflow artifact (kept 90 days). The backup holds password hashes and students' notes, so it's +encrypted before upload: artifacts of a public repository can be downloaded by others. + +### Back up by hand + +```bash +python scripts/backup_chroma.py # writes notescanner-backup-.jsonl.gz +python scripts/backup_chroma.py --out my-backup.jsonl.gz +``` + +It reads the Chroma settings from `.env` (or the environment). Login sessions, password-reset links +and OneNote sign-in tokens are left out on purpose: after a restore, students sign in again and +reconnect OneNote. Keep backup files private. + +### Open an encrypted backup + +Download the artifact from the workflow run (it's a zip containing the `.gpg` file), then: + +```bash +gpg --decrypt notescanner-backup-