SuperMarioGPT / ARCHITECTURE.md
DarkDriftz's picture
Upload 18 files
49b8c43 verified

A newer version of the Gradio SDK is available: 6.1.0

Upgrade

MarioGPT System Architecture

πŸ—οΈ Architecture Overview

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                        MarioGPT System                           β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   User Interfaces β”‚         β”‚   AI Assistants   β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€         β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ β€’ Web Browser    β”‚         β”‚ β€’ HuggingChat    β”‚
β”‚ β€’ Mobile Browser β”‚         β”‚ β€’ Claude Desktop β”‚
β”‚ β€’ Desktop App    β”‚         β”‚ β€’ Other MCP      β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜         β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚                            β”‚
         β”‚ HTTP/WebSocket             β”‚ MCP Protocol (stdio)
         β”‚                            β”‚
         β–Ό                            β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Gradio App     β”‚         β”‚   MCP Server     β”‚
β”‚  (app.py)       β”‚         β”‚  (mcp_server.py) β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€         β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ β€’ Web UI        β”‚         β”‚ β€’ Tool Registry  β”‚
β”‚ β€’ FastAPI       β”‚         β”‚ β€’ Async Handler  β”‚
β”‚ β€’ File Serving  β”‚         β”‚ β€’ Validation     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜         β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚                           β”‚
         β”‚                           β”‚
         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                     β”‚
                     β–Ό
         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
         β”‚   MarioGPT Core       β”‚
         β”‚   (supermariogpt/)    β”‚
         β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
         β”‚ β€’ MarioLM             β”‚
         β”‚ β€’ Dataset             β”‚
         β”‚ β€’ Prompter            β”‚
         β”‚ β€’ Utils               β”‚
         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                     β”‚
         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
         β”‚                       β”‚
         β–Ό                       β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  GPT-2 Model    β”‚   β”‚  Level Renderer   β”‚
β”‚  (transformers) β”‚   β”‚  (PIL/Pillow)     β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€   β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ β€’ Text Gen      β”‚   β”‚ β€’ PNG Generation β”‚
β”‚ β€’ Tokenization  β”‚   β”‚ β€’ Tile Mapping   β”‚
β”‚ β€’ Sampling      β”‚   β”‚ β€’ Visualization  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚                     β”‚
         β”‚                     β”‚
         β–Ό                     β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚        Output Artifacts         β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ β€’ Level Text (ASCII)            β”‚
β”‚ β€’ Level Image (PNG)             β”‚
β”‚ β€’ Playable HTML (CheerpJ)       β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ”„ Data Flow Diagrams

Gradio Web Interface Flow

User Input
    β”‚
    β”œβ”€ Compose Prompt (Radio buttons)
    β”‚  └─ Pipes + Enemies + Blocks + Elevation
    β”‚
    β”œβ”€ Type Prompt (Text field)
    β”‚  └─ Custom text description
    β”‚
    └─ Advanced Settings
       β”œβ”€ Temperature (0.1-2.0)
       └─ Level Size (100-2799)
    β”‚
    β–Ό
Validation & Processing
    β”‚
    β”œβ”€ Validate temperature range
    β”œβ”€ Validate level size range
    └─ Format prompt string
    β”‚
    β–Ό
MarioLM.sample()
    β”‚
    β”œβ”€ Tokenize prompt
    β”œβ”€ Generate tokens (GPT-2)
    └─ Decode to level format
    β”‚
    β–Ό
Post-Processing
    β”‚
    β”œβ”€ convert_level_to_png()
    β”‚  └─ Create visual representation
    β”‚
    └─ make_html_file()
       └─ Create playable demo
    β”‚
    β–Ό
Output to User
    β”‚
    β”œβ”€ Display PNG image
    └─ Embed playable iframe

MCP Server Flow

HuggingChat Request
    β”‚
    β–Ό
MCP Protocol (JSON-RPC)
    β”‚
    β”œβ”€ tools/list
    β”‚  └─ Return available tools
    β”‚
    └─ tools/call
       β”œβ”€ Tool: generate_mario_level
       β”‚  └─ Parameters: prompt, temperature, level_size
       β”‚
       └─ Tool: get_level_suggestions
          └─ No parameters
    β”‚
    β–Ό
Parameter Validation (Pydantic)
    β”‚
    β”œβ”€ Check types
    β”œβ”€ Validate ranges
    └─ Apply defaults
    β”‚
    β–Ό
Lazy Model Initialization
    β”‚
    β”œβ”€ Check if model loaded
    └─ Load if needed (first use)
    β”‚
    β–Ό
Generate Level
    β”‚
    β”œβ”€ MarioLM.sample()
    β”œβ”€ view_level() β†’ Text
    └─ convert_level_to_png() β†’ Image
    β”‚
    β–Ό
Encode Response
    β”‚
    β”œβ”€ Text as TextContent
    └─ Image as base64 + ImageContent
    β”‚
    β–Ό
Return to HuggingChat
    β”‚
    β”œβ”€ Display text description
    └─ Render image inline

πŸ“¦ Component Details

Core Components

1. MarioLM (Language Model)

class MarioLM:
    - model: GPT-2 Transformer
    - tokenizer: Custom Mario tokenizer
    - device: CUDA or CPU
    
    Methods:
    - sample(prompts, num_steps, temperature)
    - load_pretrained()
    - to(device)

2. Gradio Interface

Components:
- Radio buttons (pipes, enemies, blocks, elevation)
- Text input (custom prompts)
- Sliders (temperature, level_size)
- Button (generate)
- Image output (PNG preview)
- HTML output (playable demo)

3. MCP Server

Tools:
- generate_mario_level(prompt, temp, size)
  β†’ Returns: TextContent + ImageContent
  
- get_level_suggestions()
  β†’ Returns: TextContent (examples)

Protocol: stdio (JSON-RPC 2.0)
Transport: asyncio streams

4. FastAPI Backend

Routes:
- / β†’ Gradio app
- /static β†’ Static file serving
- /gradio_api β†’ Gradio API

Features:
- HTML file generation
- Static file hosting
- CORS handling

πŸ”§ Technology Stack

Backend

Python 3.8+
β”œβ”€β”€ torch (Deep Learning)
β”œβ”€β”€ transformers (GPT-2)
β”œβ”€β”€ gradio (Web UI)
β”œβ”€β”€ fastapi (API)
β”œβ”€β”€ uvicorn (ASGI Server)
└── mcp (Model Context Protocol)

Model

GPT-2 Architecture
β”œβ”€β”€ Input: Text prompt
β”œβ”€β”€ Process: Token generation
β”œβ”€β”€ Output: Mario level tokens
└── Decode: ASCII level format

Frontend

Gradio Components
β”œβ”€β”€ HTML5
β”œβ”€β”€ JavaScript
β”œβ”€β”€ CheerpJ (Java β†’ JavaScript)
└── WebSocket (real-time updates)

Protocols

HTTP/HTTPS
β”œβ”€β”€ REST API
└── WebSocket

MCP (Model Context Protocol)
β”œβ”€β”€ JSON-RPC 2.0
β”œβ”€β”€ stdio transport
└── Tool-based interface

πŸš€ Deployment Architectures

Architecture 1: HuggingFace Spaces

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚     HuggingFace Spaces          β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                                 β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚
β”‚  β”‚   Docker Container       β”‚  β”‚
β”‚  β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚  β”‚
β”‚  β”‚  β”‚   Gradio App       β”‚  β”‚  β”‚
β”‚  β”‚  β”‚   (app.py)         β”‚  β”‚  β”‚
β”‚  β”‚  β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚  β”‚
β”‚  β”‚         β”‚                β”‚  β”‚
β”‚  β”‚  β”Œβ”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚  β”‚
β”‚  β”‚  β”‚   MarioGPT Model   β”‚  β”‚  β”‚
β”‚  β”‚  β”‚   (GPU: T4/A10G)   β”‚  β”‚  β”‚
β”‚  β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚  β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚
β”‚                                 β”‚
β”‚  Storage: Persistent /static    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
          β”‚
          β–Ό
    Internet Users

Architecture 2: MCP Server Integration

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   HuggingChat UI     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
           β”‚
           β”‚ MCP Protocol
           β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   MCP Router         β”‚
β”‚   (HuggingChat)      β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
           β”‚
           β”‚ stdio
           β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   mcp_server.py      β”‚
β”‚   (Your Machine)     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
           β”‚
           β”‚ Python API
           β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   MarioGPT Model     β”‚
β”‚   (Local GPU/CPU)    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Architecture 3: Hybrid Setup

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚         Load Balancer            β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
            β”‚
     β”Œβ”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”
     β”‚             β”‚
     β–Ό             β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Gradio  β”‚   β”‚  MCP    β”‚
β”‚ Server  β”‚   β”‚ Server  β”‚
β”‚ (Web)   β”‚   β”‚ (API)   β”‚
β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”˜   β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”˜
     β”‚             β”‚
     β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜
            β–Ό
    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
    β”‚ Shared Model β”‚
    β”‚   Storage    β”‚
    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ“Š Performance Characteristics

Latency Breakdown

Total Generation Time: 5-10s (GPU) / 30-60s (CPU)

β”œβ”€β”€ Model Loading: 2-3s (first time only)
β”œβ”€β”€ Prompt Processing: <0.1s
β”œβ”€β”€ Token Generation: 3-7s (GPU) / 25-55s (CPU)
β”œβ”€β”€ Post-Processing: 0.5-1s
β”‚   β”œβ”€β”€ Level rendering: 0.3s
β”‚   └── PNG generation: 0.2s
└── File Writing: <0.1s

Resource Usage

GPU Mode (T4):
β”œβ”€β”€ VRAM: 4-6 GB
β”œβ”€β”€ System RAM: 2-4 GB
└── CPU: 1-2 cores (minimal)

CPU Mode:
β”œβ”€β”€ System RAM: 8-12 GB
β”œβ”€β”€ CPU: 4-8 cores (recommended)
└── Generation: ~5-10x slower

πŸ” Security Architecture

Input Validation

User Input
    β”‚
    β”œβ”€ Temperature
    β”‚  └─ Clamp: max(0.1, min(2.0, value))
    β”‚
    β”œβ”€ Level Size
    β”‚  └─ Clamp: max(100, min(2799, value))
    β”‚
    └─ Prompt Text
       └─ Sanitize: remove special chars
    β”‚
    β–Ό
Safe Processing

File Handling

Generated Files
    β”‚
    β”œβ”€ UUID v4 naming (privacy-safe)
    β”œβ”€ Restricted directory (./static only)
    β”œβ”€ Validated extensions (.html, .png)
    └─ Size limits enforced

🎯 Integration Points

1. HuggingFace Spaces

  • Direct deployment
  • GPU auto-allocation
  • Persistent storage
  • Built-in CDN

2. HuggingChat (MCP)

  • Tool registration
  • JSON-RPC protocol
  • Async execution
  • Rich responses (text + image)

3. Custom Applications

  • FastAPI endpoints
  • Python SDK import
  • Docker deployment
  • API integration

πŸ“ˆ Scalability Considerations

Horizontal Scaling

Load Balancer
    β”‚
    β”œβ”€ Instance 1 (GPU)
    β”œβ”€ Instance 2 (GPU)
    └─ Instance 3 (CPU fallback)

Vertical Scaling

Single Instance
β”œβ”€β”€ More GPU memory β†’ Larger batches
β”œβ”€β”€ Faster GPU β†’ Quicker generation
└── More CPU cores β†’ Better preprocessing

Caching Strategy

Cache Layer
β”œβ”€β”€ Generated levels (by prompt hash)
β”œβ”€β”€ Model weights (persistent)
└── Static assets (CDN)

Version: 1.0.0 Last Updated: December 6, 2024 Architecture: Modular, Scalable, MCP-Compatible