# tomsibu/tor-ai architecture

> Architecture diagram of the GitHub repository tomsibu/tor-ai, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-10). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/tomsibu/tor-ai
- Repository: https://github.com/tomsibu/tor-ai

## Overview

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: [`backend/app/routes`](https://github.com/tomsibu/tor-ai/tree/main/backend/app/routes) (API routes), [`frontend/src/pages`](https://github.com/tomsibu/tor-ai/tree/main/frontend/src/pages) (Role pages), [`frontend/src/lib/api.ts`](https://github.com/tomsibu/tor-ai/blob/main/frontend/src/lib/api.ts) (API client), [`backend/app/routes/content.py`](https://github.com/tomsibu/tor-ai/blob/main/backend/app/routes/content.py) (Content API)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_backend["Backend"]
  node_backend_main["App entry<br/>fastapi entry<br/>[main.py]"]
  node_backend_routes["API routes<br/>http routers"]
  node_backend_user_route["User API<br/>auth http<br/>[user.py]"]
  node_backend_classroom_route["Classroom API<br/>domain http<br/>[classroom.py]"]
  node_backend_session_route["Session API<br/>domain http<br/>[session.py]"]
  node_backend_content_route["Content API<br/>ingest http<br/>[content.py]"]
  node_backend_ai_route["AI API<br/>teaching http<br/>[ai.py]"]
  node_backend_voice_route["Voice API<br/>audio http<br/>[voice.py]"]
  node_backend_models[("Domain models<br/>orm models")]
  node_backend_schemas["Schemas<br/>pydantic contracts"]
  node_backend_db[("DB layer<br/>sqlite/sqlalchemy")]
  node_backend_services["Services<br/>workflow layer"]
  node_backend_ai_service["AI service<br/>llm orchestration<br/>[ai_service.py]"]
  node_backend_teaching_service["Teaching service<br/>session workflow"]
  node_backend_voice_services["Speech pipeline<br/>speech/audio<br/>[speech_service.py]"]
  node_backend_pdf_service["PDF service<br/>document ingestion<br/>[pdf_service.py]"]
  node_backend_auth_utils["Auth utils<br/>jwt/security"]
end

subgraph group_frontend["Frontend"]
  node_frontend_app["SPA entry<br/>react app<br/>[App.tsx]"]
  node_frontend_auth["Auth context<br/>state provider<br/>[AuthContext.tsx]"]
  node_frontend_api["API client<br/>http client<br/>[api.ts]"]
  node_frontend_routes["Role pages<br/>route pages"]
  node_frontend_layout["Dashboard shell<br/>ui shell"]
  node_frontend_protected["Protected route<br/>route guard<br/>[ProtectedRoute.tsx]"]
  node_frontend_admin["Admin UI<br/>dashboard section"]
  node_frontend_teacher["Teacher UI<br/>dashboard section"]
  node_frontend_classroom["Classroom UI<br/>dashboard section"]
end

subgraph group_storage["Storage & integrations"]
  node_backend_storage[("Local storage<br/>filesystem/db<br/>[ai_tutor.db]")]
  node_backend_external_ai{{"External AI<br/>third-party llm"}}
  node_backend_external_speech{{"Speech APIs<br/>third-party speech"}}
  node_backend_piper["Piper TTS<br/>local tts"]
end

node_backend_main -->|"mounts"| node_backend_routes
node_backend_main -->|"initializes"| node_backend_db
node_backend_routes -->|"includes"| node_backend_user_route
node_backend_routes -->|"includes"| node_backend_classroom_route
node_backend_routes -->|"includes"| node_backend_session_route
node_backend_routes -->|"includes"| node_backend_content_route
node_backend_routes -->|"includes"| node_backend_ai_route
node_backend_routes -->|"includes"| node_backend_voice_route
node_backend_user_route -->|"uses"| node_backend_auth_utils
node_backend_classroom_route -->|"reads/writes"| node_backend_models
node_backend_session_route -->|"reads/writes"| node_backend_models
node_backend_content_route -->|"uses"| node_backend_pdf_service
node_backend_content_route -->|"stores"| node_backend_storage
node_backend_ai_route -->|"orchestrates"| node_backend_teaching_service
node_backend_teaching_service -->|"calls"| node_backend_ai_service
node_backend_ai_service -->|"calls"| node_backend_external_ai
node_backend_voice_route -->|"uses"| node_backend_voice_services
node_backend_voice_services -->|"calls"| node_backend_external_speech
node_backend_voice_services -->|"writes"| node_backend_piper
node_backend_services -->|"reads/writes"| node_backend_models
node_backend_services -->|"reads/writes"| node_backend_storage
node_backend_models -->|"mapped by"| node_backend_db
node_frontend_app -->|"uses"| node_frontend_layout
node_frontend_app -->|"guards"| node_frontend_protected
node_frontend_app -->|"renders"| node_frontend_routes
node_frontend_routes -->|"includes"| node_frontend_admin
node_frontend_routes -->|"includes"| node_frontend_teacher
node_frontend_routes -->|"includes"| node_frontend_classroom
node_frontend_auth -->|"attaches token"| node_frontend_api
node_frontend_routes -->|"uses"| node_frontend_auth
node_frontend_admin -->|"fetches data"| node_frontend_api
node_frontend_teacher -->|"fetches data"| node_frontend_api
node_frontend_classroom -->|"fetches data"| node_frontend_api
node_frontend_api -->|"HTTP/JSON"| node_backend_main
node_backend_voice_route -.->|"integrates"| node_backend_external_speech
node_backend_content_route -->|"persists"| node_backend_storage
node_backend_ai_route -->|"reads state"| node_backend_storage

click node_backend_main "https://github.com/tomsibu/tor-ai/blob/main/backend/app/main.py"
click node_backend_routes "https://github.com/tomsibu/tor-ai/tree/main/backend/app/routes"
click node_backend_user_route "https://github.com/tomsibu/tor-ai/blob/main/backend/app/routes/user.py"
click node_backend_classroom_route "https://github.com/tomsibu/tor-ai/blob/main/backend/app/routes/classroom.py"
click node_backend_session_route "https://github.com/tomsibu/tor-ai/blob/main/backend/app/routes/session.py"
click node_backend_content_route "https://github.com/tomsibu/tor-ai/blob/main/backend/app/routes/content.py"
click node_backend_ai_route "https://github.com/tomsibu/tor-ai/blob/main/backend/app/routes/ai.py"
click node_backend_voice_route "https://github.com/tomsibu/tor-ai/blob/main/backend/app/routes/voice.py"
click node_backend_models "https://github.com/tomsibu/tor-ai/tree/main/backend/app/models"
click node_backend_schemas "https://github.com/tomsibu/tor-ai/tree/main/backend/app/schemas"
click node_backend_db "https://github.com/tomsibu/tor-ai/tree/main/backend/app/db"
click node_backend_services "https://github.com/tomsibu/tor-ai/tree/main/backend/app/services"
click node_backend_ai_service "https://github.com/tomsibu/tor-ai/blob/main/backend/app/services/ai_service.py"
click node_backend_teaching_service "https://github.com/tomsibu/tor-ai/blob/main/backend/app/services/teaching_service.py"
click node_backend_voice_services "https://github.com/tomsibu/tor-ai/blob/main/backend/app/services/speech_service.py"
click node_backend_pdf_service "https://github.com/tomsibu/tor-ai/blob/main/backend/app/services/pdf_service.py"
click node_backend_auth_utils "https://github.com/tomsibu/tor-ai/tree/main/backend/app/utils"
click node_backend_storage "https://github.com/tomsibu/tor-ai/blob/main/backend/ai_tutor.db"
click node_backend_piper "https://github.com/tomsibu/tor-ai/tree/main/backend/audio"
click node_frontend_app "https://github.com/tomsibu/tor-ai/blob/main/frontend/src/App.tsx"
click node_frontend_auth "https://github.com/tomsibu/tor-ai/blob/main/frontend/src/contexts/AuthContext.tsx"
click node_frontend_api "https://github.com/tomsibu/tor-ai/blob/main/frontend/src/lib/api.ts"
click node_frontend_routes "https://github.com/tomsibu/tor-ai/tree/main/frontend/src/pages"
click node_frontend_layout "https://github.com/tomsibu/tor-ai/blob/main/frontend/src/components/DashboardLayout.tsx"
click node_frontend_protected "https://github.com/tomsibu/tor-ai/blob/main/frontend/src/components/ProtectedRoute.tsx"
click node_frontend_admin "https://github.com/tomsibu/tor-ai/tree/main/frontend/src/pages/admin"
click node_frontend_teacher "https://github.com/tomsibu/tor-ai/tree/main/frontend/src/pages/teacher"
click node_frontend_classroom "https://github.com/tomsibu/tor-ai/tree/main/frontend/src/pages/classroom"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_backend_main,node_backend_routes,node_backend_user_route,node_backend_classroom_route,node_backend_session_route,node_backend_content_route,node_backend_ai_route,node_backend_voice_route,node_backend_models,node_backend_schemas,node_backend_db,node_backend_services,node_backend_ai_service,node_backend_teaching_service,node_backend_voice_services,node_backend_pdf_service,node_backend_auth_utils toneBlue
class node_frontend_app,node_frontend_auth,node_frontend_api,node_frontend_routes,node_frontend_layout,node_frontend_protected,node_frontend_admin,node_frontend_teacher,node_frontend_classroom toneAmber
class node_backend_storage,node_backend_external_ai,node_backend_external_speech,node_backend_piper toneMint
```

## Components

### Backend

FastAPI API, persistence, and service orchestration.

- [App entry](https://github.com/tomsibu/tor-ai/blob/main/backend/app/main.py): fastapi entry · Wires middleware, config, and API routers. · `backend/app/main.py`
- [API routes](https://github.com/tomsibu/tor-ai/tree/main/backend/app/routes): http routers · Feature endpoints for users, classrooms, students, sessions, content, attendance, AI, and voice. · `backend/app/routes`
- [User API](https://github.com/tomsibu/tor-ai/blob/main/backend/app/routes/user.py): auth http · Login, registration, and user access flows. · `backend/app/routes/user.py`
- [Classroom API](https://github.com/tomsibu/tor-ai/blob/main/backend/app/routes/classroom.py): domain http · Classroom management and teacher-classroom assignment flows. · `backend/app/routes/classroom.py`
- [Session API](https://github.com/tomsibu/tor-ai/blob/main/backend/app/routes/session.py): domain http · Teaching session lifecycle and state access. · `backend/app/routes/session.py`
- [Content API](https://github.com/tomsibu/tor-ai/blob/main/backend/app/routes/content.py): ingest http · PDF/content upload and retrieval endpoints. · `backend/app/routes/content.py`
- [AI API](https://github.com/tomsibu/tor-ai/blob/main/backend/app/routes/ai.py): teaching http · Teaching session start, question, and continuation endpoints. · `backend/app/routes/ai.py`
- [Voice API](https://github.com/tomsibu/tor-ai/blob/main/backend/app/routes/voice.py): audio http · Audio question handling and speech-enabled Q&A. · `backend/app/routes/voice.py`
- [Domain models](https://github.com/tomsibu/tor-ai/tree/main/backend/app/models): orm models · SQLAlchemy entities for users, classrooms, students, sessions, attendance, content, and assignments. · `backend/app/models`
- [Schemas](https://github.com/tomsibu/tor-ai/tree/main/backend/app/schemas): pydantic contracts · Request and response models separating transport from persistence. · `backend/app/schemas`
- [DB layer](https://github.com/tomsibu/tor-ai/tree/main/backend/app/db): sqlite/sqlalchemy · Engine, session, and base metadata setup. · `backend/app/db`
- [Services](https://github.com/tomsibu/tor-ai/tree/main/backend/app/services): workflow layer · AI, teaching, PDF extraction, speech, audio, and attendance workflows. · `backend/app/services`
- [AI service](https://github.com/tomsibu/tor-ai/blob/main/backend/app/services/ai_service.py): llm orchestration · Selects Gemini with Mistral fallback and manages teaching prompts. · `backend/app/services/ai_service.py`
- [Teaching service](https://github.com/tomsibu/tor-ai/blob/main/backend/app/services/teaching_service.py): session workflow · Coordinates teaching-session behavior and progression. · `backend/app/services/teaching_service.py`
- [Speech pipeline](https://github.com/tomsibu/tor-ai/blob/main/backend/app/services/speech_service.py): speech/audio · Transcription and TTS generation for voice Q&A. · `backend/app/services/speech_service.py`
- [PDF service](https://github.com/tomsibu/tor-ai/blob/main/backend/app/services/pdf_service.py): document ingestion · Extracts uploaded PDFs for downstream teaching and Q&A. · `backend/app/services/pdf_service.py`
- [Auth utils](https://github.com/tomsibu/tor-ai/tree/main/backend/app/utils): jwt/security · JWT issuance, verification, and password security helpers. · `backend/app/utils`

### Frontend

React/Vite dashboard SPA and shared client state.

- [SPA entry](https://github.com/tomsibu/tor-ai/blob/main/frontend/src/App.tsx): react app · Bootstraps routing, layout, and global providers. · `frontend/src/App.tsx`
- [Auth context](https://github.com/tomsibu/tor-ai/blob/main/frontend/src/contexts/AuthContext.tsx): state provider · Holds login state and token-driven session info. · `frontend/src/contexts/AuthContext.tsx`
- [API client](https://github.com/tomsibu/tor-ai/blob/main/frontend/src/lib/api.ts): http client · Central Axios-style boundary for backend calls. · `frontend/src/lib/api.ts`
- [Role pages](https://github.com/tomsibu/tor-ai/tree/main/frontend/src/pages): route pages · Login, register, and role-specific dashboards for admin, teacher, and classroom users. · `frontend/src/pages`
- [Dashboard shell](https://github.com/tomsibu/tor-ai/blob/main/frontend/src/components/DashboardLayout.tsx): ui shell · Shared sidebar and protected app layout. · `frontend/src/components/DashboardLayout.tsx`
- [Protected route](https://github.com/tomsibu/tor-ai/blob/main/frontend/src/components/ProtectedRoute.tsx): route guard · Enforces authenticated access in the SPA. · `frontend/src/components/ProtectedRoute.tsx`
- [Admin UI](https://github.com/tomsibu/tor-ai/tree/main/frontend/src/pages/admin): dashboard section · Administration views for users, classrooms, sessions, and analytics. · `frontend/src/pages/admin`
- [Teacher UI](https://github.com/tomsibu/tor-ai/tree/main/frontend/src/pages/teacher): dashboard section · Teacher-facing classroom and session management. · `frontend/src/pages/teacher`
- [Classroom UI](https://github.com/tomsibu/tor-ai/tree/main/frontend/src/pages/classroom): dashboard section · Classroom attendance, sessions, and teaching dashboards. · `frontend/src/pages/classroom`

### Storage & integrations

Database, local files, and external AI/speech services.

- [Local storage](https://github.com/tomsibu/tor-ai/blob/main/backend/ai_tutor.db): filesystem/db · SQLite database, uploaded files, and generated audio assets. · `backend/ai_tutor.db`
- **External AI**: third-party llm · Gemini primary model with Mistral fallback.
- **Speech APIs**: third-party speech · External speech transcription used by the voice flow.
- [Piper TTS](https://github.com/tomsibu/tor-ai/tree/main/backend/audio): local tts · Local text-to-speech generation into backend/audio. · `backend/audio`

## Connections

- 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.
