Clarity is an AI-powered study platform that combines a Next.js application, a MongoDB data layer, and a Python-based CRAG service to support tutoring, notes, flashcards, quizzes, study analytics, and content-driven learning spaces.
The codebase is now organized so that frontend code, backend runtime code, and ML training assets each have clear ownership:
- Next.js app and API routes live at the root application level.
- Python CRAG runtime lives under
services/backend. - Model training code and fine-tuned assets live under
services/ml.
- AI tutoring with contextual answers based on uploaded learning content
- Learning spaces for grouping notes, files, and study materials
- Notes, summaries, flashcards, and quizzes generated from content
- Study timers and analytics for progress tracking
- YouTube and document analysis workflows
- A local CRAG backend for generation and optional standalone retrieval endpoints
Clarity is split into three main layers:
-
app/andcomponents/The Next.js 16 frontend and route handlers. This is the main product surface. -
lib/Shared TypeScript logic such as database access, AI client configuration, service orchestration, and the TypeScript CRAG graph used by the app. -
services/Python services and ML artifacts.services/backend/contains the Flask runtime that powers local CRAG endpoints.services/ml/contains training utilities and fine-tuned T5 model assets.
At runtime, the frontend uses TypeScript retrieval and orchestration logic from lib/crag/, then sends generation requests to the Flask service configured through CRAG_FLASK_URL.
- A user asks a question in the Next.js app.
- The app resolves user context, space context, and uploaded content.
lib/crag/retrieves and grades relevant content.- The app calls the Flask service at
services/backend/app.pyfor local generation. - The generated answer is optionally polished by the configured AI provider and returned to the UI.
- Next.js 16
- React 19
- TypeScript
- Tailwind CSS
- Radix UI
- Framer Motion
- React PDF
- KaTeX
- Next.js Route Handlers under
app/api/ - MongoDB with Mongoose
- Flask for local CRAG endpoints
- Ollama for local generation
- Gemini, Groq, and Euri provider support through the TypeScript AI client
- LangChain and LangGraph for orchestration
- Sentence Transformers and FAISS for legacy Python retrieval
- T5 fine-tuning utilities under
services/ml/
crag/
├── app/ # Next.js App Router pages and API route handlers
├── components/ # UI primitives and feature-specific React components
├── data/ # Local datasets and optional legacy corpora
├── lib/ # Shared TS services, agents, models, utils, CRAG graph
├── middleware.ts # Next.js auth middleware entrypoint
├── public/ # Static assets
├── services/
│ ├── backend/
│ │ ├── app.py # Flask entrypoint for CRAG endpoints
│ │ ├── requirements.txt # Python runtime dependencies for backend service
│ │ └── crag/
│ │ ├── corrector.py
│ │ ├── generator.py
│ │ └── retriever.py
│ └── ml/
│ ├── hf_auth.py # Hugging Face token helper
│ ├── requirements.txt # Python dependencies for training
│ ├── train.py # T5 fine-tuning entrypoint
│ └── models/
│ └── T5_FineTuned/ # Fine-tuned model artifacts
├── .env.example # Environment variable template
├── next.config.mjs
├── package.json
└── tsconfig.json
Contains the Next.js App Router structure.
app/(app)/contains the authenticated app experience.app/auth/contains auth screens.app/api/contains Next.js route handlers for product APIs.
Contains UI and feature components.
components/ui/for reusable UI primitivescomponents/chat-components/for tutor/chat UIcomponents/notes-components/for notes workflowscomponents/spaces-components/for learning space featurescomponents/layout/for application shell components
Contains the shared TypeScript application layer.
lib/db.tsfor MongoDB connection logiclib/models/for Mongoose modelslib/services/for business logiclib/agents/for AI agents and orchestrationlib/crag/for the TypeScript CRAG graph used by the frontend
Contains the Python Flask backend used for local CRAG generation and optional standalone endpoints:
POST /api/queryPOST /api/retrievePOST /api/correctPOST /api/generatePOST /api/flashcards
Contains training code and model artifacts.
train.pyfine-tunes the local T5 modelmodels/T5_FineTuned/stores model weights and tokenizer files
Copy .env.example to .env and fill in the values you need.
MONGODB_URI=
JWT_SECRET=
NEXT_PUBLIC_GOOGLE_CLIENT_ID=
NEXT_PUBLIC_SITE_URL=http://localhost:3000
CRAG_FLASK_URL=http://localhost:5001AI_MODEL=gemini-2.5-flash-lite
AI_VISION_MODEL=gemini-2.5-flash-liteCLOUDINARY_CLOUD_NAME=
CLOUDINARY_API_KEY=
CLOUDINARY_API_SECRET=
UPLOADTHING_TOKEN=OLLAMA_HOST=http://localhost:11434
OLLAMA_MODEL=qwen2.5:3b
TAVILY_API_KEY=Before running the project locally, make sure you have:
- Node.js 18 or newer
- npm
- Python 3.10 or newer
- MongoDB running locally or a hosted MongoDB connection string
- Ollama installed if you want to run local CRAG generation
npm installpython3 -m venv .venv
source .venv/bin/activatepip install -r services/backend/requirements.txtpip install -r services/ml/requirements.txtcp .env.example .envUpdate .env with your MongoDB URI, auth secrets, and any AI provider keys you want to use.
Runs the Next.js app and Flask backend together.
npm run devThis starts:
npm run dev:webnpm run dev:backend
npm run dev:webnpm run flaskOr directly:
python3 services/backend/app.pynpm run build
npm startnpm run lintThe T5 training entrypoint is now:
npm run train:modelOr directly:
python3 services/ml/train.py- Optional custom QA file:
data/custom_qa.jsonl - Fine-tuned model output:
services/ml/models/T5_FineTuned/
data/custom_qa.jsonlis optional.- If the file is absent, the training script falls back to SQuAD examples.
- Training dependencies are intentionally isolated from the backend runtime dependencies.
The main application APIs live under app/api/ and include domains such as:
app/api/auth/app/api/chat/app/api/content/app/api/conversations/app/api/dashboard/app/api/flashcards/app/api/learning/app/api/notes/app/api/podcasts/app/api/quizzes/app/api/settings/app/api/spaces/app/api/study-timer/app/api/youtube/
The Python backend exposes local endpoints for standalone CRAG behavior:
POST /api/query
POST /api/retrieve
POST /api/correct
POST /api/generate
POST /api/flashcards
The current application primarily retrieves content from MongoDB-backed user content through the TypeScript CRAG pipeline.
The Python retriever also supports a legacy local corpus file:
data/notes.txt
If that file is missing, the backend starts cleanly and retrieval endpoints simply return no legacy local documents. This is intentional so the main application flow remains usable even without the old file-based corpus.
The auth redirect middleware now uses the standard Next.js entrypoint:
middleware.ts
This keeps auth behavior aligned with Next.js conventions and avoids the older non-standard proxy.ts entrypoint name.
- Keep React UI work inside
app/,components/, andlib/. - Keep Python backend runtime logic inside
services/backend/. - Keep training utilities and model artifacts inside
services/ml/. - Keep route handlers thin and move business logic into
lib/services/. - Prefer
@/imports for frontend and shared TypeScript code.
If the browser still shows old UI after code changes:
rm -rf .next
npm run devThen hard refresh the browser.
Make sure backend dependencies are installed:
pip install -r services/backend/requirements.txtInstall the ML dependencies separately:
pip install -r services/ml/requirements.txtCheck that:
- Ollama is installed and running
OLLAMA_HOSTpoints to the right serverOLLAMA_MODELis available locallyCRAG_FLASK_URLmatches the Flask service address
This is expected if data/notes.txt is not present. The main app still uses the TypeScript and MongoDB-backed retrieval pipeline.
When contributing:
- Keep structural ownership clear.
- Update documentation when moving files or changing runtime commands.
- Avoid mixing Python service code into the Next.js root again.
- Prefer feature-specific folders over unrelated root-level files.
- Validate frontend and backend changes independently before combining them.
The current structure is intentionally conservative. Good follow-up improvements would be:
- Consolidate scattered component files into
components/shared/and feature-specific folders. - Add dedicated backend and ML test commands.
- Introduce Docker support for the Flask and Ollama stack.
- Add typed API contracts for key
app/api/routes.
Clarity now has a cleaner separation between:
- product UI and route handlers
- shared TypeScript logic
- Python CRAG runtime
- model training assets
That separation makes the project easier to onboard onto, easier to extend, and easier to deploy in a more professional way.