# xvalentim/neroedu-gemma3n architecture

> Architecture diagram of the GitHub repository xvalentim/neroedu-gemma3n, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-10-09). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/xvalentim/neroedu-gemma3n
- Repository: https://github.com/xvalentim/neroedu-gemma3n

## Overview

##### Project Type and Purpose
- NeroEdu is a full-stack, cross-platform AI application for offline exam preparation. It provides three AI agents (essay grading, flashcard generation, mock exam creation) powered by a local Gemma 3n LLM (via Ollama) and TF-IDF retrieval. Multiple clients (Electron desktop, iOS app, Python scripts/notebooks) consume a FastAPI backend that orchestrates the AI flows.

##### File Structure Analysis
- Top-level directories:
- agent/  – LangGraph API store (vector indexes, pickles)
- app/    – FastAPI backend (main.py, routers, internal modules, database layer)
- mobile-ios/ – SwiftUI mobile client (Views, ViewModels, Services)
- neroedu-desktop/ – Electron + React desktop client (bundled FastAPI backend & Ollama via NSIS)
- neroedu-lp/ – Next.js landing page (static marketing site)
- data/, vectorstore/ – Exam corpora, TF-IDF CSVs and pickled models
- storage/ – Local JSON database for user data and settings
- notebooks/ & src/ – Research scripts and CLI utilities

##### README Insights
- Architecture is offline-first, uses:
- FastAPI (backend API & orchestration)
- Gemma 3n via Ollama as a local LLM runtime
- TF-IDF + cosine similarity for document retrieval
- Electron for desktop packaging, NSIS installer
- SwiftUI + local services for iOS

a. Main Components
- Clients:
- Desktop App (Electron + React)
- iOS App (SwiftUI)
- CLI/Notebooks (Python scripts)
- Marketing Site (Next.js)
- Backend API: FastAPI service with modular routers (essay, flashcards, exams)
- AI Agent Orchestrator: LangChain-style flows in app/internal
- Vector Store: TF-IDF indexes (CSV + .pkl files in vectorstore/)
- LLM Runtime: Ollama server hosting Gemma 3n (local container/process)
- Data Storage: JSON DB (storage/database.json) or SQLite abstraction (app/database.py)
- OCR Engine (mobile-iOS service) for scanning paper essays

b. Relationships & Interactions
- Clients → HTTP/IPC calls → FastAPI endpoints
- FastAPI → TF-IDF retrieval module → fetch relevant passages
- FastAPI → prompt builder → sends structured prompts → Ollama/Gemma 3n → LLM responses
- FastAPI → post-processing (parse JSON, score mapping) → returns to client
- Mobile iOS: camera input → OCRService → text → LLMService or backend API
- Electron desktop bundles and launches local FastAPI + Ollama; uses IPC or HTTP internally

c. Architectural Patterns & Principles
- Layered architecture: presentation (clients), API layer, orchestration layer, data layer
- Modular services: one router per AI agent
- “Lite RAG”: retrieval (TF-IDF) + generation (LLM)
- Offline-first: all components run locally, no cloud dependencies
- Bundling strategy: NSIS packs Python backend + Ollama with desktop app

d. Key Technologies & Frameworks
- Backend: Python 3.10+, FastAPI, LangChain, Ollama CLI/HTTP, TF-IDF (scikit-learn or custom)
- Desktop: Electron, React, TypeScript, NSIS installer
- Mobile: SwiftUI, Combine/ViewModels, custom OCRService & LLMService wrappers
- Landing: Next.js, TailwindCSS
- Storage: JSON file or SQLite via SQLAlchemy

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Clients
    subgraph "Clients"
        DesktopClient["Electron Desktop App"]:::client
        iOSClient["iOS Mobile App"]:::client
        CLI["Notebooks & CLI Tools"]:::client
        LandingPage["Landing Page (Next.js)"]:::client
    end

    %% Backend API Layer
    subgraph "FastAPI Backend" 
        FastAPI["FastAPI Backend"]:::backend
        subgraph "Routers"
            CreditsRouter["Credits Router"]:::backend
            ItemsRouter["Items Router"]:::backend
            ServicesRouter["Services Router"]:::backend
            UsersRouter["Users Router"]:::backend
            SchemasRouter["Schemas Module"]:::backend
        end
        Orchestrator["AI Agent Orchestrator"]:::backend
        DatabaseLayer["Database Layer"]:::data
    end

    %% Data and AI Engine
    subgraph "AI & Data Stores"
        TFIDF["TF-IDF Vector Store"]:::data
        AgentStore["Agent Store (LangGraph)"]:::data
        JSONDB["Local JSON Database"]:::data
        DS_Vector["Desktop Vectorstore"]:::data
        DS_JSON["Desktop JSON DB"]:::data
        Ollama["Ollama/Gemma 3n LLM"]:::external
    end

    %% iOS internal services
    subgraph "iOS Services"
        OCR["OCR Service"]:::backend
        LLMService["LLMService"]:::backend
    end

    %% Interactions
    DesktopClient -->|"HTTP/IPC (JSON)"| FastAPI
    DesktopClient -.->|"bundles"| Ollama
    DesktopClient -.->|"bundles"| TFIDF
    iOSClient -->|"HTTP/JSON"| FastAPI
    iOSClient -->|"OCR text"| OCR
    OCR -->|"text"| LLMService
    LLMService -->|"HTTP/JSON"| FastAPI
    CLI -->|"Direct module calls"| Orchestrator
    LandingPage -->|"HTTP/REST"| FastAPI

    FastAPI -->|"calls routers"| CreditsRouter
    FastAPI -->|"calls routers"| ItemsRouter
    FastAPI -->|"calls routers"| ServicesRouter
    FastAPI -->|"calls routers"| UsersRouter
    FastAPI -->|"uses schemas"| SchemasRouter

    FastAPI -->|"SQLAlchemy/JSON"| DatabaseLayer
    FastAPI -->|"pickle load"| TFIDF
    FastAPI -->|"invoke orchestration"| Orchestrator

    Orchestrator -->|"build prompt"| Ollama
    Orchestrator -->|"retrieve docs"| TFIDF
    Orchestrator -->|"store vectors"| AgentStore

    Ollama -->|"LLM response"| Orchestrator
    Orchestrator -->|"response JSON"| FastAPI

    %% Click Events
    click FastAPI "https://github.com/xvalentim/neroedu-gemma3n/blob/main/app/main.py"
    click CreditsRouter "https://github.com/xvalentim/neroedu-gemma3n/blob/main/app/routers/credits.py"
    click ItemsRouter "https://github.com/xvalentim/neroedu-gemma3n/blob/main/app/routers/items.py"
    click ServicesRouter "https://github.com/xvalentim/neroedu-gemma3n/blob/main/app/routers/services.py"
    click UsersRouter "https://github.com/xvalentim/neroedu-gemma3n/blob/main/app/routers/users.py"
    click SchemasRouter "https://github.com/xvalentim/neroedu-gemma3n/blob/main/app/routers/schemas.py"
    click Orchestrator "https://github.com/xvalentim/neroedu-gemma3n/tree/main/app/internal/"
    click DatabaseLayer "https://github.com/xvalentim/neroedu-gemma3n/blob/main/app/database.py"
    click TFIDF "https://github.com/xvalentim/neroedu-gemma3n/tree/main/vectorstore/"
    click AgentStore "https://github.com/xvalentim/neroedu-gemma3n/blob/main/agent/.langgraph_api/store.pckl"
    click JSONDB "https://github.com/xvalentim/neroedu-gemma3n/blob/main/storage/database.json"
    click DS_Vector "https://github.com/xvalentim/neroedu-gemma3n/tree/main/neroedu-desktop/vectorstore/"
    click DS_JSON "https://github.com/xvalentim/neroedu-gemma3n/blob/main/neroedu-desktop/storage/database.json"
    click DesktopClient "https://github.com/xvalentim/neroedu-gemma3n/tree/main/neroedu-desktop/src/main/"
    click iOSClient "https://github.com/xvalentim/neroedu-gemma3n/tree/main/mobile-ios/NeroEdu/Views/"
    click OCR "https://github.com/xvalentim/neroedu-gemma3n/blob/main/mobile-ios/NeroEdu/Services/OCRService.swift"
    click LLMService "https://github.com/xvalentim/neroedu-gemma3n/blob/main/mobile-ios/NeroEdu/Services/LLMService.swift"
    click LandingPage "https://github.com/xvalentim/neroedu-gemma3n/tree/main/neroedu-lp/src/app/"
    click CLI "https://github.com/xvalentim/neroedu-gemma3n/tree/main/notebooks/"

    %% Styles
    classDef client fill:#c3dafe,stroke:#2b6cb0,color:#2c5282;
    classDef backend fill:#c6f6d5,stroke:#38a169,color:#276749;
    classDef data fill:#fed7d7,stroke:#c53030,color:#742a2a;
    classDef external fill:#e2e8f0,stroke:#4a5568,color:#2d3748;
```
