Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- README.md +557 -558
- app.py +0 -0
- requirements.txt +4 -5
README.md
CHANGED
|
@@ -1,558 +1,557 @@
|
|
| 1 |
-
---
|
| 2 |
-
title: StoryWeaver
|
| 3 |
-
emoji: 📖
|
| 4 |
-
colorFrom: red
|
| 5 |
-
colorTo: purple
|
| 6 |
-
sdk: gradio
|
| 7 |
-
sdk_version:
|
| 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 |
-
python evaluation/run_evaluations.py --task
|
| 81 |
-
python evaluation/run_evaluations.py --task
|
| 82 |
-
python evaluation/run_evaluations.py --task
|
| 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 |
-
- `GameState.
|
| 120 |
-
- `GameState.
|
| 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 |
-
- call
|
| 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 |
-
- adding
|
| 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 |
-
- changing
|
| 308 |
-
- changing
|
| 309 |
-
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
-
|
| 319 |
-
-
|
| 320 |
-
- process
|
| 321 |
-
-
|
| 322 |
-
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
-
|
| 328 |
-
-
|
| 329 |
-
- changing how
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
- one
|
| 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 |
-
- [evaluation/datasets/
|
| 407 |
-
- [evaluation/datasets/
|
| 408 |
-
- [evaluation/datasets/
|
| 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 |
-
- compare
|
| 440 |
-
- compare
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
|
| 446 |
-
|
| 447 |
-
|
| 448 |
-
|
| 449 |
-
- [
|
| 450 |
-
- [
|
| 451 |
-
- [
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
|
| 458 |
-
|
| 459 |
-
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
|
| 463 |
-
|
| 464 |
-
|
| 465 |
-
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
|
| 469 |
-
|
| 470 |
-
|
| 471 |
-
|
| 472 |
-
|
| 473 |
-
|
| 474 |
-
|
| 475 |
-
|
| 476 |
-
|
| 477 |
-
|
| 478 |
-
|
| 479 |
-
|
| 480 |
-
|
| 481 |
-
|
| 482 |
-
|
| 483 |
-
|
| 484 |
-
|
| 485 |
-
|
| 486 |
-
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
|
| 490 |
-
|
| 491 |
-
|
| 492 |
-
|
| 493 |
-
|
| 494 |
-
|
| 495 |
-
|
| 496 |
-
|
| 497 |
-
|
| 498 |
-
|
| 499 |
-
-
|
| 500 |
-
-
|
| 501 |
-
|
| 502 |
-
|
| 503 |
-
|
| 504 |
-
|
| 505 |
-
-
|
| 506 |
-
-
|
| 507 |
-
|
| 508 |
-
|
| 509 |
-
|
| 510 |
-
|
| 511 |
-
-
|
| 512 |
-
|
| 513 |
-
|
| 514 |
-
|
| 515 |
-
|
| 516 |
-
|
| 517 |
-
|
| 518 |
-
-
|
| 519 |
-
-
|
| 520 |
-
-
|
| 521 |
-
|
| 522 |
-
|
| 523 |
-
|
| 524 |
-
|
| 525 |
-
|
| 526 |
-
|
| 527 |
-
|
| 528 |
-
|
| 529 |
-
|
| 530 |
-
|
| 531 |
-
|
| 532 |
-
|
| 533 |
-
|
| 534 |
-
|
| 535 |
-
|
| 536 |
-
|
| 537 |
-
|
| 538 |
-
|
| 539 |
-
|
| 540 |
-
|
| 541 |
-
|
| 542 |
-
-
|
| 543 |
-
-
|
| 544 |
-
-
|
| 545 |
-
-
|
| 546 |
-
|
| 547 |
-
|
| 548 |
-
|
| 549 |
-
|
| 550 |
-
-
|
| 551 |
-
-
|
| 552 |
-
-
|
| 553 |
-
-
|
| 554 |
-
|
| 555 |
-
|
| 556 |
-
|
| 557 |
-
|
| 558 |
-
MIT
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: StoryWeaver
|
| 3 |
+
emoji: 📖
|
| 4 |
+
colorFrom: red
|
| 5 |
+
colorTo: purple
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 6.7.0
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: false
|
| 10 |
+
license: mit
|
| 11 |
+
short_description: Interactive NLP story engine with evaluation and logging
|
| 12 |
+
---
|
| 13 |
+
|
| 14 |
+
# StoryWeaver
|
| 15 |
+
|
| 16 |
+
StoryWeaver is an interactive text-adventure system built for our NLP course project. The repo is structured as an engineering project first and a demo second: it contains the playable app, the state-management core, evaluation scripts, and logging utilities needed for report writing and team collaboration.
|
| 17 |
+
|
| 18 |
+
This README is written for teammates who need to:
|
| 19 |
+
|
| 20 |
+
- understand how the system is organized
|
| 21 |
+
- run the app locally
|
| 22 |
+
- know where to change prompts, rules, or UI
|
| 23 |
+
- collect evaluation results for the report
|
| 24 |
+
- debug a bad interaction without reading the whole codebase first
|
| 25 |
+
|
| 26 |
+
## What This Repository Contains
|
| 27 |
+
|
| 28 |
+
At a high level, the project has five responsibilities:
|
| 29 |
+
|
| 30 |
+
1. parse player input into structured intent
|
| 31 |
+
2. keep the world state consistent across turns
|
| 32 |
+
3. generate the next story response and options
|
| 33 |
+
4. expose the system through a Gradio UI
|
| 34 |
+
5. export logs and run reproducible evaluation
|
| 35 |
+
|
| 36 |
+
This means the repo is not only a "game demo". It is also the evidence pipeline for the course deliverables.
|
| 37 |
+
|
| 38 |
+
## Quick Start
|
| 39 |
+
|
| 40 |
+
### 1. Install dependencies
|
| 41 |
+
|
| 42 |
+
```bash
|
| 43 |
+
pip install -r requirements.txt
|
| 44 |
+
```
|
| 45 |
+
|
| 46 |
+
### 2. Create `.env`
|
| 47 |
+
|
| 48 |
+
Create a `.env` file in the project root:
|
| 49 |
+
|
| 50 |
+
```env
|
| 51 |
+
QWEN_API_KEY=your_api_key_here
|
| 52 |
+
```
|
| 53 |
+
|
| 54 |
+
Optional:
|
| 55 |
+
|
| 56 |
+
```env
|
| 57 |
+
STORYWEAVER_LOG_DIR=logs/interactions
|
| 58 |
+
```
|
| 59 |
+
|
| 60 |
+
### 3. Run the app
|
| 61 |
+
|
| 62 |
+
```bash
|
| 63 |
+
python app.py
|
| 64 |
+
```
|
| 65 |
+
|
| 66 |
+
Default local URL:
|
| 67 |
+
|
| 68 |
+
- `http://localhost:7860`
|
| 69 |
+
|
| 70 |
+
### 4. Run evaluation
|
| 71 |
+
|
| 72 |
+
```bash
|
| 73 |
+
python evaluation/run_evaluations.py --task all --repeats 3
|
| 74 |
+
```
|
| 75 |
+
|
| 76 |
+
Useful variants:
|
| 77 |
+
|
| 78 |
+
```bash
|
| 79 |
+
python evaluation/run_evaluations.py --task intent
|
| 80 |
+
python evaluation/run_evaluations.py --task consistency
|
| 81 |
+
python evaluation/run_evaluations.py --task latency --repeats 5
|
| 82 |
+
python evaluation/run_evaluations.py --task branch
|
| 83 |
+
```
|
| 84 |
+
|
| 85 |
+
## Deploy to Hugging Face Spaces (Web Upload)
|
| 86 |
+
|
| 87 |
+
If you want to deploy quickly without using git commands, use this checklist:
|
| 88 |
+
|
| 89 |
+
1. Create a new Space on Hugging Face:
|
| 90 |
+
- SDK: `Gradio`
|
| 91 |
+
- Python version: default is fine (3.10+)
|
| 92 |
+
2. Upload project files from this repository root.
|
| 93 |
+
3. Do **not** upload local-only files/directories:
|
| 94 |
+
- `venv/`, `.venv/`, `.env`, `__pycache__/`, `.gradio/`, `logs/`, `evaluation/results/`
|
| 95 |
+
4. In Space settings, add secret:
|
| 96 |
+
- `QWEN_API_KEY`
|
| 97 |
+
5. Wait for build to finish, then open the Space URL.
|
| 98 |
+
|
| 99 |
+
This repository already uses the standard Gradio entrypoint in `app.py`, so Spaces will start the app automatically.
|
| 100 |
+
|
| 101 |
+
## Recommended Reading Order
|
| 102 |
+
|
| 103 |
+
If you are new to the repo, read files in this order:
|
| 104 |
+
|
| 105 |
+
1. [state_manager.py](./state_manager.py)
|
| 106 |
+
Why: this is the single source of truth for player state, world state, quests, items, consistency checks, and state updates.
|
| 107 |
+
2. [nlu_engine.py](./nlu_engine.py)
|
| 108 |
+
Why: this shows how raw player text becomes structured intent.
|
| 109 |
+
3. [story_engine.py](./story_engine.py)
|
| 110 |
+
Why: this is the main generation pipeline and fallback logic.
|
| 111 |
+
4. [app.py](./app.py)
|
| 112 |
+
Why: this connects the UI with the engines and now also writes interaction logs.
|
| 113 |
+
5. [evaluation/run_evaluations.py](./evaluation/run_evaluations.py)
|
| 114 |
+
Why: this shows how we measure the system for the report.
|
| 115 |
+
|
| 116 |
+
If you only have 10 minutes, start with:
|
| 117 |
+
|
| 118 |
+
- `GameState.pre_validate_action`
|
| 119 |
+
- `GameState.check_consistency`
|
| 120 |
+
- `GameState.apply_changes`
|
| 121 |
+
- `NLUEngine.parse_intent`
|
| 122 |
+
- `StoryEngine.generate_story_stream`
|
| 123 |
+
- `process_user_input` in [app.py](./app.py)
|
| 124 |
+
|
| 125 |
+
## Repository Map
|
| 126 |
+
|
| 127 |
+
```text
|
| 128 |
+
StoryWeaver/
|
| 129 |
+
|-- app.py
|
| 130 |
+
|-- nlu_engine.py
|
| 131 |
+
|-- story_engine.py
|
| 132 |
+
|-- state_manager.py
|
| 133 |
+
|-- telemetry.py
|
| 134 |
+
|-- utils.py
|
| 135 |
+
|-- requirements.txt
|
| 136 |
+
|-- evaluation/
|
| 137 |
+
| |-- run_evaluations.py
|
| 138 |
+
| |-- datasets/
|
| 139 |
+
| `-- results/
|
| 140 |
+
`-- logs/
|
| 141 |
+
`-- interactions/
|
| 142 |
+
```
|
| 143 |
+
|
| 144 |
+
Core responsibilities by file:
|
| 145 |
+
|
| 146 |
+
- [app.py](./app.py)
|
| 147 |
+
Gradio app, session lifecycle, UI callbacks, per-turn logging.
|
| 148 |
+
- [state_manager.py](./state_manager.py)
|
| 149 |
+
Player/world models, item registry, NPC registry, quest registry, state validation, consistency checks, change application.
|
| 150 |
+
- [nlu_engine.py](./nlu_engine.py)
|
| 151 |
+
Intent parsing. Uses LLM parsing when available and keyword fallback when not.
|
| 152 |
+
- [story_engine.py](./story_engine.py)
|
| 153 |
+
Opening generation, main story generation, option generation, stream handling, fallback handling, telemetry tags.
|
| 154 |
+
- [telemetry.py](./telemetry.py)
|
| 155 |
+
Session metadata and JSONL interaction log export.
|
| 156 |
+
- [utils.py](./utils.py)
|
| 157 |
+
API client setup, Qwen calls, JSON extraction, retry helpers.
|
| 158 |
+
- [evaluation/run_evaluations.py](./evaluation/run_evaluations.py)
|
| 159 |
+
Reproducible experiment runner for the report.
|
| 160 |
+
|
| 161 |
+
## System Architecture
|
| 162 |
+
|
| 163 |
+
The main runtime path is:
|
| 164 |
+
|
| 165 |
+
`Player Input -> NLU -> Validation -> Story Generation -> State Update -> UI Output -> Interaction Log`
|
| 166 |
+
|
| 167 |
+
There are two ideas that matter most in this codebase:
|
| 168 |
+
|
| 169 |
+
### 1. `GameState` is the source of truth
|
| 170 |
+
|
| 171 |
+
Almost everything meaningful lives in [state_manager.py](./state_manager.py):
|
| 172 |
+
|
| 173 |
+
- player stats
|
| 174 |
+
- location
|
| 175 |
+
- time and weather
|
| 176 |
+
- inventory and equipment
|
| 177 |
+
- quests
|
| 178 |
+
- NPC states
|
| 179 |
+
- event history
|
| 180 |
+
|
| 181 |
+
When changing gameplay, try to keep state logic here instead of scattering it across prompts and UI code.
|
| 182 |
+
|
| 183 |
+
### 2. The app is a coordinator, not the game logic
|
| 184 |
+
|
| 185 |
+
[app.py](./app.py) should mostly:
|
| 186 |
+
|
| 187 |
+
- receive user input
|
| 188 |
+
- call NLU
|
| 189 |
+
- call the story engine
|
| 190 |
+
- update the chat UI
|
| 191 |
+
- write telemetry logs
|
| 192 |
+
|
| 193 |
+
If a new feature changes game rules, it probably belongs in [state_manager.py](./state_manager.py) or [story_engine.py](./story_engine.py), not in the UI layer.
|
| 194 |
+
|
| 195 |
+
## Runtime Flow
|
| 196 |
+
|
| 197 |
+
### Text input flow
|
| 198 |
+
|
| 199 |
+
For normal text input, the path is:
|
| 200 |
+
|
| 201 |
+
1. `process_user_input` receives raw text from the UI
|
| 202 |
+
2. `NLUEngine.parse_intent` converts it into a structured intent dict
|
| 203 |
+
3. `GameState.pre_validate_action` blocks clearly invalid actions early
|
| 204 |
+
4. `StoryEngine.generate_story_stream` runs the main narrative pipeline
|
| 205 |
+
5. `GameState.check_consistency` and `apply_changes` update state
|
| 206 |
+
6. UI is refreshed with story text, options, and status panel
|
| 207 |
+
7. `_record_interaction_log` writes a JSONL record to disk
|
| 208 |
+
|
| 209 |
+
### Option click flow
|
| 210 |
+
|
| 211 |
+
Button clicks do not go through full free-text parsing. Instead:
|
| 212 |
+
|
| 213 |
+
1. the selected option is converted to an intent-like dict
|
| 214 |
+
2. the story engine processes it the same way as text input
|
| 215 |
+
3. the result is rendered and logged
|
| 216 |
+
|
| 217 |
+
This is useful because option interactions and free-text interactions now share the same evaluation and observability format.
|
| 218 |
+
|
| 219 |
+
## Main Modules in More Detail
|
| 220 |
+
|
| 221 |
+
### `state_manager.py`
|
| 222 |
+
|
| 223 |
+
This file defines:
|
| 224 |
+
|
| 225 |
+
- `PlayerState`
|
| 226 |
+
- `WorldState`
|
| 227 |
+
- `GameEvent`
|
| 228 |
+
- `GameState`
|
| 229 |
+
|
| 230 |
+
Important methods:
|
| 231 |
+
|
| 232 |
+
- `pre_validate_action`
|
| 233 |
+
Rejects obviously invalid actions before calling the model.
|
| 234 |
+
- `check_consistency`
|
| 235 |
+
Detects contradictions in proposed state changes.
|
| 236 |
+
- `apply_changes`
|
| 237 |
+
Applies state changes and returns a readable change log.
|
| 238 |
+
- `validate`
|
| 239 |
+
Makes sure the resulting state is legal.
|
| 240 |
+
- `to_prompt`
|
| 241 |
+
Serializes the current game state into prompt-ready text.
|
| 242 |
+
|
| 243 |
+
When to edit this file:
|
| 244 |
+
|
| 245 |
+
- adding new items, NPCs, quests, or locations
|
| 246 |
+
- adding deterministic rules
|
| 247 |
+
- improving consistency checks
|
| 248 |
+
- changing state serialization for prompts
|
| 249 |
+
|
| 250 |
+
### `nlu_engine.py`
|
| 251 |
+
|
| 252 |
+
This file is responsible for intent recognition.
|
| 253 |
+
|
| 254 |
+
Current behavior:
|
| 255 |
+
|
| 256 |
+
- try LLM parsing first
|
| 257 |
+
- fall back to keyword rules if parsing fails
|
| 258 |
+
- return a normalized intent dict with `parser_source`
|
| 259 |
+
|
| 260 |
+
Current intent labels include:
|
| 261 |
+
|
| 262 |
+
- `ATTACK`
|
| 263 |
+
- `TALK`
|
| 264 |
+
- `MOVE`
|
| 265 |
+
- `EXPLORE`
|
| 266 |
+
- `USE_ITEM`
|
| 267 |
+
- `TRADE`
|
| 268 |
+
- `EQUIP`
|
| 269 |
+
- `REST`
|
| 270 |
+
- `QUEST`
|
| 271 |
+
- `SKILL`
|
| 272 |
+
- `PICKUP`
|
| 273 |
+
- `FLEE`
|
| 274 |
+
- `CUSTOM`
|
| 275 |
+
|
| 276 |
+
When to edit this file:
|
| 277 |
+
|
| 278 |
+
- adding a new intent type
|
| 279 |
+
- improving keyword fallback
|
| 280 |
+
- adding target extraction logic
|
| 281 |
+
- improving low-confidence handling
|
| 282 |
+
|
| 283 |
+
### `story_engine.py`
|
| 284 |
+
|
| 285 |
+
This is the main generation module.
|
| 286 |
+
|
| 287 |
+
It currently handles:
|
| 288 |
+
|
| 289 |
+
- opening generation
|
| 290 |
+
- story generation for each turn
|
| 291 |
+
- streaming and non-streaming paths
|
| 292 |
+
- default/fallback outputs
|
| 293 |
+
- consistency-aware regeneration
|
| 294 |
+
- response telemetry such as fallback reason and engine mode
|
| 295 |
+
|
| 296 |
+
Important methods:
|
| 297 |
+
|
| 298 |
+
- `generate_opening_stream`
|
| 299 |
+
- `generate_story`
|
| 300 |
+
- `generate_story_stream`
|
| 301 |
+
- `process_option_selection_stream`
|
| 302 |
+
- `_fallback_response`
|
| 303 |
+
|
| 304 |
+
When to edit this file:
|
| 305 |
+
|
| 306 |
+
- changing prompts
|
| 307 |
+
- changing multi-stage generation logic
|
| 308 |
+
- changing fallback behavior
|
| 309 |
+
- adding generation-side telemetry
|
| 310 |
+
|
| 311 |
+
### `app.py`
|
| 312 |
+
|
| 313 |
+
This file is the UI entry point and interaction orchestrator.
|
| 314 |
+
|
| 315 |
+
Important responsibilities:
|
| 316 |
+
|
| 317 |
+
- create a new game session
|
| 318 |
+
- start and restart the app session
|
| 319 |
+
- process text input
|
| 320 |
+
- process option clicks
|
| 321 |
+
- update Gradio components
|
| 322 |
+
- write structured interaction logs
|
| 323 |
+
|
| 324 |
+
When to edit this file:
|
| 325 |
+
|
| 326 |
+
- changing UI flow
|
| 327 |
+
- adding debug panels
|
| 328 |
+
- changing how logs are written
|
| 329 |
+
- changing how outputs are displayed
|
| 330 |
+
|
| 331 |
+
### `telemetry.py`
|
| 332 |
+
|
| 333 |
+
This file handles structured log export.
|
| 334 |
+
|
| 335 |
+
It is intentionally simple and file-based:
|
| 336 |
+
|
| 337 |
+
- one session gets one JSONL file
|
| 338 |
+
- one turn becomes one JSON object line
|
| 339 |
+
|
| 340 |
+
This is useful for:
|
| 341 |
+
|
| 342 |
+
- report case studies
|
| 343 |
+
- measuring fallback rate
|
| 344 |
+
- debugging weird turns
|
| 345 |
+
- collecting examples for later evaluation
|
| 346 |
+
|
| 347 |
+
## Logging and Observability
|
| 348 |
+
|
| 349 |
+
Interaction logs are written under:
|
| 350 |
+
|
| 351 |
+
- [logs/interactions](./logs/interactions)
|
| 352 |
+
|
| 353 |
+
Each turn record includes at least:
|
| 354 |
+
|
| 355 |
+
- input source
|
| 356 |
+
- user input
|
| 357 |
+
- NLU result
|
| 358 |
+
- latency
|
| 359 |
+
- fallback metadata
|
| 360 |
+
- state changes
|
| 361 |
+
- consistency issues
|
| 362 |
+
- final output text
|
| 363 |
+
- post-turn state snapshot
|
| 364 |
+
|
| 365 |
+
Example shape:
|
| 366 |
+
|
| 367 |
+
```json
|
| 368 |
+
{
|
| 369 |
+
"timestamp": "2026-03-14T18:55:00",
|
| 370 |
+
"session_id": "sw-20260314-185500-ab12cd34",
|
| 371 |
+
"turn_index": 3,
|
| 372 |
+
"input_source": "text_input",
|
| 373 |
+
"user_input": "和村长老伯谈谈最近森林里的怪事",
|
| 374 |
+
"nlu_result": {
|
| 375 |
+
"intent": "TALK",
|
| 376 |
+
"target": "村长老伯",
|
| 377 |
+
"parser_source": "llm"
|
| 378 |
+
},
|
| 379 |
+
"latency_ms": 842.13,
|
| 380 |
+
"used_fallback": false,
|
| 381 |
+
"state_changes": {},
|
| 382 |
+
"output_text": "...",
|
| 383 |
+
"post_turn_snapshot": {
|
| 384 |
+
"location": "村庄广场"
|
| 385 |
+
}
|
| 386 |
+
}
|
| 387 |
+
```
|
| 388 |
+
|
| 389 |
+
If you need to debug a bad interaction, the fastest path is:
|
| 390 |
+
|
| 391 |
+
1. check the log file
|
| 392 |
+
2. inspect `nlu_result`
|
| 393 |
+
3. inspect `telemetry.used_fallback`
|
| 394 |
+
4. inspect `state_changes`
|
| 395 |
+
5. inspect the post-turn snapshot
|
| 396 |
+
|
| 397 |
+
## Evaluation Pipeline
|
| 398 |
+
|
| 399 |
+
Evaluation entry point:
|
| 400 |
+
|
| 401 |
+
- [evaluation/run_evaluations.py](./evaluation/run_evaluations.py)
|
| 402 |
+
|
| 403 |
+
Datasets:
|
| 404 |
+
|
| 405 |
+
- [evaluation/datasets/intent_accuracy.json](./evaluation/datasets/intent_accuracy.json)
|
| 406 |
+
- [evaluation/datasets/consistency.json](./evaluation/datasets/consistency.json)
|
| 407 |
+
- [evaluation/datasets/latency.json](./evaluation/datasets/latency.json)
|
| 408 |
+
- [evaluation/datasets/branch_divergence.json](./evaluation/datasets/branch_divergence.json)
|
| 409 |
+
|
| 410 |
+
Results:
|
| 411 |
+
|
| 412 |
+
- [evaluation/results](./evaluation/results)
|
| 413 |
+
|
| 414 |
+
### What each task measures
|
| 415 |
+
|
| 416 |
+
#### Intent
|
| 417 |
+
|
| 418 |
+
- labeled input -> predicted intent
|
| 419 |
+
- optional target matching
|
| 420 |
+
- parser source breakdown
|
| 421 |
+
- per-example latency
|
| 422 |
+
|
| 423 |
+
#### Consistency
|
| 424 |
+
|
| 425 |
+
- action guard correctness via `pre_validate_action`
|
| 426 |
+
- contradiction detection via `check_consistency`
|
| 427 |
+
|
| 428 |
+
#### Latency
|
| 429 |
+
|
| 430 |
+
- NLU latency
|
| 431 |
+
- generation latency
|
| 432 |
+
- total latency
|
| 433 |
+
- fallback rate
|
| 434 |
+
|
| 435 |
+
#### Branch divergence
|
| 436 |
+
|
| 437 |
+
- same start state, different choices
|
| 438 |
+
- compare resulting story text
|
| 439 |
+
- compare option differences
|
| 440 |
+
- compare state snapshot differences
|
| 441 |
+
|
| 442 |
+
## Common Development Tasks
|
| 443 |
+
|
| 444 |
+
### Add a new intent
|
| 445 |
+
|
| 446 |
+
You will usually need to touch:
|
| 447 |
+
|
| 448 |
+
- [nlu_engine.py](./nlu_engine.py)
|
| 449 |
+
- [state_manager.py](./state_manager.py)
|
| 450 |
+
- [story_engine.py](./story_engine.py)
|
| 451 |
+
- [evaluation/datasets/intent_accuracy.json](./evaluation/datasets/intent_accuracy.json)
|
| 452 |
+
|
| 453 |
+
Suggested checklist:
|
| 454 |
+
|
| 455 |
+
1. add the label to the NLU logic
|
| 456 |
+
2. decide whether it needs pre-validation
|
| 457 |
+
3. make sure story prompts know how to handle it
|
| 458 |
+
4. add at least a few evaluation examples
|
| 459 |
+
|
| 460 |
+
### Add a new location, NPC, quest, or item
|
| 461 |
+
|
| 462 |
+
Most of the time you only need:
|
| 463 |
+
|
| 464 |
+
- [state_manager.py](./state_manager.py)
|
| 465 |
+
|
| 466 |
+
That file contains the initial world setup and registry-style data.
|
| 467 |
+
|
| 468 |
+
### Add more evaluation cases
|
| 469 |
+
|
| 470 |
+
Edit files under:
|
| 471 |
+
|
| 472 |
+
- [evaluation/datasets](./evaluation/datasets)
|
| 473 |
+
|
| 474 |
+
This is the easiest way to improve the report without changing runtime logic.
|
| 475 |
+
|
| 476 |
+
### Investigate a strange game turn
|
| 477 |
+
|
| 478 |
+
Check in this order:
|
| 479 |
+
|
| 480 |
+
1. interaction log under `logs/interactions`
|
| 481 |
+
2. `parser_source` in the NLU result
|
| 482 |
+
3. `telemetry` in the final story result
|
| 483 |
+
4. whether `pre_validate_action` rejected or allowed the turn
|
| 484 |
+
5. whether `check_consistency` flagged anything
|
| 485 |
+
|
| 486 |
+
### Change UI behavior without touching gameplay
|
| 487 |
+
|
| 488 |
+
Edit:
|
| 489 |
+
|
| 490 |
+
- [app.py](./app.py)
|
| 491 |
+
|
| 492 |
+
Try not to put game rules in the UI layer.
|
| 493 |
+
|
| 494 |
+
## Environment Notes
|
| 495 |
+
|
| 496 |
+
### If `QWEN_API_KEY` is missing
|
| 497 |
+
|
| 498 |
+
- warning logs will appear
|
| 499 |
+
- some paths will still run through fallback logic
|
| 500 |
+
- evaluation can still execute, but model-quality conclusions are not meaningful
|
| 501 |
+
|
| 502 |
+
### If `openai` is not installed
|
| 503 |
+
|
| 504 |
+
- the repo can still import in some cases because the client is lazily initialized
|
| 505 |
+
- full Qwen generation will not work
|
| 506 |
+
- evaluation scripts will mostly reflect fallback behavior
|
| 507 |
+
|
| 508 |
+
### If `gradio` is not installed
|
| 509 |
+
|
| 510 |
+
- the app cannot launch
|
| 511 |
+
- offline evaluation scripts can still be useful
|
| 512 |
+
|
| 513 |
+
## Current Known Limitations
|
| 514 |
+
|
| 515 |
+
These are the main gaps we still know about:
|
| 516 |
+
|
| 517 |
+
- some item and equipment effects are stored as metadata but not fully executed as deterministic rules
|
| 518 |
+
- combat and trade are still more prompt-driven than rule-driven
|
| 519 |
+
- branch divergence is much more meaningful with a real model than in fallback-only mode
|
| 520 |
+
- evaluation quality depends on whether the real model environment is available
|
| 521 |
+
|
| 522 |
+
## Suggested Team Workflow
|
| 523 |
+
|
| 524 |
+
If multiple teammates are working in parallel, this split is usually clean:
|
| 525 |
+
|
| 526 |
+
- gameplay/state teammate
|
| 527 |
+
Focus on [state_manager.py](./state_manager.py)
|
| 528 |
+
- prompt/generation teammate
|
| 529 |
+
Focus on [story_engine.py](./story_engine.py)
|
| 530 |
+
- NLU/evaluation teammate
|
| 531 |
+
Focus on [nlu_engine.py](./nlu_engine.py) and [evaluation](./evaluation)
|
| 532 |
+
- UI/demo teammate
|
| 533 |
+
Focus on [app.py](./app.py)
|
| 534 |
+
- report teammate
|
| 535 |
+
Focus on `evaluation/results`, `logs/interactions`, and case-study collection
|
| 536 |
+
|
| 537 |
+
## What To Use in the Final Report
|
| 538 |
+
|
| 539 |
+
For the course report, the most useful artifacts from this repo are:
|
| 540 |
+
|
| 541 |
+
- evaluation JSON outputs under `evaluation/results`
|
| 542 |
+
- interaction logs under `logs/interactions`
|
| 543 |
+
- dataset files under `evaluation/datasets`
|
| 544 |
+
- readable state transitions from `change_log`
|
| 545 |
+
- fallback metadata from `telemetry`
|
| 546 |
+
|
| 547 |
+
These can directly support:
|
| 548 |
+
|
| 549 |
+
- experiment setup
|
| 550 |
+
- metric definition
|
| 551 |
+
- result tables
|
| 552 |
+
- success cases
|
| 553 |
+
- failure case analysis
|
| 554 |
+
|
| 555 |
+
## License
|
| 556 |
+
|
| 557 |
+
MIT
|
|
|
app.py
CHANGED
|
The diff for this file is too large to render.
See raw diff
|
|
|
requirements.txt
CHANGED
|
@@ -1,5 +1,4 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
pydantic==2.9.2
|
|
|
|
| 1 |
+
openai>=1.0.0
|
| 2 |
+
gradio==6.7.0
|
| 3 |
+
python-dotenv>=1.0.0
|
| 4 |
+
pydantic>=2.0.0
|
|
|