# underneathall/pinferencia architecture

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

- Interactive diagram: https://gitdiagram.com/underneathall/pinferencia
- Repository: https://github.com/underneathall/pinferencia

## Overview

##### Project Type and Purpose
- This is a full-stack Python application—a minimalist, extensible machine-learning inference server called Pinferencia.
- It provides:
- A REST API (default and KServe-compatible)
- A built-in GUI (Streamlit-based)
- Pluggable model handlers (PyTorch, TensorFlow, HuggingFace, joblib, dill, etc.)
- Automatic API documentation (Swagger UI)

##### High-Level File-Structure Analysis
- Top-level directories:
- pinferencia/ (core library)
- examples/ (demo apps for various frameworks)
- tests/ (unit and end-to-end tests)
- docs/ (MkDocs documentation)
- Key subdirectories under pinferencia/:
- apis/… (REST routers & versioned handlers: default, KServe v1/v2)
- frontend/… (API manager for GUI, Streamlit app, templates)
- handlers/… (model deserializers & runners)
- model_manager.py & repository.py (model lifecycle & storage)
- static/ & swagger.py (embedded Swagger UI assets)
- main.py (entrypoint wiring CLI→Server)

##### Insights from README
- Core concept: A single `Server` class that you instantiate, `register()`, then launch via CLI (`pinfer app:service` or `uvicorn app:service`).
- Under the hood:
- FastAPI/Starlette (via Uvicorn) for REST
- Streamlit for GUI
- Modular routers for default vs. KServe APIs
- Handlers choose the right way to call your model

##### Diagramming the Architecture
1) CLI / Entrypoint
- `pinfer` / `uvicorn` → runs `main.py` → builds two server sub-apps
2) Backend HTTP Server
- Uvicorn + FastAPI/Starlette
- API Manager (registers routers & middleware)
- Routers:
- Default API (basic, management, metadata)
- KServe v1 & KServe v2 (predict, metadata)
- Swagger UI (served from static/)
3) Frontend GUI
- Streamlit app (pinferencia/frontend/app.py)
- Uses Frontend API Manager to call backend endpoints
- Templates for different ML tasks
4) Core Services
- Model Manager & Repository: holds registered models & metadata in‐memory
- Handlers: dill, joblib, pytorch, etc. (load/deserialize & execute `predict`)
- Context & Config modules (global settings)
5) External Dependencies
- ML frameworks (transformers, torch, tensorflow, paddlepaddle…)
- HTTP server (uvicorn)
- CLI parsing (click or Typer under the hood)

1) User → CLI command → Server startup
2) User → Browser/Streamlit UI → Frontend GUI → HTTP REST → Backend Server
3) User → HTTP client (curl, Postman) → REST API → Router → Model Manager → Handler → External ML library → Handler returns result → Router formats → Client
4) Backend → serves Swagger UI assets → Browser

c. Architectural Patterns & Principles
- Modular “pluggable” sub-APIs (default vs. KServe)
- Factory/Manager pattern (Model Manager, API Manager)
- Dependency injection via global Context
- Clear separation of concerns:
- Routing & parsing (apis/)
- Model lifecycle (model_manager.py, repository.py)
- Model execution (handlers/)
- UI layer (frontend/)

d. Key Technologies & Frameworks
- Python 3.7+
- FastAPI/Starlette (REST) + Uvicorn
- Streamlit (GUI)
- Pytest (tests)
- MkDocs (documentation)
- Swagger UI (static assets)
- ML libraries: torch, tensorflow, transformers, paddlepaddle, joblib, dill

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% CLI / Entrypoint
    subgraph "CLI / Entrypoint"
        Entrypoint["CLI Entrypoint"]:::cli
    end

    Entrypoint --> ServerApp["Server App"]:::backend
    click Entrypoint "https://github.com/underneathall/pinferencia/blob/main/pinferencia/main.py"
    click ServerApp "https://github.com/underneathall/pinferencia/blob/main/pinferencia/app.py"

    %% Backend REST API
    subgraph "Backend (REST API)" 
        APIManager["API Manager"]:::backend
        SwaggerUI["Swagger UI"]:::backend
        APIManager --> DefaultAPI
        APIManager --> KServeV1
        APIManager --> KServeV2
        APIManager --> SwaggerUI
    end
    click APIManager "https://github.com/underneathall/pinferencia/blob/main/pinferencia/api_manager.py"
    click SwaggerUI "https://github.com/underneathall/pinferencia/blob/main/pinferencia/swagger.py"

    %% Default API Routers
    subgraph "Default API (v1)" 
        DefaultConfig["config.py"]:::backend
        DefaultModels["models.py"]:::backend
        DefaultParsers["parsers.py"]:::backend
        BasicRouter["basic.py"]:::backend
        MgmtRouter["management.py"]:::backend
        MetaRouter["metadata.py"]:::backend

        DefaultConfig --> BasicRouter
        DefaultModels --> BasicRouter
        DefaultParsers --> BasicRouter
        DefaultConfig --> MgmtRouter
        DefaultModels --> MgmtRouter
        DefaultParsers --> MgmtRouter
        DefaultConfig --> MetaRouter
        DefaultModels --> MetaRouter
        DefaultParsers --> MetaRouter
    end
    click DefaultConfig "https://github.com/underneathall/pinferencia/blob/main/pinferencia/apis/default/v1/config.py"
    click DefaultModels "https://github.com/underneathall/pinferencia/blob/main/pinferencia/apis/default/v1/models.py"
    click DefaultParsers "https://github.com/underneathall/pinferencia/blob/main/pinferencia/apis/default/v1/parsers.py"
    click BasicRouter "https://github.com/underneathall/pinferencia/blob/main/pinferencia/apis/default/v1/routers/basic.py"
    click MgmtRouter "https://github.com/underneathall/pinferencia/blob/main/pinferencia/apis/default/v1/routers/management.py"
    click MetaRouter "https://github.com/underneathall/pinferencia/blob/main/pinferencia/apis/default/v1/routers/metadata.py"

    %% KServe v1 API Routers
    subgraph "KServe API v1" 
        K1Config["config.py"]:::backend
        K1Models["models.py"]:::backend
        K1Parsers["parsers.py"]:::backend
        K1Meta["metadata.py"]:::backend
        K1Predict["predict.py"]:::backend

        K1Config --> K1Meta
        K1Models --> K1Meta
        K1Parsers --> K1Meta
        K1Config --> K1Predict
        K1Models --> K1Predict
        K1Parsers --> K1Predict
    end
    click K1Config "https://github.com/underneathall/pinferencia/blob/main/pinferencia/apis/kserve/v1/config.py"
    click K1Models "https://github.com/underneathall/pinferencia/blob/main/pinferencia/apis/kserve/v1/models.py"
    click K1Parsers "https://github.com/underneathall/pinferencia/blob/main/pinferencia/apis/kserve/v1/parsers.py"
    click K1Meta "https://github.com/underneathall/pinferencia/blob/main/pinferencia/apis/kserve/v1/routers/metadata.py"
    click K1Predict "https://github.com/underneathall/pinferencia/blob/main/pinferencia/apis/kserve/v1/routers/predict.py"

    %% KServe v2 API Routers
    subgraph "KServe API v2" 
        K2Config["config.py"]:::backend
        K2Models["models.py"]:::backend
        K2Parsers["parsers.py"]:::backend
        K2Meta["metadata.py"]:::backend
        K2Predict["predict.py"]:::backend

        K2Config --> K2Meta
        K2Models --> K2Meta
        K2Parsers --> K2Meta
        K2Config --> K2Predict
        K2Models --> K2Predict
        K2Parsers --> K2Predict
    end
    click K2Config "https://github.com/underneathall/pinferencia/blob/main/pinferencia/apis/kserve/v2/config.py"
    click K2Models "https://github.com/underneathall/pinferencia/blob/main/pinferencia/apis/kserve/v2/models.py"
    click K2Parsers "https://github.com/underneathall/pinferencia/blob/main/pinferencia/apis/kserve/v2/parsers.py"
    click K2Meta "https://github.com/underneathall/pinferencia/blob/main/pinferencia/apis/kserve/v2/routers/metadata.py"
    click K2Predict "https://github.com/underneathall/pinferencia/blob/main/pinferencia/apis/kserve/v2/routers/predict.py"

    ServerApp --> APIManager

    %% Frontend GUI
    subgraph "Frontend (GUI)" 
        FrontendApp["Streamlit App"]:::frontend
        FEAPIManager["Frontend API Manager"]:::frontend
        Template1["image_classification.py"]:::frontend
        Template2["text_to_text.py"]:::frontend
        FEConfig["config.py"]:::frontend
        FEModelMgr["model_manager.py"]:::frontend

        FrontendApp --> FEAPIManager
        FEAPIManager --> Template1
        FEAPIManager --> Template2
        FrontendApp --> FEConfig
        FrontendApp --> FEModelMgr
    end
    click FrontendApp "https://github.com/underneathall/pinferencia/blob/main/pinferencia/frontend/app.py"
    click FEAPIManager "https://github.com/underneathall/pinferencia/blob/main/pinferencia/frontend/api_manager/base.py"
    click FEAPIManager "https://github.com/underneathall/pinferencia/blob/main/pinferencia/frontend/api_manager/default.py"
    click Template1 "https://github.com/underneathall/pinferencia/blob/main/pinferencia/frontend/templates/image_classification.py"
    click Template2 "https://github.com/underneathall/pinferencia/blob/main/pinferencia/frontend/templates/text_to_text.py"
    click FEConfig "https://github.com/underneathall/pinferencia/blob/main/pinferencia/frontend/config.py"
    click FEModelMgr "https://github.com/underneathall/pinferencia/blob/main/pinferencia/frontend/model_manager.py"

    FrontendApp -->|REST calls| APIManager

    %% Core Services
    subgraph "Core Services" 
        CoreModelMgr["Model Manager"]:::core
        CoreRepo["Repository"]:::core
        ContextMod["context.py"]:::core
        ConfigMod["config.py"]:::core

        CoreModelMgr --> CoreRepo
    end
    click CoreModelMgr "https://github.com/underneathall/pinferencia/blob/main/pinferencia/model_manager.py"
    click CoreRepo "https://github.com/underneathall/pinferencia/blob/main/pinferencia/repository.py"
    click ContextMod "https://github.com/underneathall/pinferencia/blob/main/pinferencia/context.py"
    click ConfigMod "https://github.com/underneathall/pinferencia/blob/main/pinferencia/config.py"

    %% Handlers
    subgraph "Handlers" 
        BaseHandler["base.py"]:::core
        DillHandler["dill.py"]:::core
        JoblibHandler["joblib.py"]:::core
        TorchHandler["pytorch.py"]:::core

        CoreModelMgr --> BaseHandler
        CoreModelMgr --> DillHandler
        CoreModelMgr --> JoblibHandler
        CoreModelMgr --> TorchHandler
    end
    click BaseHandler "https://github.com/underneathall/pinferencia/blob/main/pinferencia/handlers/base.py"
    click DillHandler "https://github.com/underneathall/pinferencia/blob/main/pinferencia/handlers/dill.py"
    click JoblibHandler "https://github.com/underneathall/pinferencia/blob/main/pinferencia/handlers/joblib.py"
    click TorchHandler "https://github.com/underneathall/pinferencia/blob/main/pinferencia/handlers/pytorch.py"

    %% External ML Libraries
    subgraph "External ML Libraries" 
        Transformers["Transformers"]:::external
        TorchLib["PyTorch"]:::external
        TF["TensorFlow"]:::external
        Paddle["PaddlePaddle"]:::external
        JoblibLib["joblib"]:::external
        DillLib["dill"]:::external
    end

    BaseHandler --> Transformers
    BaseHandler --> TorchLib
    BaseHandler --> TF
    DillHandler --> DillLib
    JoblibHandler --> JoblibLib
    TorchHandler --> TorchLib

    %% User interactions
    UserUI(["User"]):::external
    UserUI -->|CLI command| Entrypoint
    UserUI -->|Browser| FrontendApp
    UserUI -->|HTTP client| APIManager

    %% Styles
    classDef cli fill:#DDDDDD,stroke:#333,stroke-width:1px;
    classDef backend fill:#D0E6FF,stroke:#333,stroke-width:1px;
    classDef frontend fill:#DFFFD0,stroke:#333,stroke-width:1px;
    classDef core fill:#FFE4B2,stroke:#333,stroke-width:1px;
    classDef external fill:#FFF3B0,stroke:#333,stroke-width:1px;
```
