# bhupencod3/perfectpick architecture

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

- Interactive diagram: https://gitdiagram.com/bhupencod3/perfectpick
- Repository: https://github.com/bhupencod3/perfectpick

## Overview

##### Project Type and Purpose
- This is a full-stack, AI-driven e-commerce recommendation application.
- It consists of a Flask-based web UI, a Python recommendation service (the “PerfectPick” package), a NoSQL database (AstraDB/Cassandra), and a containerized deployment with Kubernetes, Prometheus, and Grafana for monitoring.

##### File Structure
- Top-level code:
- app.py, main.py: Flask entry points
- perfectpick/: Core recommendation library modules (data_ingestion, data_converter, retrieval, generation, recommender, service, session_memory, config)
- notebooks/: Exploratory analysis
- static/, templates/: Frontend assets (CSS, HTML)
- Deployment:
- Dockerfile, deployment/*: Scripts & K8s manifests (deployments, services, ingress, HPA, ConfigMaps, Secrets)
- deployment/prometheus/*, deployment/grafana/*: Monitoring stack definitions
- Utilities and tests under utils/ and tests/.

##### README Insights
- AI/ML components: LangChain, HuggingFace, Groq
- Database: AstraDB (Cassandra)
- Monitoring: Prometheus + Grafana
- Orchestration: Docker + Minikube/Kubernetes

##### Main Components and Their Relationships
a. Frontend (Flask + HTML/CSS)
- Templates (templates/index.html) and static assets (static/style.css)
- Exposes HTTP endpoints (app.py/main.py)
b. Backend Service (“PerfectPick”)
- Data Ingestion & Conversion: cleans and prepares mobile data (data_ingestion.py, data_converter.py)
- Retrieval & Generation: fetches candidate products and generates recommendations using LLMs (retrieval.py, generation.py)
- Recommender Core: ranking and personalization logic (recommender.py)
- Session & Service Layer: manages user sessions and API logic (session_memory.py, service.py)
c. Database (AstraDB/Cassandra)
- Stores user sessions, product metadata, recommendation logs
- Accessed via PerfectPick’s config and data layers
d. External AI Services
- LangChain orchestration, Groq compute, HuggingFace APIs
e. Monitoring & Logging
- Prometheus scrapes metrics endpoints exposed by Flask and PerfectPick
f. Containerization & Orchestration
- Docker container built from Dockerfile
- Kubernetes objects: Namespaces, Deployment, Service, Ingress, ConfigMap, Secret, HPA
- Helm-style scripts under deployment/deploy-all.sh and entrypoint.sh

##### Architectural Patterns & Design Principles
- Monolithic microservice for backend with clear module boundaries (data, retrieval, recommendation, service).
- Separation of concerns: frontend UI vs. backend logic vs. data storage vs. monitoring.
- MLOps pipeline: data ingestion → model invocation → results stored & served.
- Twelve-factor: externalize config (dotenv, K8s ConfigMaps/Secrets), logs to stdout, dependencies via requirements.txt.

- Async or sync calls to external AI APIs.
- Monitoring endpoints (e.g., /metrics) and the scrape configuration in Prometheus.
- Grafana dashboards reading from Prometheus.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Presentation Layer
    subgraph "Presentation Layer"
        direction TB
        UI["Flask UI"]:::frontend
        APP_PY["app.py"]:::frontend
        MAIN_PY["main.py"]:::frontend
        TEMPLATE["templates/index.html"]:::frontend
        STATIC["static/style.css"]:::frontend
        UI --> APP_PY
        UI --> MAIN_PY
        APP_PY --> TEMPLATE
        MAIN_PY --> STATIC
    end

    %% Ingress and User
    User["User Browser"]:::frontend
    Ingress["K8s Ingress"]:::orchestration

    User -->|"HTTP(S)"| Ingress
    Ingress -->|"routes to"| UI

    %% Application Layer
    subgraph "Application Layer (PerfectPick Service)"
        direction TB
        CONFIG["config.py"]:::app
        INGEST["data_ingestion.py"]:::app
        CONVERT["data_converter.py"]:::app
        RETRIEVE["retrieval.py"]:::app
        GENERATE["generation.py"]:::app
        RECOMMEND["recommender.py"]:::app
        SERVICE["service.py"]:::app
        SESSION["session_memory.py"]:::app

        SERVICE --> SESSION
        SERVICE --> RECOMMEND
        RECOMMEND --> RETRIEVE
        RECOMMEND --> GENERATE
        INGEST --> CONVERT
        SERVICE --> INGEST
        SERVICE --> CONFIG
    end

    UI -->|"REST API"| SERVICE

    %% Data Layer
    subgraph "Data Layer"
        direction TB
        AstraDB["AstraDB / Cassandra"]:::data
    end

    SERVICE -->|"Cassandra Driver"| AstraDB

    %% External AI Services
    subgraph "External AI API Layer"
        direction TB
        LangChain["LangChain"]:::external
        Groq["Groq Compute"]:::external
        HuggingFace["HuggingFace APIs"]:::external
    end

    RETRIEVE -->|"orchestrates"| LangChain
    GENERATE -->|"calls"| HuggingFace
    GENERATE -->|"uses"| Groq

    %% Monitoring Layer
    subgraph "Monitoring Layer"
        direction TB
        Prometheus["Prometheus"]:::monitor
        Grafana["Grafana"]:::monitor
    end

    UI -->|"exposes /metrics"| Prometheus
    SERVICE -->|"exposes /metrics"| Prometheus
    Prometheus -->|"data source"| Grafana

    %% Orchestration Layer
    subgraph "Orchestration Layer"
        direction TB
        Docker["Docker"]:::orchestration
        K8s_NS["namespace.yaml"]:::orchestration
        K8s_Deploy["deployment.yml"]:::orchestration
        K8s_Svc["service.yml"]:::orchestration
        K8s_Ing["ingress.yaml"]:::orchestration
        K8s_HPA["hpa.yaml"]:::orchestration
        K8s_Config["configmap.yaml"]:::orchestration
        K8s_Secret["secret-template.yaml"]:::orchestration
    end

    Docker -->|"builds image"| K8s_Deploy
    K8s_NS --> K8s_Deploy
    K8s_Deploy --> K8s_Svc
    K8s_Svc --> K8s_Ing
    K8s_Deploy --> K8s_HPA
    K8s_Deploy --> K8s_Config
    K8s_Deploy --> K8s_Secret

    %% Utilities & Tests (optional overflow)
    subgraph "Utilities & Tests"
        direction TB
        Logger["utils/logger.py"]:::app
        Exception["utils/custom_exception.py"]:::app
        Validators["utils/validators.py"]:::app
        TestIngest["tests/test_data_ingestion.py"]:::app
        TestConvert["tests/test_data_converter.py"]:::app
        TestRec["tests/test_recommender.py"]:::app
        TestServ["tests/test_service.py"]:::app
        TestConfig["tests/test_config.py"]:::app
    end

    %% Click Events
    click APP_PY "https://github.com/bhupencod3/perfectpick/blob/main/app.py"
    click MAIN_PY "https://github.com/bhupencod3/perfectpick/blob/main/main.py"
    click TEMPLATE "https://github.com/bhupencod3/perfectpick/blob/main/templates/index.html"
    click STATIC "https://github.com/bhupencod3/perfectpick/blob/main/static/style.css"
    click CONFIG "https://github.com/bhupencod3/perfectpick/blob/main/perfectpick/config.py"
    click INGEST "https://github.com/bhupencod3/perfectpick/blob/main/perfectpick/data_ingestion.py"
    click CONVERT "https://github.com/bhupencod3/perfectpick/blob/main/perfectpick/data_converter.py"
    click RETRIEVE "https://github.com/bhupencod3/perfectpick/blob/main/perfectpick/retrieval.py"
    click GENERATE "https://github.com/bhupencod3/perfectpick/blob/main/perfectpick/generation.py"
    click RECOMMEND "https://github.com/bhupencod3/perfectpick/blob/main/perfectpick/recommender.py"
    click SERVICE "https://github.com/bhupencod3/perfectpick/blob/main/perfectpick/service.py"
    click SESSION "https://github.com/bhupencod3/perfectpick/blob/main/perfectpick/session_memory.py"
    click K8s_Config "https://github.com/bhupencod3/perfectpick/blob/main/deployment/k8s/configmap.yaml"
    click K8s_Secret "https://github.com/bhupencod3/perfectpick/blob/main/deployment/k8s/secret-template.yaml"
    click Docker "https://github.com/bhupencod3/perfectpick/tree/main/Dockerfile"
    click K8s_Deploy "https://github.com/bhupencod3/perfectpick/blob/main/deployment/k8s/deployment.yml"
    click K8s_Svc "https://github.com/bhupencod3/perfectpick/blob/main/deployment/k8s/service.yml"
    click K8s_Ing "https://github.com/bhupencod3/perfectpick/blob/main/deployment/k8s/ingres.yaml"
    click K8s_HPA "https://github.com/bhupencod3/perfectpick/blob/main/deployment/k8s/hpa.yaml"
    click K8s_NS "https://github.com/bhupencod3/perfectpick/blob/main/deployment/k8s/namespace.yaml"
    click Logger "https://github.com/bhupencod3/perfectpick/blob/main/utils/logger.py"
    click Exception "https://github.com/bhupencod3/perfectpick/blob/main/utils/custom_exception.py"
    click Validators "https://github.com/bhupencod3/perfectpick/blob/main/utils/validators.py"
    click TestIngest "https://github.com/bhupencod3/perfectpick/blob/main/tests/test_data_ingestion.py"
    click TestConvert "https://github.com/bhupencod3/perfectpick/blob/main/tests/test_data_converter.py"
    click TestRec "https://github.com/bhupencod3/perfectpick/blob/main/tests/test_recommender.py"
    click TestServ "https://github.com/bhupencod3/perfectpick/blob/main/tests/test_service.py"
    click TestConfig "https://github.com/bhupencod3/perfectpick/blob/main/tests/test_config.py"
    click Prometheus "https://github.com/bhupencod3/perfectpick/blob/main/deployment/prometheus/prometheus-deployment.yaml"
    click Grafana "https://github.com/bhupencod3/perfectpick/blob/main/deployment/grafana/grafana-deployment.yaml"

    %% Styles
    classDef frontend fill:#D0E6FF,stroke:#003366,color:#003366
    classDef app fill:#DFF5E1,stroke:#2E7D32,color:#2E7D32
    classDef data fill:#FFE7C0,stroke:#FF9800,color:#E65100
    classDef external fill:#F3E5F5,stroke:#6A1B9A,color:#4A148C
    classDef monitor fill:#FFEBEE,stroke:#D32F2F,color:#B71C1C
    classDef orchestration fill:#F5F5F5,stroke:#616161,color:#212121
```
