How underneathall/pinferencia works
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