Spaces:
Runtime error
Runtime error
File size: 13,462 Bytes
49b8c43 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 |
# 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)
```python
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
```python
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
```python
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
```python
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
|