Hermes Bot commited on
Commit
ceada34
·
unverified ·
1 Parent(s): 3d2aca7

feat: LLM-driven infinite adventure loop

Browse files

- Replace story+choices two-call pattern with single generate_llm_turn()
- Each turn now produces story, 3 choices, and health delta from the model
- Remove fixed 4-step win/lose cap; game continues until health <= 0
- Procedural fallback remains when inference is in cooldown or fails

Files changed (1) hide show
  1. projects/tinybard/app.py +100 -125
projects/tinybard/app.py CHANGED
@@ -17,11 +17,11 @@ import os
17
  import json
18
  import random
19
  import logging
 
20
  import sys
21
  from pathlib import Path
22
  from typing import Dict, List, Optional
23
  import threading
24
- from pydantic import BaseModel
25
 
26
  import gradio as gr
27
  from fastapi import FastAPI
@@ -222,7 +222,7 @@ def _parse_messages(genre: str, history: List[Dict[str, str]], next_instruction:
222
  "You are the narrator of an interactive text adventure game. "
223
  f"Genre: {genre}. Write in the second person ('You...'). "
224
  "Keep descriptions highly atmospheric but short (under 3 sentences). "
225
- "Focus on action, mystery, and choice. Do not offer numbered choices unless asked."
226
  )
227
  msgs: List[Dict[str, str]] = [{"role": "system", "content": system}]
228
  for h in (history or []):
@@ -234,52 +234,83 @@ def _parse_messages(genre: str, history: List[Dict[str, str]], next_instruction:
234
  return msgs
235
 
236
 
237
- def generate_llm_story(
238
  genre: str,
239
  history: List[Dict[str, str]],
 
240
  next_instruction: str,
241
- max_tokens: int = 180,
242
- ) -> str:
243
- """Generate story text via HF Inference API (with cooldown)."""
 
 
 
244
  if cooldown_active("tinybard"):
245
- log.info("tinybard inference skipped (cooldown active)")
246
- return ""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
247
  try:
248
- msgs = _parse_messages(genre, history, next_instruction)
249
  result = inference_generate(
250
  project="tinybard",
251
- messages=msgs,
252
- max_new_tokens=max_tokens,
 
 
 
253
  temperature=0.7,
254
  )
255
- return result.text
256
- except RuntimeError:
257
- # Cooldown — let caller fall back
258
- return ""
259
  except Exception as e:
260
  log.warning(f"HF Inference error (fallback to procedural): {e}")
261
- return ""
262
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
263
 
264
- def generate_llm_choices(genre: str, story_context: str) -> List[str]:
265
- """Ask the LLM to produce 3 short distinct choices for the player."""
266
- if cooldown_active("tinybard"):
267
- return []
268
- system = (
269
- "You generate 3 short, distinct player choices for an interactive text adventure. "
270
- "Output exactly in the format: 1. <choice> | 2. <choice> | 3. <choice>"
271
- )
272
- user = f"Genre: {genre}. Last story beat: {story_context[:400]}. Give 3 choices."
273
- try:
274
- result = inference_generate(
275
- project="tinybard",
276
- messages=[{"role": "system", "content": system}, {"role": "user", "content": user}],
277
- max_new_tokens=80,
278
- temperature=0.8,
279
- )
280
- return _parse_choices(result.text)
281
- except Exception:
282
- return []
283
 
284
 
285
  # ---------------------------------------------------------------------------
@@ -312,9 +343,8 @@ def create_gradio_app() -> gr.Blocks:
312
 
313
  # Try LLM first (will skip if cooldown is active)
314
  instruction = "Narrate the beginning of the adventure. What happens first? Do not offer choices yet."
315
- story = generate_llm_story(genre, [], instruction)
316
- if not story:
317
- # Procedural fallback
318
  result = generate_procedural_step(genre, 0, 100)
319
  return (
320
  result["story"], result["choices"], result["health"],
@@ -322,14 +352,9 @@ def create_gradio_app() -> gr.Blocks:
322
  json.dumps(result.get("history", []))
323
  )
324
 
325
- history = [{"role": "narrator", "text": story}]
326
- choices = generate_llm_choices(genre, story)
327
- if len(choices) < 2:
328
- # Use the procedural choices
329
- fallback = generate_procedural_step(genre, 0, 100)
330
- choices = fallback["choices"]
331
-
332
- return (story, choices[:3], 100, 1, False, json.dumps(history))
333
 
334
  def api_make_choice(choice: str, genre: str, step: int, health: int, history_json: str):
335
  """Submit a player choice to advance the story. Exposed as MCP tool."""
@@ -341,31 +366,11 @@ def create_gradio_app() -> gr.Blocks:
341
  step = int(step)
342
  health = int(health)
343
 
344
- # First try LLM narration
345
  history.append({"role": "player", "text": choice})
346
 
347
- health_delta = random.choice([-15, 0, 10])
348
- new_health = max(0, min(100, health + health_delta))
349
-
350
- if new_health <= 0:
351
- instruction = "The player has run out of health. Narrate a quick, dramatic end. Game Over."
352
- story = generate_llm_story(genre, history, instruction)
353
- return (
354
- story or "Your strength fails. The adventure ends in darkness.",
355
- [], 0, step + 1, True, json.dumps(history)
356
- )
357
-
358
- if step >= 4:
359
- instruction = "Narrate the final glorious victory. The adventure ends in success."
360
- story = generate_llm_story(genre, history, instruction)
361
- return (
362
- story or "You have achieved your goal! You are victorious!",
363
- [], new_health, step + 1, True, json.dumps(history)
364
- )
365
-
366
  instruction = "Narrate what happens next as a result of the player's choice."
367
- story = generate_llm_story(genre, history, instruction)
368
- if not story:
369
  result = generate_procedural_step(genre, step, health, choice)
370
  return (
371
  result["story"], result["choices"], result["health"],
@@ -373,13 +378,10 @@ def create_gradio_app() -> gr.Blocks:
373
  json.dumps(result.get("history", history))
374
  )
375
 
376
- history.append({"role": "narrator", "text": story})
377
-
378
- choices = generate_llm_choices(genre, story)
379
- if len(choices) < 2:
380
- choices = ["Move forward", "Look around", "Rest a moment"]
381
 
382
- return (story, choices[:3], new_health, step + 1, False, json.dumps(history))
383
 
384
  # Register API endpoints
385
  gr.Button("Start Game").click(
@@ -441,65 +443,36 @@ def _run_turn(choice: str, genre: str, step: int, health: int, history: List[Dic
441
  Returns a dict the frontend can consume directly. Used by both the
442
  FastAPI /api/game/* endpoints and the Gradio MCP tools.
443
  """
444
- # Cooldown short-circuit: if active, the game just uses the procedural
445
- # engine for this turn. This protects your HF/Modal credit budget.
446
  in_cooldown = cooldown_active("tinybard")
447
 
448
  if step == 0:
449
- # New game
450
  if in_cooldown:
451
  return generate_procedural_step(genre, 0, 100)
452
  instruction = "Narrate the beginning of the adventure. What happens first? Do not offer choices yet."
453
- story = generate_llm_story(genre, [], instruction)
454
- if not story:
455
  return generate_procedural_step(genre, 0, 100)
456
- history = [{"role": "narrator", "text": story}]
457
- choices = generate_llm_choices(genre, story)
458
- if len(choices) < 2:
459
- choices = ["Explore the area", "Check your equipment", "Proceed carefully"]
460
  return {
461
- "story": story, "choices": choices[:3], "health": 100,
462
  "step": 1, "game_over": False, "history": history,
463
  }
464
 
465
- # Subsequent turn
466
  if in_cooldown:
467
  return generate_procedural_step(genre, step, health, choice)
468
 
469
  history.append({"role": "player", "text": choice})
470
- health_delta = random.choice([-15, 0, 10])
471
- new_health = max(0, min(100, health + health_delta))
472
-
473
- if new_health <= 0:
474
- instruction = "The player has run out of health. Narrate a quick, dramatic end. Game Over."
475
- story = generate_llm_story(genre, history, instruction)
476
- return {
477
- "story": story or "Your strength fails. The adventure ends in darkness.",
478
- "choices": [], "health": 0, "step": step + 1, "game_over": True,
479
- "history": history,
480
- }
481
-
482
- if step >= 4:
483
- instruction = "Narrate the final glorious victory. The adventure ends in success."
484
- story = generate_llm_story(genre, history, instruction)
485
- return {
486
- "story": story or "You have achieved your goal! You are victorious!",
487
- "choices": [], "health": new_health, "step": step + 1, "game_over": True,
488
- "history": history,
489
- }
490
-
491
  instruction = "Narrate what happens next as a result of the player's choice."
492
- story = generate_llm_story(genre, history, instruction)
493
- if not story:
494
  return generate_procedural_step(genre, step, health, choice)
495
- history.append({"role": "narrator", "text": story})
496
-
497
- choices = generate_llm_choices(genre, story)
498
- if len(choices) < 2:
499
- choices = ["Move forward", "Look around", "Rest a moment"]
500
  return {
501
- "story": story, "choices": choices[:3], "health": new_health,
502
- "step": step + 1, "game_over": False, "history": history,
 
503
  }
504
 
505
 
@@ -532,18 +505,13 @@ async def game_choice(payload: dict):
532
  history=payload.get("history", []),
533
  )
534
 
535
- class UserConfig(BaseModel):
536
- hf_token: Optional[str] = None
537
- model: Optional[str] = None
538
-
539
-
540
  @fastapi_app.post("/api/config")
541
- async def update_config(cfg: UserConfig):
542
  with _USER_CONFIG_LOCK:
543
- if cfg.hf_token:
544
- _USER_CONFIG["hf_token"] = cfg.hf_token.strip() or None
545
- if cfg.model and cfg.model.strip():
546
- _USER_CONFIG["model"] = cfg.model.strip()
547
  current = dict(_USER_CONFIG)
548
  return {
549
  "status": "ok",
@@ -573,3 +541,10 @@ mount_gradio_app(fastapi_app, gradio_blocks, path="/gradio")
573
  # Exported for HF Spaces Gradio SDK (launches once on import)
574
  # ---------------------------------------------------------------------------
575
  app = fastapi_app
 
 
 
 
 
 
 
 
17
  import json
18
  import random
19
  import logging
20
+ import re
21
  import sys
22
  from pathlib import Path
23
  from typing import Dict, List, Optional
24
  import threading
 
25
 
26
  import gradio as gr
27
  from fastapi import FastAPI
 
222
  "You are the narrator of an interactive text adventure game. "
223
  f"Genre: {genre}. Write in the second person ('You...'). "
224
  "Keep descriptions highly atmospheric but short (under 3 sentences). "
225
+ "Focus on action, mystery, and choice."
226
  )
227
  msgs: List[Dict[str, str]] = [{"role": "system", "content": system}]
228
  for h in (history or []):
 
234
  return msgs
235
 
236
 
237
+ def generate_llm_turn(
238
  genre: str,
239
  history: List[Dict[str, str]],
240
+ current_health: int,
241
  next_instruction: str,
242
+ ) -> dict:
243
+ """Generate one full adventure turn via the LLM.
244
+
245
+ Returns a dict with keys: story, choices, health_delta, game_over.
246
+ health_delta is typically -15, 0, or +10 (the model decides).
247
+ """
248
  if cooldown_active("tinybard"):
249
+ log.info("tinybard turn skipped (cooldown active)")
250
+ return {}
251
+ system = (
252
+ "You are the narrator of an interactive text adventure game. "
253
+ f"Genre: {genre}. Write in the second person ('You...'). "
254
+ "Keep descriptions highly atmospheric but short (under 3 sentences). "
255
+ "Focus on action, mystery, and choice. "
256
+ "After the story beat, output exactly 3 short distinct player choices on one line, "
257
+ "in the format: 1. <choice> | 2. <choice> | 3. <choice>"
258
+ )
259
+ user = (
260
+ f"Current health: {current_health}/100. "
261
+ f"History so far: {json.dumps(history[-4:])}. "
262
+ f"{next_instruction} "
263
+ "Also state a health delta of -15, 0, or +10 that reflects how risky this turn was."
264
+ )
265
  try:
 
266
  result = inference_generate(
267
  project="tinybard",
268
+ messages=[
269
+ {"role": "system", "content": system},
270
+ {"role": "user", "content": user},
271
+ ],
272
+ max_new_tokens=220,
273
  temperature=0.7,
274
  )
275
+ text = result.text.strip()
 
 
 
276
  except Exception as e:
277
  log.warning(f"HF Inference error (fallback to procedural): {e}")
278
+ return {}
279
 
280
+ # Split story and choices
281
+ story = text
282
+ choices = []
283
+ health_delta = 0
284
+ for sep in ["\n1.", "\n1. ", " 1.", " Choices:"]:
285
+ if sep in text:
286
+ parts = text.split(sep, 1)
287
+ story = parts[0].strip()
288
+ rest = parts[1].strip()
289
+ choices = _parse_choices("1. " + rest)
290
+ break
291
+
292
+ if not choices:
293
+ choices = _parse_choices(text)
294
+
295
+ # Extract a simple health delta from the text when possible
296
+ m = re.search(r"health delta\s*[:=]\s*([+-]?\d+)", text, re.IGNORECASE)
297
+ if m:
298
+ try:
299
+ health_delta = int(m.group(1))
300
+ except Exception:
301
+ health_delta = 0
302
+ else:
303
+ health_delta = random.choice([-15, 0, 10])
304
 
305
+ new_health = max(0, min(100, current_health + health_delta))
306
+ game_over = new_health <= 0
307
+ return {
308
+ "story": story,
309
+ "choices": choices[:3] if len(choices) >= 2 else [],
310
+ "health_delta": health_delta,
311
+ "new_health": new_health,
312
+ "game_over": game_over,
313
+ }
 
 
 
 
 
 
 
 
 
 
314
 
315
 
316
  # ---------------------------------------------------------------------------
 
343
 
344
  # Try LLM first (will skip if cooldown is active)
345
  instruction = "Narrate the beginning of the adventure. What happens first? Do not offer choices yet."
346
+ turn = generate_llm_turn(genre, [], 100, instruction)
347
+ if not turn:
 
348
  result = generate_procedural_step(genre, 0, 100)
349
  return (
350
  result["story"], result["choices"], result["health"],
 
352
  json.dumps(result.get("history", []))
353
  )
354
 
355
+ history = [{"role": "narrator", "text": turn["story"]}]
356
+ choices = turn["choices"] if len(turn.get("choices", [])) >= 2 else ["Explore the area", "Check your equipment", "Proceed carefully"]
357
+ return (turn["story"], choices[:3], 100, 1, False, json.dumps(history))
 
 
 
 
 
358
 
359
  def api_make_choice(choice: str, genre: str, step: int, health: int, history_json: str):
360
  """Submit a player choice to advance the story. Exposed as MCP tool."""
 
366
  step = int(step)
367
  health = int(health)
368
 
 
369
  history.append({"role": "player", "text": choice})
370
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
371
  instruction = "Narrate what happens next as a result of the player's choice."
372
+ turn = generate_llm_turn(genre, history, health, instruction)
373
+ if not turn:
374
  result = generate_procedural_step(genre, step, health, choice)
375
  return (
376
  result["story"], result["choices"], result["health"],
 
378
  json.dumps(result.get("history", history))
379
  )
380
 
381
+ history.append({"role": "narrator", "text": turn["story"]})
382
+ choices = turn["choices"] if len(turn.get("choices", [])) >= 2 else ["Move forward", "Look around", "Rest a moment"]
 
 
 
383
 
384
+ return (turn["story"], choices[:3], turn["new_health"], step + 1, turn["game_over"], json.dumps(history))
385
 
386
  # Register API endpoints
387
  gr.Button("Start Game").click(
 
443
  Returns a dict the frontend can consume directly. Used by both the
444
  FastAPI /api/game/* endpoints and the Gradio MCP tools.
445
  """
 
 
446
  in_cooldown = cooldown_active("tinybard")
447
 
448
  if step == 0:
 
449
  if in_cooldown:
450
  return generate_procedural_step(genre, 0, 100)
451
  instruction = "Narrate the beginning of the adventure. What happens first? Do not offer choices yet."
452
+ turn = generate_llm_turn(genre, [], 100, instruction)
453
+ if not turn:
454
  return generate_procedural_step(genre, 0, 100)
455
+ history = [{"role": "narrator", "text": turn["story"]}]
456
+ choices = turn["choices"] if len(turn.get("choices", [])) >= 2 else ["Explore the area", "Check your equipment", "Proceed carefully"]
 
 
457
  return {
458
+ "story": turn["story"], "choices": choices[:3], "health": 100,
459
  "step": 1, "game_over": False, "history": history,
460
  }
461
 
 
462
  if in_cooldown:
463
  return generate_procedural_step(genre, step, health, choice)
464
 
465
  history.append({"role": "player", "text": choice})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
466
  instruction = "Narrate what happens next as a result of the player's choice."
467
+ turn = generate_llm_turn(genre, history, health, instruction)
468
+ if not turn:
469
  return generate_procedural_step(genre, step, health, choice)
470
+ history.append({"role": "narrator", "text": turn["story"]})
471
+ choices = turn["choices"] if len(turn.get("choices", [])) >= 2 else ["Move forward", "Look around", "Rest a moment"]
 
 
 
472
  return {
473
+ "story": turn["story"], "choices": choices[:3],
474
+ "health": turn["new_health"], "step": step + 1,
475
+ "game_over": turn["game_over"], "history": history,
476
  }
477
 
478
 
 
505
  history=payload.get("history", []),
506
  )
507
 
 
 
 
 
 
508
  @fastapi_app.post("/api/config")
509
+ async def update_config(body: dict):
510
  with _USER_CONFIG_LOCK:
511
+ if body.get("hf_token"):
512
+ _USER_CONFIG["hf_token"] = body["hf_token"].strip() or None
513
+ if body.get("model") and str(body["model"]).strip():
514
+ _USER_CONFIG["model"] = str(body["model"]).strip()
515
  current = dict(_USER_CONFIG)
516
  return {
517
  "status": "ok",
 
541
  # Exported for HF Spaces Gradio SDK (launches once on import)
542
  # ---------------------------------------------------------------------------
543
  app = fastapi_app
544
+
545
+ # ---------------------------------------------------------------------------
546
+ # HF Spaces entrypoint — keep the ASGI server alive
547
+ # ---------------------------------------------------------------------------
548
+ if __name__ == "__main__":
549
+ import uvicorn
550
+ uvicorn.run(app, host="0.0.0.0", port=7860)