# apple/ml-keyframer architecture

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

- Interactive diagram: https://gitdiagram.com/apple/ml-keyframer
- Repository: https://github.com/apple/ml-keyframer

## Overview

##### Project type and purpose
- Project Type: Full-stack web application built with Next.js and React.
- Purpose: “Keyframer” allows designers to upload or select an SVG, send a natural-language prompt to GPT-4 (via an API route), and receive CSS animations which can then be iteratively refined through a UI with code and property editors.

##### File structure
- Top-level directories/files:
- pages/        → Next.js routing:
– pages/index.js: main UI entry point
– pages/api/generate.js: serverless API endpoint that forwards prompts to OpenAI
- components/   → React components for inputs, forms, editors, output displays
- public/       → static assets (e.g., default SVGs under figs/)
- package.json  → dependencies (React, Next.js, OpenAI SDK, CSS modules)
- figs/         → documentation screenshots used in README
- No traditional database—state is kept in React, calls to GPT-4 are stateless, with prompts and SVG code passed on each request.

##### README for additional insights
- Architecture described implicitly:
- Frontend React interface (Input SVG, Prompt editors, Code/Property editors, Output display)
- Backend API route that uses the OpenAI Node.js client to call GPT-4
- Dependencies:
- OpenAI API key required
- Next.js dev and build scripts

##### Elements
- Clear labels:
- “Browser UI (Next.js + React components)”
- “Serverless Function (/api/generate.js)”
- “OpenAI GPT-4 API”
- Blue rectangles for UI components
- Orange rectangle for serverless/API layer
- Annotations for key functions:
- “GenerateAnimationsButton → POST SVG + prompt”
- “PropertyEditor → CSS updates”
- “CodeEditor → raw CSS editing”

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Frontend Layer
    subgraph "Browser UI (Next.js + React components)"
        direction TB
        IndexPage["pages/index.js"]:::ui
        CSSModule["pages/index.module.css"]:::ui
        ClientState["Client State (in-memory)"]:::state

        subgraph "SVG Input Components"
            direction TB
            SVGForm["SVGForm.js"]:::ui
            SVGInput["SVGInput.js"]:::ui
            DefaultSVGs["defaultSVGs.js"]:::ui
        end

        subgraph "Prompt Editors"
            direction TB
            FullPrompt["FullPrompt.js"]:::ui
            IterationComponent["IterationComponent.js"]:::ui
            Prompt4Multiples["Multiples/Prompt4Multiples.js"]:::ui
            PromptHelpers["Multiples/PromptHelpers.js"]:::ui
        end

        subgraph "Code & CSS Forms"
            direction TB
            CodeForm["CodeForm.js"]:::ui
            CSSForm["CSSForm.js"]:::ui
        end

        subgraph "Property Editors"
            direction TB
            PropertiesForm["PropertiesForm.js"]:::ui
            BezierSelect["PropertyFormElements/BezierSelect.js"]:::ui
            TimingFunctionSelect["PropertyFormElements/TimingFunctionSelect.js"]:::ui
            ColorPicker["PropertyFormElements/ColorPicker.js"]:::ui
        end

        subgraph "Output & Rendering"
            direction TB
            OutputEditor["OutputEditor.js"]:::ui
            SVGOutput["SVGOutput.js"]:::ui
            MultiplesRendering["Multiples/MultiplesRendering.js"]:::ui
            MultiplesOutput["Multiples/MultiplesOutput.js"]:::ui
        end

        subgraph "UI Helpers & Controls"
            direction TB
            ButtonGroup["ButtonGroup.js"]:::ui
            SegmentedControl["SegmentedControl.js"]:::ui
            TranslateHelpers["TranslateHelpers.js"]:::ui
            ErrorMessage["ErrorMessage.js"]:::ui
            Backpack["Backpack.js"]:::ui
        end
    end

    %% Backend Layer
    subgraph "Serverless Function (/api)"
        direction TB
        APIGenerate["pages/api/generate.js"]:::api
    end

    %% External Services
    subgraph "External Service"
        direction TB
        OpenAI["OpenAI GPT-4 API"]:::ext
    end

    %% Configuration
    subgraph "Project Configuration"
        direction TB
        PackageJSON["package.json"]:::config
        PackageLock["package-lock.json"]:::config
    end

    %% Data Flows
    IndexPage -->|"maintains state"| ClientState
    ClientState -->|"user submits prompt"| APIGenerate
    APIGenerate -->|"calls GPT-4"| OpenAI
    OpenAI -->|"returns CSS code"| APIGenerate
    APIGenerate -->|"responds with CSS"| ClientState
    ClientState -->|"updates UI"| OutputEditor

    %% Click Events
    click IndexPage "https://github.com/apple/ml-keyframer/blob/main/pages/index.js"
    click CSSModule "https://github.com/apple/ml-keyframer/blob/main/pages/index.module.css"
    click APIGenerate "https://github.com/apple/ml-keyframer/blob/main/pages/api/generate.js"
    click SVGForm "https://github.com/apple/ml-keyframer/blob/main/components/SVGForm.js"
    click SVGInput "https://github.com/apple/ml-keyframer/blob/main/components/SVGInput.js"
    click DefaultSVGs "https://github.com/apple/ml-keyframer/blob/main/components/defaultSVGs.js"
    click FullPrompt "https://github.com/apple/ml-keyframer/blob/main/components/FullPrompt.js"
    click IterationComponent "https://github.com/apple/ml-keyframer/blob/main/components/IterationComponent.js"
    click Prompt4Multiples "https://github.com/apple/ml-keyframer/blob/main/components/Multiples/Prompt4Multiples.js"
    click PromptHelpers "https://github.com/apple/ml-keyframer/blob/main/components/Multiples/PromptHelpers.js"
    click CodeForm "https://github.com/apple/ml-keyframer/blob/main/components/CodeForm.js"
    click CSSForm "https://github.com/apple/ml-keyframer/blob/main/components/CSSForm.js"
    click PropertiesForm "https://github.com/apple/ml-keyframer/blob/main/components/PropertiesForm.js"
    click BezierSelect "https://github.com/apple/ml-keyframer/blob/main/components/PropertyFormElements/BezierSelect.js"
    click TimingFunctionSelect "https://github.com/apple/ml-keyframer/blob/main/components/PropertyFormElements/TimingFunctionSelect.js"
    click ColorPicker "https://github.com/apple/ml-keyframer/blob/main/components/PropertyFormElements/ColorPicker.js"
    click OutputEditor "https://github.com/apple/ml-keyframer/blob/main/components/OutputEditor.js"
    click SVGOutput "https://github.com/apple/ml-keyframer/blob/main/components/SVGOutput.js"
    click MultiplesRendering "https://github.com/apple/ml-keyframer/blob/main/components/Multiples/MultiplesRendering.js"
    click MultiplesOutput "https://github.com/apple/ml-keyframer/blob/main/components/Multiples/MultiplesOutput.js"
    click ButtonGroup "https://github.com/apple/ml-keyframer/blob/main/components/ButtonGroup.js"
    click SegmentedControl "https://github.com/apple/ml-keyframer/blob/main/components/SegmentedControl.js"
    click TranslateHelpers "https://github.com/apple/ml-keyframer/blob/main/components/TranslateHelpers.js"
    click ErrorMessage "https://github.com/apple/ml-keyframer/blob/main/components/ErrorMessage.js"
    click Backpack "https://github.com/apple/ml-keyframer/blob/main/components/Backpack.js"
    click PackageJSON "https://github.com/apple/ml-keyframer/blob/main/package.json"
    click PackageLock "https://github.com/apple/ml-keyframer/blob/main/package-lock.json"

    %% Styles
    classDef ui fill:#1f77b4,stroke:#333,stroke-width:1px,color:#fff;
    classDef api fill:#ff7f0e,stroke:#333,stroke-width:1px,color:#000;
    classDef ext fill:#9467bd,stroke:#333,stroke-width:1px,color:#000;
    classDef state fill:#2ca02c,stroke:#333,stroke-width:1px,color:#000;
    classDef config fill:#8c564b,stroke:#333,stroke-width:1px,color:#fff;
```
