How tomsibu/tor-ai works
1. This is a two-tier application: a FastAPI backend in backend/app and a React/Vite/TypeScript frontend in frontend/src. The main architectural boundary is HTTP/JSON between the UI and the API, with the backend also doing file I/O and calls to external AI/speech services.
2. The backend entrypoint is backend/app/main.py, which wires together the API routers and the app-wide middleware/config. From there, the application is organized by feature routes rather than by UI page: user, classroom, student, attendance, content, session, ai, and voice.
3. Persistence is centered on SQLite (backend/ai_tutor.db) with SQLAlchemy models under backend/app/models and a shared database/session layer under backend/app/db. The model set shows the core domain: users, classrooms, students, sessions, attendance, session state, content, and teacher-classroom assignments.
4. Request/response contracts are separated into Pydantic schemas under backend/app/schemas. This indicates a typical API boundary where routes accept validated input schemas and return shaped output schemas, keeping the ORM models and transport models distinct.
5. The backend service layer is where the nontrivial behavior lives. ai_service.py, teaching_service.py, teaching_formatter.py, pdf_service.py, speech_service.py, audio_service.py, and attendance_service.py suggest that routes mostly orchestrate domain workflows while services handle AI prompting, PDF extraction, speech-to-text, text-to-speech, teaching content formatting, and attendance logic.
6. The AI teaching flow is a distinct subsystem. The README indicates Gemini is the primary model and Mistral is the fallback; the routes under /ai/* support starting a teaching session, asking questions, and continuing a session. Session-specific state is modeled separately, implying the system tracks conversation/teaching progression per session rather than treating AI chat as stateless.
7. The voice flow is another integration boundary. The /voice/ask-voice endpoint and the presence of speech_service.py plus audio_service.py indicate a pipeline that accepts audio input, transcribes it using an external speech API, then synthesizes audio output locally through Piper TTS into the backend audio/ directory.
8. Content ingestion is file-based and PDF-centric. pdf_service.py plus the content route and the README’s upload path imply that uploaded PDFs are extracted on the backend, stored locally under backend/uploads/, and then used as source material for teaching and Q&A workflows.
9. Authentication and authorization are built around JWT. The utils/jwt.py and utils/security.py modules, plus the README’s role model, indicate token-based login with role checks across admin, teacher, and classroom users. This is enforced on the backend and mirrored in the frontend with protected routes and token attachment.
10. The frontend is a dashboard-style SPA with route-based sections for different roles: pages/admin, pages/teacher, and pages/classroom. Shared UI shells live in components like DashboardLayout, AppSidebar, ProtectedRoute, and classroom-specific management panels, which suggests the main UX boundary is “role dashboard” rather than a single generic app shell.
11. The frontend data layer is thin and centralized. src/lib/api.ts is the likely Axios client boundary, while AuthContext.tsx handles authentication state. This means pages and components mostly consume API data through shared client/context layers instead of calling backend endpoints ad hoc.
12. External and local infrastructure is intentionally simple: SQLite for durable app data, the filesystem for uploads/audio, and third-party AI services for cognition and transcription. There is no evidence of a separate queue, cache, or message bus; request handling appears synchronous and API-driven end to end.
13. For diagramming, the most important end-to-end paths are: login/register → JWT issuance → protected dashboard access; classroom/content upload → backend storage/extraction → teaching session generation; and voice question → speech transcription → AI answer → TTS audio output. These flows connect the main subsystems of the repo.
Main entry points: routes API routes · pages Role pages · api.ts API client · content.py Content API
Components 30
Backend
FastAPI API, persistence, and service orchestration.
- App entry · Wires middleware, config, and API routers.
backend/app/main.py - API routes · Feature endpoints for users, classrooms, students, sessions, content, attendance, AI, and voice.
backend/app/routes - User API · Login, registration, and user access flows.
backend/app/routes/user.py - Classroom API · Classroom management and teacher-classroom assignment flows.
backend/app/routes/classroom.py - Session API · Teaching session lifecycle and state access.
backend/app/routes/session.py - Content API · PDF/content upload and retrieval endpoints.
backend/app/routes/content.py - AI API · Teaching session start, question, and continuation endpoints.
backend/app/routes/ai.py - Voice API · Audio question handling and speech-enabled Q&A.
backend/app/routes/voice.py - Domain models · SQLAlchemy entities for users, classrooms, students, sessions, attendance, content, and assignments.
backend/app/models - Schemas · Request and response models separating transport from persistence.
backend/app/schemas - DB layer · Engine, session, and base metadata setup.
backend/app/db - Services · AI, teaching, PDF extraction, speech, audio, and attendance workflows.
backend/app/services - AI service · Selects Gemini with Mistral fallback and manages teaching prompts.
backend/app/services/ai_service.py - Teaching service · Coordinates teaching-session behavior and progression.
backend/app/services/teaching_service.py - Speech pipeline · Transcription and TTS generation for voice Q&A.
backend/app/services/speech_service.py - PDF service · Extracts uploaded PDFs for downstream teaching and Q&A.
backend/app/services/pdf_service.py - Auth utils · JWT issuance, verification, and password security helpers.
backend/app/utils
Frontend
React/Vite dashboard SPA and shared client state.
- SPA entry · Bootstraps routing, layout, and global providers.
frontend/src/App.tsx - Auth context · Holds login state and token-driven session info.
frontend/src/contexts/AuthContext.tsx - API client · Central Axios-style boundary for backend calls.
frontend/src/lib/api.ts - Role pages · Login, register, and role-specific dashboards for admin, teacher, and classroom users.
frontend/src/pages - Dashboard shell · Shared sidebar and protected app layout.
frontend/src/components/DashboardLayout.tsx - Protected route · Enforces authenticated access in the SPA.
frontend/src/components/ProtectedRoute.tsx - Admin UI · Administration views for users, classrooms, sessions, and analytics.
frontend/src/pages/admin - Teacher UI · Teacher-facing classroom and session management.
frontend/src/pages/teacher - Classroom UI · Classroom attendance, sessions, and teaching dashboards.
frontend/src/pages/classroom
Storage & integrations
Database, local files, and external AI/speech services.
- Local storage · SQLite database, uploaded files, and generated audio assets.
backend/ai_tutor.db - External AI · Gemini primary model with Mistral fallback.
- Speech APIs · External speech transcription used by the voice flow.
- Piper TTS · Local text-to-speech generation into backend/audio.
backend/audio
Connections 37
- App entry → API routes: mounts · Registers the feature routers on startup.
- App entry → DB layer: initializes · Connects app startup to the shared database layer.
- API routes → User API: includes · User and auth endpoints.
- API routes → Classroom API: includes · Classroom management endpoints.
- API routes → Session API: includes · Teaching session endpoints.
- API routes → Content API: includes · Content upload and retrieval endpoints.
- API routes → AI API: includes · AI teaching endpoints.
- API routes → Voice API: includes · Voice question and answer endpoints.
- User API → Auth utils: uses · JWT and password security helpers.
- Classroom API → Domain models: reads/writes · Persists classrooms and teacher-classroom assignments.
- Session API → Domain models: reads/writes · Persists sessions and session state.
- Content API → PDF service: uses · Extracts uploaded PDFs for teaching content.
- Content API → Local storage: stores · Writes uploads to local filesystem and persists metadata.
- AI API → Teaching service: orchestrates · Coordinates teaching-session workflows.
- Teaching service → AI service: calls · Delegates model prompting and response generation.
- AI service → External AI: calls · Uses Gemini first, then Mistral fallback.
- Voice API → Speech pipeline: uses · Transcription and audio generation pipeline.
- Speech pipeline → Speech APIs: calls · Speech-to-text service integration.
- Speech pipeline → Piper TTS: writes · Generates TTS audio files locally.
- Services → Domain models: reads/writes · Business workflows operate on domain entities.
- Services → Local storage: reads/writes · Uses SQLite and filesystem assets.
- Domain models → DB layer: mapped by · ORM models share the common database base.
- SPA entry → Dashboard shell: uses · Wraps app pages in the dashboard shell.
- SPA entry → Protected route: guards · Restricts dashboard routes to authenticated users.
- SPA entry → Role pages: renders · Hosts login, register, and role dashboards.
- Role pages → Admin UI: includes · Admin-facing sections.
- Role pages → Teacher UI: includes · Teacher-facing sections.
- Role pages → Classroom UI: includes · Classroom-facing sections.
- Auth context → API client: attaches token · Provides authenticated request context.
- Role pages → Auth context: uses · Reads login state for navigation and access.
- Admin UI → API client: fetches data · Loads administrative entities through the shared API client.
- Teacher UI → API client: fetches data · Loads teacher workflow data through the shared API client.
- Classroom UI → API client: fetches data · Loads classroom workflow data through the shared API client.
- API client → App entry: HTTP/JSON · Frontend talks to the FastAPI backend over the API boundary.
- Voice API → Speech APIs: integrates · Voice endpoint depends on transcription service.
- Content API → Local storage: persists · Uploaded content is stored locally before extraction.
- AI API → Local storage: reads state · AI teaching flow uses persisted session/content context.