Spaces:
Runtime error
Runtime error
A newer version of the Gradio SDK is available:
6.1.0
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