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