# yukasama/txt2vec architecture

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

- Interactive diagram: https://gitdiagram.com/yukasama/txt2vec
- Repository: https://github.com/yukasama/txt2vec

## Overview

##### Project Type and Purpose
- This is a single-service, full-stack Python backend application (a text-embedding microservice).
- It exposes REST endpoints for uploading datasets, generating embeddings, managing and retrieving datasets, and interfacing with external storage (local files or GitHub).
- The core features include: dataset ingestion & validation, AI-model inference (embeddings), data persistence (SQLite via SQLAlchemy/aiosqlite), and file-upload management.

##### File Structure Analysis
- Top level:
- deploy/compose.yaml – Docker-Compose configuration
- Dockerfile – container build
- docs/ – PlantUML source for ER, API, use-cases
- src/txt2vec/ – application code
- scripts/ – utility and load-testing scripts (locust)
- test_data/ and tests/ – test vectors and unit tests
- Under src/txt2vec:
- app.py & server.py – application factory and startup (uvicorn via `uv run app`)
- config/ – configuration loading (app.toml), DB, logging, security, seeding
- common/ – shared models, exceptions, status codes
- ai_model/ – model definitions, repository layer for embeddings, helper utils
- datasets/ – models, repository, service, router, validation & file-handling utils
- upload/ – router + schemas + services (local vs GitHub backends)
- synthesis/ – domain models for embedding results
- utils/ – file sanitization

##### Key Insights from README
- Uses Astral’s “uv” wrapper (likely uvicorn + FastAPI)
- SQLite via SQLAlchemy/aiosqlite
- Locust for load testing (scripts/locust.py)
- CI/CD via GitHub Actions, Docker multi-stage build

##### Main Components
a. Clients & Interfaces
- External clients (e.g., CLI clients, web front ends, scripts)
- Locust load-tester as a special client
b. API Layer
- Uvicorn/“uv” server
- FastAPI routers:
- /datasets – ingestion, validation, metadata
- /upload – file upload (local / GitHub)
- /embeddings or /synthesis – embedding generation endpoints
c. Service Layer
- DatasetService – orchestration of file readers, mappers, normalizers
- UploadService – local vs GitHub backends behind a strategy interface
- AIModelService – loading model from repository, exposing embed(text)
d. Persistence Layer
- SQLAlchemy ORM (aiosqlite)
- Entities: Dataset, EmbeddingResult, UploadRecord, etc.
- config/seed for initial data
e. External Systems
- GitHub API (for remote file upload)
- Local file system storage (uploads)
f. Utilities & Cross-cutting
- Logging (config/logger.py)
- Security (config/security.py)
- Error handling (error_handler.py, common/exceptions)

##### Interactions and Data Flows
- Client → HTTP request → Uvicorn/FastAPI server
- Server dispatches to corresponding Router
- Router calls Service → Service uses Repository → Repository interacts with DB or FS or External API
- AIModelService retrieves or caches a model, runs inference, returns embeddings
- Results persisted to DB and returned to client

##### Architectural Patterns & Principles
- Layered Architecture: Presentation (routers) → Business Logic (services) → Data Access (repositories)
- Strategy Pattern in upload services (local vs GitHub)
- Dependency Injection via config/app factory
- Modularization by domain (ai_model, datasets, upload, synthesis)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Clients Layer
    subgraph "Clients" 
        direction TB
        CLI["CLI Client"]:::external
        WebUI["Web Frontend"]:::external
        Locust["Locust Load Tester"]:::external
    end

    %% Application Layer
    subgraph "txt2vec Application" 
        direction TB

        %% Configuration & Initialization
        subgraph "Initialization & Configuration"
            direction TB
            AppFactory["app.py\n(Application Factory)"]:::config
            ServerEP["server.py / __main__.py\n(Uvicorn Entry)"]:::config
            ConfigLoad["config.py\n(Configuration DI)"]:::config
            DBConfig["db.py\n(SQLite/SQLAlchemy)"]:::config
            LoggerCfg["logger.py\n(Logging)"]:::config
            SecurityCfg["security.py\n(Security)"]:::config
            SeedData["seed.py & app.toml\n(Seeding)"]:::config
        end

        %% Common & Error Handling
        subgraph "Common & Exceptions"
            direction TB
            Models["models.py\n(Shared Models)"]:::common
            Status["status.py\n(Status Codes)"]:::common
            Exceptions["exceptions.py & error_handler.py\n(Global Exceptions)"]:::common
        end

        %% API Layer
        subgraph "API Layer" 
            direction TB
            DatasetRouter["router.py\n(/datasets)"]:::api
            UploadRouter["router.py\n(/upload)"]:::api
            SynthesisRouter["router.py\n(/embeddings)"]:::api
        end

        %% Service Layer
        subgraph "Service Layer"
            direction TB
            DatasetService["service.py\n(DatasetService)"]:::service
            UploadOrchestrator["model_service.py\n(UploadStrategy)"]:::service
            LocalUpload["local_service.py\n(Local Backend)"]:::service
            GitHubUpload["github_service.py\n(GitHub Backend)"]:::service
            AIModelService["repository.py\n(AIModelService)"]:::service
        end

        %% Repository Layer
        subgraph "Repository & Persistence"
            direction TB
            DatasetRepo["repository.py\n(Dataset Repo)"]:::repo
            AIModelRepo["repository.py\n(AIModel Repo)"]:::repo
        end

        %% Utilities
        subgraph "Utilities"
            direction TB
            FileUtils["file_sanitizer.py\n(File Sanitizer)"]:::util
        end
    end

    %% Persistence & External Systems
    subgraph "Persistence & External"
        direction TB
        SQLiteDB["SQLite\n(aiosqlite)"]:::db
        FileSystem["Local File System"]:::external
        GitHubAPI["GitHub API"]:::external
    end

    %% Flow Connections
    CLI -->|"HTTP Request"| ServerEP
    WebUI -->|"HTTP Request"| ServerEP
    Locust -->|"HTTP Request"| ServerEP

    ServerEP -->|"initialize()"| AppFactory
    AppFactory --> ConfigLoad
    AppFactory --> DBConfig
    AppFactory --> LoggerCfg
    AppFactory --> SecurityCfg
    AppFactory --> SeedData

    ServerEP --> DatasetRouter
    ServerEP --> UploadRouter
    ServerEP --> SynthesisRouter

    DatasetRouter -->|"POST /datasets"| DatasetService
    UploadRouter -->|"POST /upload"| UploadOrchestrator
    SynthesisRouter -->|"POST /embeddings"| AIModelService

    DatasetService --> DatasetRepo
    DatasetService --> FileUtils

    UploadOrchestrator --> LocalUpload
    UploadOrchestrator --> GitHubUpload
    LocalUpload --> FileSystem
    GitHubUpload --> GitHubAPI

    AIModelService --> AIModelRepo
    AIModelService --> FileUtils

    DatasetRepo --> SQLiteDB
    AIModelRepo --> SQLiteDB

    %% Click Events
    click AppFactory "https://github.com/yukasama/txt2vec/blob/main/src/txt2vec/app.py"
    click ServerEP "https://github.com/yukasama/txt2vec/blob/main/src/txt2vec/server.py"
    click ServerEP "https://github.com/yukasama/txt2vec/blob/main/src/txt2vec/__main__.py"
    click ConfigLoad "https://github.com/yukasama/txt2vec/blob/main/src/txt2vec/config/config.py"
    click DBConfig "https://github.com/yukasama/txt2vec/blob/main/src/txt2vec/config/db.py"
    click LoggerCfg "https://github.com/yukasama/txt2vec/blob/main/src/txt2vec/config/logger.py"
    click SecurityCfg "https://github.com/yukasama/txt2vec/blob/main/src/txt2vec/config/security.py"
    click SeedData "https://github.com/yukasama/txt2vec/blob/main/src/txt2vec/config/seed.py"
    click Models "https://github.com/yukasama/txt2vec/blob/main/src/txt2vec/common/models.py"
    click Status "https://github.com/yukasama/txt2vec/blob/main/src/txt2vec/common/status.py"
    click Exceptions "https://github.com/yukasama/txt2vec/blob/main/src/txt2vec/common/exceptions.py"
    click Exceptions "https://github.com/yukasama/txt2vec/blob/main/src/txt2vec/error_handler.py"
    click DatasetRouter "https://github.com/yukasama/txt2vec/blob/main/src/txt2vec/datasets/router.py"
    click DatasetService "https://github.com/yukasama/txt2vec/blob/main/src/txt2vec/datasets/service.py"
    click DatasetRepo "https://github.com/yukasama/txt2vec/blob/main/src/txt2vec/datasets/repository.py"
    click FileUtils "https://github.com/yukasama/txt2vec/blob/main/src/txt2vec/utils/file_sanitizer.py"
    click UploadOrchestrator "https://github.com/yukasama/txt2vec/blob/main/src/txt2vec/upload/model_service.py"
    click LocalUpload "https://github.com/yukasama/txt2vec/blob/main/src/txt2vec/upload/local_service.py"
    click GitHubUpload "https://github.com/yukasama/txt2vec/blob/main/src/txt2vec/upload/github_service.py"
    click AIModelService "https://github.com/yukasama/txt2vec/blob/main/src/txt2vec/ai_model/repository.py"

    %% Styles
    classDef api fill:#cce5ff,stroke:#3399ff,color:#004085;
    classDef service fill:#d4edda,stroke:#28a745,color:#155724,stroke-dasharray: 5 2;
    classDef repo fill:#fff3cd,stroke:#ffce54,color:#856404;
    classDef external fill:#f8d7da,stroke:#dc3545,color:#721c24;
    classDef db fill:#e2e3e5,stroke:#6c757d,color:#383d41,stroke-dasharray: 2 2;
    classDef util fill:#d1ecf1,stroke:#17a2b8,color:#0c5460;
    classDef config fill:#f5c6cb,stroke:#d63384,color:#581845;
    classDef common fill:#e2e3e5,stroke:#6c757d,color:#383d41,stroke-dasharray: 1 2;
```
