Srishti280992 commited on
Commit
f0aabd8
·
verified ·
1 Parent(s): caad8d0

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +967 -0
README.md CHANGED
@@ -12,3 +12,970 @@ short_description: Forge ecosystems from small models
12
  ---
13
 
14
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  ---
13
 
14
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
15
+
16
+ # WorldSmithAI
17
+
18
+ **Production-Level Agent-Based World Simulation Framework**
19
+
20
+ WorldSmithAI is a modular Python framework that converts natural language descriptions into generic agent-based worlds and simulates emergent behavior.
21
+
22
+ The core idea is simple:
23
+
24
+ ```text
25
+ Natural language prompt
26
+ → WorldSpec JSON DSL
27
+ → Pydantic validation
28
+ → WorldFactory
29
+ → World
30
+ → Scheduler
31
+ → Agents
32
+ → Policies
33
+ → Behaviors
34
+ → Metrics
35
+ → Visualization
36
+ → Narration
37
+ → Gradio UI
38
+ ```
39
+
40
+ WorldSmithAI is intentionally **not hardcoded for any single domain**.
41
+
42
+ There are no runtime classes like:
43
+
44
+ ```python
45
+ class Sheep
46
+ class Wolf
47
+ class Scientist
48
+ class Merchant
49
+ class Dragon
50
+ ```
51
+
52
+ Instead, every world is composed from generic runtime concepts:
53
+
54
+ ```python
55
+ Agent
56
+ Behavior
57
+ Resource
58
+ Event
59
+ World
60
+ Policy
61
+ ```
62
+
63
+ That means the same engine can simulate:
64
+
65
+ - farm ecosystems
66
+ - medieval civilizations
67
+ - research ecosystems
68
+ - startup economies
69
+ - social networks
70
+ - fantasy worlds
71
+ - transport networks
72
+ - power grids
73
+ - space colonies
74
+ - any DSL-generated ecosystem
75
+
76
+ ---
77
+
78
+ ## Project Status
79
+
80
+ WorldSmithAI is designed as a hackathon-grade prototype with production-oriented architecture.
81
+
82
+ Current capabilities include:
83
+
84
+ - generic agent representation
85
+ - behavior abstraction
86
+ - concrete behavior modules
87
+ - rule-based policies
88
+ - contextual bandit policy scaffold
89
+ - Pydantic DSL schema
90
+ - JSON parser and semantic validator
91
+ - WorldFactory for DSL → runtime conversion
92
+ - metrics for diversity, entropy, stability, and interestingness
93
+ - Matplotlib renderer
94
+ - GIF/MP4 animation support
95
+ - chart generation
96
+ - deterministic narration
97
+ - root-level Gradio app
98
+ - CLI runner
99
+ - example DSL worlds
100
+
101
+ The system works without an external LLM by using a deterministic fallback world generator. If a model is configured, the model is only responsible for generating JSON DSL, never Python code.
102
+
103
+ ---
104
+
105
+ ## Repository Layout
106
+
107
+ ```text
108
+ WorldSmithAI/
109
+ ├── app.py
110
+ ├── callbacks.py
111
+ ├── main.py
112
+ ├── README.md
113
+ ├── requirements.txt
114
+ │
115
+ ├── core/
116
+ │ ├── agent.py
117
+ │ ├── behavior.py
118
+ │ ├── resource.py
119
+ │ ├── event.py
120
+ │ ├── world.py
121
+ │ └── scheduler.py
122
+ │
123
+ ├── behaviors/
124
+ │ ├── movement.py
125
+ │ ├── consume.py
126
+ │ ├── trade.py
127
+ │ ├── attack.py
128
+ │ ├── research.py
129
+ │ ├── collaboration.py
130
+ │ ├── transport.py
131
+ │ ├── construction.py
132
+ │ ├── governance.py
133
+ │ ├── market.py
134
+ │ ├── adoption.py
135
+ │ ├── planning.py
136
+ │ └── memory.py
137
+ │
138
+ ├── policies/
139
+ │ ├── base_policy.py
140
+ │ ├── rule_policy.py
141
+ │ └── contextual_bandit.py
142
+ │
143
+ ├── dsl/
144
+ │ ├── schema.py
145
+ │ ├── parser.py
146
+ │ └── validator.py
147
+ │
148
+ ├── factory/
149
+ │ └── world_factory.py
150
+ │
151
+ ├── metrics/
152
+ │ ├── diversity.py
153
+ │ ├── entropy.py
154
+ │ ├── stability.py
155
+ │ └── interestingness.py
156
+ │
157
+ ├── visualization/
158
+ │ ├── renderer.py
159
+ │ ├── animation.py
160
+ │ └── charts.py
161
+ │
162
+ ├── llm/
163
+ │ ├── world_generator.py
164
+ │ ├── narrator.py
165
+ │ └── prompts.py
166
+ │
167
+ └── examples/
168
+ ├── farm.json
169
+ ├── civilization.json
170
+ └── research.json
171
+ ```
172
+
173
+ `app.py` is intentionally at the repository root because Hugging Face Gradio Spaces expect the app entry point there.
174
+
175
+ ---
176
+
177
+ ## Core Design Principles
178
+
179
+ WorldSmithAI follows these principles:
180
+
181
+ 1. **No hardcoded species or domains**
182
+
183
+ A wolf, scientist, startup founder, train, dragon, or power node is just an `Agent` with a different `type`, `state`, `memory`, `goals`, `behaviors`, and `policy`.
184
+
185
+ 2. **DSL-first world definition**
186
+
187
+ Worlds are described as JSON using the `WorldSpec` schema.
188
+
189
+ 3. **LLM generates JSON only**
190
+
191
+ The model never generates Python code. The Python engine remains deterministic.
192
+
193
+ 4. **Composition over inheritance**
194
+
195
+ Agent behavior comes from behavior objects and policies, not subclass-specific logic.
196
+
197
+ 5. **Strong module boundaries**
198
+
199
+ Parsing, validation, factory construction, simulation, visualization, metrics, narration, and UI are separate layers.
200
+
201
+ 6. **Gradio app is thin**
202
+
203
+ `app.py` defines the UI. `callbacks.py` runs the engine pipeline.
204
+
205
+ ---
206
+
207
+ ## Installation
208
+
209
+ ### 1. Clone the repository
210
+
211
+ ```bash
212
+ git clone <your-repo-url>
213
+ cd WorldSmithAI
214
+ ```
215
+
216
+ ### 2. Create a virtual environment
217
+
218
+ Python 3.11 or newer is recommended.
219
+
220
+ ```bash
221
+ python -m venv .venv
222
+ ```
223
+
224
+ Activate it:
225
+
226
+ ```bash
227
+ source .venv/bin/activate
228
+ ```
229
+
230
+ On Windows PowerShell:
231
+
232
+ ```powershell
233
+ .venv\Scripts\Activate.ps1
234
+ ```
235
+
236
+ ### 3. Install dependencies
237
+
238
+ If your project already has a `requirements.txt`, run:
239
+
240
+ ```bash
241
+ python -m pip install --upgrade pip
242
+ python -m pip install -r requirements.txt
243
+ ```
244
+
245
+ If you have not created `requirements.txt` yet, use this minimal set:
246
+
247
+ ```bash
248
+ python -m pip install --upgrade pip
249
+ python -m pip install gradio pydantic numpy matplotlib pillow huggingface_hub
250
+ ```
251
+
252
+ Optional development tools:
253
+
254
+ ```bash
255
+ python -m pip install pytest ruff mypy
256
+ ```
257
+
258
+ Optional MP4 support requires `ffmpeg` installed on the system. GIF output works through Pillow and is the safer default.
259
+
260
+ ---
261
+
262
+ ## Suggested `requirements.txt`
263
+
264
+ Create this file at the repository root:
265
+
266
+ ```text
267
+ gradio
268
+ pydantic>=2
269
+ numpy
270
+ matplotlib
271
+ pillow
272
+ huggingface_hub
273
+ ```
274
+
275
+ Optional dev dependencies can be placed in `requirements-dev.txt`:
276
+
277
+ ```text
278
+ pytest
279
+ ruff
280
+ mypy
281
+ ```
282
+
283
+ ---
284
+
285
+ ## Can This Be Tested Locally?
286
+
287
+ Yes. WorldSmithAI can be tested locally without Hugging Face Spaces and without an external LLM.
288
+
289
+ The deterministic fallback generator allows this command to work even if no model is configured:
290
+
291
+ ```bash
292
+ python main.py --prompt "A research ecosystem with scientists, reviewers, funding agents, and knowledge resources" --steps 10 --no-animation
293
+ ```
294
+
295
+ For a faster smoke test, disable all visual outputs:
296
+
297
+ ```bash
298
+ python main.py \
299
+ --prompt "A tiny farm ecosystem with farmers, water, crops, and soil health" \
300
+ --steps 5 \
301
+ --no-animation \
302
+ --no-charts \
303
+ --no-final-image \
304
+ --no-narrative
305
+ ```
306
+
307
+ If that completes, your parser, schema, world generation fallback, validation, factory, scheduler, policies, behaviors, and metrics are at least importable and executable.
308
+
309
+ ---
310
+
311
+ ## Local Smoke Test Checklist
312
+
313
+ Run these from the repository root.
314
+
315
+ ### 1. Check Python syntax
316
+
317
+ ```bash
318
+ python -m compileall .
319
+ ```
320
+
321
+ This catches syntax errors across all modules.
322
+
323
+ ### 2. List example worlds
324
+
325
+ ```bash
326
+ python main.py --list-examples
327
+ ```
328
+
329
+ Expected output should include some or all of:
330
+
331
+ ```text
332
+ farm
333
+ civilization
334
+ research
335
+ ```
336
+
337
+ ### 3. Validate an example DSL file
338
+
339
+ ```bash
340
+ python main.py --example research --validate-only
341
+ ```
342
+
343
+ Or directly:
344
+
345
+ ```bash
346
+ python main.py --dsl examples/research.json --validate-only
347
+ ```
348
+
349
+ This checks:
350
+
351
+ - JSON parsing
352
+ - Pydantic schema validation
353
+ - semantic DSL validation
354
+
355
+ ### 4. Run a short simulation without animation
356
+
357
+ ```bash
358
+ python main.py --example research --steps 5 --no-animation
359
+ ```
360
+
361
+ This checks:
362
+
363
+ - WorldFactory
364
+ - runtime object construction
365
+ - policies
366
+ - behaviors
367
+ - scheduler/world stepping
368
+ - metrics
369
+ - charts/final image unless disabled
370
+
371
+ ### 5. Run the fastest full-pipeline smoke test
372
+
373
+ ```bash
374
+ python main.py \
375
+ --example farm \
376
+ --steps 5 \
377
+ --no-animation \
378
+ --no-charts \
379
+ --no-final-image \
380
+ --no-narrative
381
+ ```
382
+
383
+ This is useful when debugging core engine issues.
384
+
385
+ ### 6. Run a full local artifact test
386
+
387
+ ```bash
388
+ python main.py --example research --steps 20 --output-dir outputs/research_test
389
+ ```
390
+
391
+ Expected outputs:
392
+
393
+ ```text
394
+ outputs/research_test/
395
+ ├── world_spec.json
396
+ ├── validation_report.json
397
+ ├── metrics.json
398
+ ├── narrative.md
399
+ ├── run_summary.json
400
+ ├── simulation.gif
401
+ ├── population chart image
402
+ ├── resource chart image
403
+ └── final world image
404
+ ```
405
+
406
+ Exact image filenames may depend on the callback artifact writer.
407
+
408
+ ### 7. Run the Gradio app locally
409
+
410
+ ```bash
411
+ python app.py
412
+ ```
413
+
414
+ Then open the local URL printed in the terminal, usually something like:
415
+
416
+ ```text
417
+ http://127.0.0.1:7860
418
+ ```
419
+
420
+ Try the default prompt first. Then try one of:
421
+
422
+ ```text
423
+ A medieval civilization with rulers, merchants, artisans, guards, public trust, taxes, and trade.
424
+
425
+ A startup economy with founders, customers, investors, market adoption, and shifting goals.
426
+
427
+ A fantasy world with dragons, mages, healers, mana, alliances, and negotiation.
428
+ ```
429
+
430
+ ---
431
+
432
+ ## CLI Usage
433
+
434
+ `main.py` is the local command-line entry point.
435
+
436
+ ### Run an example world
437
+
438
+ ```bash
439
+ python main.py --example research --steps 60
440
+ ```
441
+
442
+ ### Run a DSL file
443
+
444
+ ```bash
445
+ python main.py --dsl examples/farm.json --steps 80
446
+ ```
447
+
448
+ ### Generate a world from a prompt
449
+
450
+ ```bash
451
+ python main.py \
452
+ --prompt "A fantasy kingdom with dragons, mages, merchants, mana, trade, and alliances" \
453
+ --steps 70
454
+ ```
455
+
456
+ ### Validate only
457
+
458
+ ```bash
459
+ python main.py --dsl examples/civilization.json --validate-only
460
+ ```
461
+
462
+ ### Generate DSL only
463
+
464
+ ```bash
465
+ python main.py \
466
+ --prompt "A transport network with hubs, carriers, queues, chargers, and route planning" \
467
+ --generate-only
468
+ ```
469
+
470
+ ### Disable expensive outputs
471
+
472
+ ```bash
473
+ python main.py --example research --steps 20 --no-animation --no-charts
474
+ ```
475
+
476
+ ### Use MP4 animation
477
+
478
+ ```bash
479
+ python main.py --example research --steps 30 --animation-format mp4
480
+ ```
481
+
482
+ If MP4 fails, install `ffmpeg` or use GIF:
483
+
484
+ ```bash
485
+ python main.py --example research --steps 30 --animation-format gif
486
+ ```
487
+
488
+ ---
489
+
490
+ ## Gradio App Usage
491
+
492
+ Start the app:
493
+
494
+ ```bash
495
+ python app.py
496
+ ```
497
+
498
+ The UI includes:
499
+
500
+ 1. **Generate + Simulate**
501
+
502
+ Enter a natural-language world prompt. The app generates DSL, builds a world, runs simulation, and returns:
503
+
504
+ - generated JSON DSL
505
+ - validation report
506
+ - animation
507
+ - population chart
508
+ - resource chart
509
+ - final world image
510
+ - narrative summary
511
+ - metrics JSON
512
+
513
+ 2. **Generate DSL only**
514
+
515
+ Use this to inspect the generated `WorldSpec` before simulation.
516
+
517
+ 3. **Simulate existing DSL**
518
+
519
+ Paste JSON or load an example from `examples/`.
520
+
521
+ 4. **Validate DSL**
522
+
523
+ Validate a JSON world spec without running it.
524
+
525
+ ---
526
+
527
+ ## Optional Model Setup
528
+
529
+ WorldSmithAI works without an LLM by using a deterministic fallback generator.
530
+
531
+ To enable a Hugging Face model for DSL generation, set:
532
+
533
+ ```bash
534
+ export WORLDSMITHAI_MODEL_ID="your-model-id"
535
+ ```
536
+
537
+ Optional token:
538
+
539
+ ```bash
540
+ export HF_TOKEN="your-hugging-face-token"
541
+ ```
542
+
543
+ Then run:
544
+
545
+ ```bash
546
+ python app.py
547
+ ```
548
+
549
+ The model is only asked to generate WorldSpec JSON. It is not allowed to generate Python code.
550
+
551
+ If the model fails or returns invalid JSON, WorldSmithAI can fall back to deterministic generation.
552
+
553
+ ---
554
+
555
+ ## Environment Variables
556
+
557
+ | Variable | Purpose |
558
+ |---|---|
559
+ | `WORLDSMITHAI_MODEL_ID` | Optional Hugging Face model id for JSON DSL generation |
560
+ | `HF_TOKEN` | Optional Hugging Face token |
561
+ | `WORLDSMITHAI_OUTPUT_DIR` | Directory for generated artifacts |
562
+ | `WORLDSMITHAI_DEFAULT_STEPS` | Default simulation steps in the app |
563
+ | `WORLDSMITHAI_MAX_FRAMES` | Maximum animation frames |
564
+ | `WORLDSMITHAI_LOG_LEVEL` | Logging level, for example `INFO` or `DEBUG` |
565
+
566
+ Example:
567
+
568
+ ```bash
569
+ export WORLDSMITHAI_DEFAULT_STEPS=60
570
+ export WORLDSMITHAI_MAX_FRAMES=80
571
+ export WORLDSMITHAI_LOG_LEVEL=INFO
572
+ python app.py
573
+ ```
574
+
575
+ ---
576
+
577
+ ## DSL Example
578
+
579
+ A minimal world looks like this:
580
+
581
+ ```json
582
+ {
583
+ "schema_version": "1.0",
584
+ "id": "tiny_world",
585
+ "name": "Tiny World",
586
+ "description": "A minimal generic world.",
587
+ "simulation": {
588
+ "steps": 10,
589
+ "seed": 0,
590
+ "scheduler": "sequential",
591
+ "activation": "sequential",
592
+ "collect_history": true
593
+ },
594
+ "space": {
595
+ "dimensions": 2,
596
+ "bounds": [[0, 10], [0, 10]],
597
+ "toroidal": false,
598
+ "enforce_bounds": true
599
+ },
600
+ "agents": [
601
+ {
602
+ "id": "agent_1",
603
+ "type": "explorer",
604
+ "position": [1, 1],
605
+ "state": {
606
+ "energy": 10,
607
+ "credits": 5
608
+ },
609
+ "memory": {
610
+ "goals": [
611
+ {
612
+ "id": "learn",
613
+ "importance": 1,
614
+ "score": 1
615
+ }
616
+ ]
617
+ },
618
+ "goals": [
619
+ {
620
+ "id": "learn",
621
+ "importance": 1,
622
+ "score": 1
623
+ }
624
+ ],
625
+ "behaviors": [
626
+ {
627
+ "name": "prioritize",
628
+ "params": {
629
+ "source_path": "memory.goals"
630
+ }
631
+ },
632
+ {
633
+ "name": "choose_goal",
634
+ "params": {}
635
+ },
636
+ {
637
+ "name": "remember",
638
+ "params": {
639
+ "category": "initial",
640
+ "content": {
641
+ "note": "hello world"
642
+ }
643
+ }
644
+ }
645
+ ],
646
+ "policy": {
647
+ "type": "rule_policy",
648
+ "params": {
649
+ "rules": [
650
+ {
651
+ "behavior_name": "choose_goal",
652
+ "score_delta": 3
653
+ },
654
+ {
655
+ "behavior_name": "prioritize",
656
+ "score_delta": 2
657
+ },
658
+ {
659
+ "behavior_name": "remember",
660
+ "score_delta": 1
661
+ }
662
+ ]
663
+ }
664
+ },
665
+ "alive": true,
666
+ "metadata": {}
667
+ }
668
+ ],
669
+ "resources": [],
670
+ "events": [],
671
+ "metrics": [
672
+ {
673
+ "name": "diversity",
674
+ "params": {
675
+ "collection": "agents",
676
+ "group_by_path": "type"
677
+ }
678
+ }
679
+ ],
680
+ "metadata": {}
681
+ }
682
+ ```
683
+
684
+ ---
685
+
686
+ ## Architecture Notes
687
+
688
+ ### World generation
689
+
690
+ `llm/world_generator.py` converts prompts into `WorldSpec`.
691
+
692
+ If no model client is configured, it uses a deterministic fallback builder.
693
+
694
+ ### Parsing and validation
695
+
696
+ `dsl/parser.py` accepts:
697
+
698
+ - JSON strings
699
+ - dictionaries
700
+ - JSON files
701
+ - Markdown-fenced JSON
702
+ - model responses containing JSON
703
+
704
+ `dsl/schema.py` performs structural validation.
705
+
706
+ `dsl/validator.py` performs semantic validation, such as checking behavior names, policy names, constructor parameters, and references.
707
+
708
+ ### Runtime construction
709
+
710
+ `factory/world_factory.py` turns `WorldSpec` into runtime objects:
711
+
712
+ ```text
713
+ WorldSpec
714
+ → Agent
715
+ → Resource
716
+ → Event
717
+ → Behavior
718
+ → Policy
719
+ → World
720
+ ```
721
+
722
+ ### Simulation
723
+
724
+ The world and scheduler advance agents. Agents delegate decision-making to policies, and behaviors mutate generic state/memory.
725
+
726
+ ### Visualization
727
+
728
+ `visualization/renderer.py` renders a single world state.
729
+
730
+ `visualization/animation.py` renders GIF or MP4 animations.
731
+
732
+ `visualization/charts.py` renders population and resource curves.
733
+
734
+ ### Metrics
735
+
736
+ `metrics/` includes:
737
+
738
+ - diversity
739
+ - entropy
740
+ - stability
741
+ - interestingness
742
+
743
+ ### Narration
744
+
745
+ `llm/narrator.py` produces deterministic narrative summaries and can optionally use a model client for polished narration.
746
+
747
+ ---
748
+
749
+ ## Testing Strategy
750
+
751
+ A practical local testing ladder:
752
+
753
+ ### Level 1: Syntax
754
+
755
+ ```bash
756
+ python -m compileall .
757
+ ```
758
+
759
+ ### Level 2: DSL parsing
760
+
761
+ ```bash
762
+ python main.py --dsl examples/farm.json --validate-only
763
+ python main.py --dsl examples/civilization.json --validate-only
764
+ python main.py --dsl examples/research.json --validate-only
765
+ ```
766
+
767
+ ### Level 3: Fast simulation
768
+
769
+ ```bash
770
+ python main.py --example farm --steps 5 --no-animation --no-charts --no-final-image --no-narrative
771
+ ```
772
+
773
+ ### Level 4: Metrics and narration
774
+
775
+ ```bash
776
+ python main.py --example research --steps 10 --no-animation --no-charts --no-final-image
777
+ ```
778
+
779
+ ### Level 5: Full artifact generation
780
+
781
+ ```bash
782
+ python main.py --example civilization --steps 20 --output-dir outputs/civilization_test
783
+ ```
784
+
785
+ ### Level 6: Gradio UI
786
+
787
+ ```bash
788
+ python app.py
789
+ ```
790
+
791
+ ---
792
+
793
+ ## Troubleshooting
794
+
795
+ ### `ModuleNotFoundError`
796
+
797
+ Make sure you are running commands from the repository root:
798
+
799
+ ```bash
800
+ pwd
801
+ ```
802
+
803
+ The current directory should contain:
804
+
805
+ ```text
806
+ app.py
807
+ callbacks.py
808
+ main.py
809
+ core/
810
+ behaviors/
811
+ dsl/
812
+ factory/
813
+ metrics/
814
+ visualization/
815
+ llm/
816
+ ```
817
+
818
+ Then run:
819
+
820
+ ```bash
821
+ python -m compileall .
822
+ ```
823
+
824
+ ### `ImportError` for a behavior module
825
+
826
+ Check that the behavior file exists and that the behavior registry includes the expected names.
827
+
828
+ For example:
829
+
830
+ ```bash
831
+ python - <<'PY'
832
+ from dsl.validator import load_default_behavior_registry
833
+
834
+ result = load_default_behavior_registry()
835
+ print("Behavior count:", len(result.registry))
836
+ print("Import errors:", result.import_errors)
837
+ print("Names:", sorted(result.registry.keys())[:50])
838
+ PY
839
+ ```
840
+
841
+ ### Unknown behavior warnings
842
+
843
+ If semantic validation reports unknown behavior names, check:
844
+
845
+ 1. the behavior file exists
846
+ 2. it defines a `BEHAVIOR_REGISTRY`
847
+ 3. the behavior name in JSON matches the registry key
848
+ 4. `factory/world_factory.py` includes the behavior module in `DEFAULT_BEHAVIOR_MODULES`
849
+
850
+ ### Constructor parameter warnings
851
+
852
+ If the validator reports unknown constructor parameters, either:
853
+
854
+ - fix the JSON behavior params, or
855
+ - update the behavior dataclass to accept the parameter, or
856
+ - run in permissive mode through `callbacks.py`, which is friendlier for hackathon demos.
857
+
858
+ ### MP4 animation fails
859
+
860
+ Use GIF instead:
861
+
862
+ ```bash
863
+ python main.py --example research --steps 30 --animation-format gif
864
+ ```
865
+
866
+ MP4 requires `ffmpeg` installed on the host.
867
+
868
+ ### Gradio app starts but generation is slow
869
+
870
+ Use fewer steps:
871
+
872
+ ```bash
873
+ export WORLDSMITHAI_DEFAULT_STEPS=20
874
+ export WORLDSMITHAI_MAX_FRAMES=30
875
+ python app.py
876
+ ```
877
+
878
+ Or disable model usage in the UI and use deterministic fallback generation.
879
+
880
+ ### Model returns invalid JSON
881
+
882
+ The parser can extract JSON from common model response formats, but small models may still produce invalid JSON.
883
+
884
+ Use:
885
+
886
+ - the deterministic fallback generator
887
+ - “Generate DSL only” tab
888
+ - “Validate DSL” tab
889
+ - simpler prompts
890
+ - fewer agents
891
+
892
+ ---
893
+
894
+ ## Hugging Face Space Notes
895
+
896
+ For a Gradio Space, keep these files at the repository root:
897
+
898
+ ```text
899
+ app.py
900
+ callbacks.py
901
+ requirements.txt
902
+ ```
903
+
904
+ The Space should install dependencies from `requirements.txt`.
905
+
906
+ Recommended Space hardware for the deterministic version is CPU basic. If you use a larger model locally inside the Space, choose hardware appropriate for that model.
907
+
908
+ ---
909
+
910
+ ## Development Tips
911
+
912
+ Run formatting and lint checks if you install dev tools:
913
+
914
+ ```bash
915
+ ruff check .
916
+ ```
917
+
918
+ Run type checks if your environment is ready:
919
+
920
+ ```bash
921
+ mypy .
922
+ ```
923
+
924
+ Run a quick no-UI smoke test before pushing:
925
+
926
+ ```bash
927
+ python main.py --example research --steps 5 --no-animation --no-charts --no-final-image --no-narrative
928
+ ```
929
+
930
+ Then run the app:
931
+
932
+ ```bash
933
+ python app.py
934
+ ```
935
+
936
+ ---
937
+
938
+ ## Hackathon Demo Script
939
+
940
+ A good demo flow:
941
+
942
+ 1. Open the app.
943
+ 2. Enter a world prompt, for example:
944
+
945
+ ```text
946
+ A startup economy where founders, investors, customers, and competitors bid for attention,
947
+ adopt strategies, collaborate, forecast demand, and rebalance resources.
948
+ ```
949
+
950
+ 3. Run simulation for 40 to 60 steps.
951
+ 4. Show the generated DSL.
952
+ 5. Show the animation.
953
+ 6. Show population and resource charts.
954
+ 7. Show metrics JSON.
955
+ 8. Read the narrative summary.
956
+ 9. Explain that the model generated only JSON, while the deterministic Python engine executed the simulation.
957
+
958
+ ---
959
+
960
+ ## Current Limitations
961
+
962
+ WorldSmithAI is designed as a hackathon-grade but production-oriented framework. Some areas are intentionally extensible:
963
+
964
+ - event execution is generic and can be expanded
965
+ - richer scheduler activation modes can be added
966
+ - metric registry integration can be expanded
967
+ - behavior parameter schemas can be tightened
968
+ - model-specific generation adapters can be added
969
+ - graph/network visualization can be added
970
+ - multi-run comparison can be added
971
+ - unit tests should be expanded as the codebase stabilizes
972
+
973
+ ---
974
+
975
+
976
+
977
+ ## Acknowledgments
978
+
979
+ Built for the Hugging Face Build Small Hackathon.
980
+
981
+ WorldSmithAI explores how small language models can generate compact, validated world DSLs while deterministic Python systems execute and visualize the resulting simulations.