# parthsolanke/penman architecture

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

- Interactive diagram: https://gitdiagram.com/parthsolanke/penman
- Repository: https://github.com/parthsolanke/penman

## Overview

##### Project Overview & Main Components
- Penman is a full-stack handwriting generator app that delivers real-time handwriting effects using a custom Server-Sent Events (SSE) implementation. The project is split into two major parts – a frontend and a backend – each containerized via Docker.
- Frontend: A React/TypeScript single-page application (with Vite, Tailwind, etc.) that hosts pages like Playground and Cards. It manages user interactions, displays live handwriting updates, and provides export options.
- Backend: A Python-based API server handling requests, SSE streaming, and running the handwriting generation engine. It is composed of classic API endpoints (in app/routes.py) and the handwriting generation logic, including model inference and style management.
- Docker Infrastructure: Indicate that both the frontend and backend are containerized and orchestrated using docker-compose.

##### Analyzing the File Structure
- Under the top-level directories, two primary folders (frontend and backend) directly mirror the two layers of the application.
- The backend directory has a further split:
- The “app” folder (with models.py, routes.py, and utils.py) is where the API endpoints and utility functions reside.
- The “handwriting” folder is the engine core with submodules such as “models” (containing RNN and associated cells/operations), “data” (for loading handwriting and style data), and “utils” (like drawing and TensorFlow helper utilities). It also contains pre-trained model checkpoints and styles files.
- The frontend contains components, pages, contexts, APIs, and configuration files structured for building a modern web UI.

##### Relationships and Data Flow
- Users interact with the Frontend (browsers sending requests).
- The Frontend queries the Backend via standard HTTP requests or initiates an SSE connection for streaming handwriting generation.
- The Backend’s API layer processes incoming requests (via routes defined in app/routes.py) and, when generating handwriting, invokes the handwriting module.
- Within the handwriting module, data loaders fetch style and character/stroke files, while model inference (using RNN-based models) is performed with the TensorFlow-based components. The generated data is then pushed via SSE back to the frontend.
- The Docker environment (via docker-compose) orchestrates both containers and maps ports (frontend on port 3000 and backend API on port 8000).

##### Key Architectural Patterns & Technologies
- Separation of concerns (frontend vs. backend) and containerized deployment.
- Main technologies:
- Frontend: React, TypeScript, Vite, Tailwind.
- Backend: Python, TensorFlow (implied by the model checkpoint files and modules), and Docker.
- Infrastructure: Docker/Docker Compose for container orchestration.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Nodes
    User["User (Browser)"]:::user
    DC["Docker Compose (docker-compose.yml)"]:::docker

    %% User -> Docker Compose
    User -->|"HTTP_Request"| DC

    %% Containers Subgraph
    subgraph "Containers"
      direction TB
      FE["Frontend Container"]:::docker
      BE["Backend Container"]:::docker
    end

    DC --> FE
    DC --> BE

    %% Frontend Application
    subgraph "Frontend Application"
      direction TB
      PAGES["Pages (Home, Playground, Cards)"]:::frontend
      UI["UI Components (ExportButton, GenerateButton)"]:::frontend
    end

    FE --> PAGES
    FE --> UI

    %% Backend Application
    subgraph "Backend Application"
      direction TB
      API["API Endpoints (/app/routes.py)"]:::backend
      Models["Backend Models (/app/models.py)"]:::backend
      Utils["Backend Utilities (/app/utils.py)"]:::backend
      Engine["Handwriting Generation Engine (generator.py)"]:::backend
      API -->|"Invokes"| Engine
      API -->|"Uses"| Models
      API -->|"Uses"| Utils
    end

    BE --> API

    %% Handwriting Engine Components
    subgraph "Handwriting Engine Components"
      direction TB
      RNN["RNN-based Models (rnn.py, rnn_cell.py, rnn_ops.py, tf_base_model.py)"]:::engineComp
      Loaders["Data Loaders (data_loader.py, styles_loader.py)"]:::engineComp
      DU["Drawing Utilities (drawing_utils.py)"]:::engineComp
      TFU["TensorFlow Utilities (tf_utils.py)"]:::engineComp
    end

    Engine -->|"Uses"| RNN
    Engine -->|"Uses"| Loaders
    Engine -->|"Uses"| DU
    Engine -->|"Uses"| TFU

    %% SSE Stream from Backend to Frontend
    BE -->|"SSE_Stream:Incremental_Handwriting_Updates"| FE

    %% Click Events
    click FE "https://github.com/parthsolanke/penman/tree/main/frontend/Dockerfile"
    click DC "https://github.com/parthsolanke/penman/blob/main/docker-compose.yml"
    click PAGES "https://github.com/parthsolanke/penman/tree/main/frontend/src/pages/"
    click UI "https://github.com/parthsolanke/penman/tree/main/frontend/src/components/"
    click API "https://github.com/parthsolanke/penman/blob/main/backend/app/routes.py"
    click Models "https://github.com/parthsolanke/penman/blob/main/backend/app/models.py"
    click Utils "https://github.com/parthsolanke/penman/blob/main/backend/app/utils.py"
    click Engine "https://github.com/parthsolanke/penman/blob/main/backend/handwriting/generator.py"
    click RNN "https://github.com/parthsolanke/penman/tree/main/backend/handwriting/models/"
    click Loaders "https://github.com/parthsolanke/penman/tree/main/backend/handwriting/data/"
    click DU "https://github.com/parthsolanke/penman/blob/main/backend/handwriting/utils/drawing_utils.py"
    click TFU "https://github.com/parthsolanke/penman/blob/main/backend/handwriting/utils/tf_utils.py"
    click BE "https://github.com/parthsolanke/penman/tree/main/backend/Dockerfile"
    click FE "https://github.com/parthsolanke/penman/tree/main/frontend/"

    %% Styles
    classDef user fill:#ffd966,stroke:#333,stroke-width:2px;
    classDef docker fill:#f5b7b1,stroke:#333,stroke-width:2px;
    classDef frontend fill:#f9e79f,stroke:#333,stroke-width:2px;
    classDef backend fill:#aed6f1,stroke:#333,stroke-width:2px;
    classDef engineComp fill:#d5f5e3,stroke:#333,stroke-width:2px;
```
