jonpreamble commited on
Commit
a659a92
·
1 Parent(s): b0e72b5
Files changed (4) hide show
  1. app.py +36 -18
  2. decider_questions.py +7 -0
  3. decider_utils.py +1 -6
  4. game_content.py +24 -0
app.py CHANGED
@@ -1,8 +1,9 @@
1
  import gradio as gr
2
  from game_content import GAME_INTRO, NOTES_TO_THE_NARRATOR, AWAITING_INPUT
 
3
  import decider_utils
4
  from decider_utils import YES, NO
5
-
6
 
7
 
8
 
@@ -15,14 +16,7 @@ from decider_utils import YES, NO
15
  N_COMPLETIONS_WHEN_ELABORATING = 1 # I previously had this set to 3, but that made the program very slow.
16
  MINIMUM_COMPLETION_LENGTH_CHARS_WHEN_ELABORATING = 7
17
 
18
- QUESTION_IS_USER_HOME = "At the end of the above story, is the protagonist located at their destination?"
19
- QUESTION_DOES_USER_STILL_HAVE_AT_LEAST_30_GOLD = "At the end of the above story, does the protagonist still have at least 30 gold pieces?"
20
- QUESTION_IS_USER_ENGAGED_WITH_BANDITS = "At the end of the above story, is the protagonist currently still engaged in a standoff with bandits?"
21
- QUESTION_IS_ACTION_LIKELY_LETHAL = "Is the action just described likely to result in anyone dying?"
22
- QUESTION_IS_ACTION_RUNNING_AWAY = "Is the action just described an example of running away by sprinting?"
23
- QUESTION_IS_ACTION_MAGIC = "Is the action just described an example of using supernatural magical spells / potions / etc?"
24
- QUESTION_DID_PROTAGONIST_KILL = "In the story segment above, did the protagonist kill anyone?"
25
- QUESTION_DID_PROTAGONIST_PERISH = "In the story segment above, does the protagonist get killed?"
26
 
27
  N_TURNS_REQUIRED_TO_PASS_FIRST_BANDIT_ENCOUNTER = 3
28
  N_TURNS_REQUIRED_TO_REACH_HOME = 6
@@ -79,29 +73,53 @@ def elaborate(
79
 
80
 
81
 
82
- def run_1_game_turn(s_narrator_transcript, s_n_turns_elapsed, s_print_output, s_user_input):
83
  n_turns_elapsed = int(s_n_turns_elapsed)
84
- n_turns_elapsed += 1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
 
86
  s_n_turns_elapsed = str(n_turns_elapsed)
87
  s_user_input = ""
88
- return [s_narrator_transcript, s_n_turns_elapsed, s_print_output, s_user_input]
 
 
 
 
89
 
90
 
91
  demo = gr.Blocks()
92
 
93
  with demo:
94
- # unique_session_key = str(uuid.uuid4())
 
 
 
 
95
 
96
- gr_narrator_transcript = gr.Textbox(value=GAME_INTRO + NOTES_TO_THE_NARRATOR + AWAITING_INPUT, interactive=False) #, visible=False)
97
- gr_n_turns_elapsed = gr.Textbox(value="0", interactive=False) #, visible=False)
98
- gr_print_output = gr.Textbox(label="", value=GAME_INTRO + AWAITING_INPUT, interactive=False, max_lines=9999)
99
  gr_markdown1 = gr.Markdown("After clicking Run Next Turn, please be patient as it may take up to a minute for the game state to update.")
100
  gr_user_input = gr.Textbox(label="", value="", placeholder="Describe your next action here...", interactive=True)
101
  gr_button1 = gr.Button(value="Run Next Turn")
 
 
102
 
103
  gr_button1.click(fn=run_1_game_turn,
104
- inputs=[gr_narrator_transcript, gr_n_turns_elapsed, gr_print_output, gr_user_input],
105
- outputs=[gr_narrator_transcript, gr_n_turns_elapsed, gr_print_output, gr_user_input])
106
 
107
  demo.launch()
 
1
  import gradio as gr
2
  from game_content import GAME_INTRO, NOTES_TO_THE_NARRATOR, AWAITING_INPUT
3
+ from game_content import game_over_victory_txt, game_over_fail_txt, S_GAME_OVER
4
  import decider_utils
5
  from decider_utils import YES, NO
6
+ from decider_questions import * # QUESTION_IS_USER_HOME and other questions
7
 
8
 
9
 
 
16
  N_COMPLETIONS_WHEN_ELABORATING = 1 # I previously had this set to 3, but that made the program very slow.
17
  MINIMUM_COMPLETION_LENGTH_CHARS_WHEN_ELABORATING = 7
18
 
19
+
 
 
 
 
 
 
 
20
 
21
  N_TURNS_REQUIRED_TO_PASS_FIRST_BANDIT_ENCOUNTER = 3
22
  N_TURNS_REQUIRED_TO_REACH_HOME = 6
 
73
 
74
 
75
 
76
+ def run_1_game_turn(s_narr_transcript, s_n_turns_elapsed, s_user_transcript, s_user_input):
77
  n_turns_elapsed = int(s_n_turns_elapsed)
78
+
79
+ finally_add2_both_tscripts = ""
80
+
81
+ if s_user_input == "":
82
+ s_user_transcript += "You must choose an action."
83
+
84
+ elif S_GAME_OVER in s_narr_transcript:
85
+ s_user_transcript += "Sorry, the game is already over. To play again, please refresh the page."
86
+
87
+ elif decider_utils.yesno(QUESTION_IS_ACTION_LIKELY_LETHAL, s_user_input, default=NO):
88
+ finally_add2_both_tscripts += game_over_fail_txt("You have taken an action that is likely to result in killing someone.")
89
+
90
+ else:
91
+ # User input accepted.
92
+
93
+ n_turns_elapsed += 1
94
+
95
+
96
 
97
  s_n_turns_elapsed = str(n_turns_elapsed)
98
  s_user_input = ""
99
+
100
+ s_narr_transcript += finally_add2_both_tscripts
101
+ s_user_transcript += finally_add2_both_tscripts
102
+
103
+ return [s_narr_transcript, s_n_turns_elapsed, s_user_transcript, s_user_input]
104
 
105
 
106
  demo = gr.Blocks()
107
 
108
  with demo:
109
+ s_narr_transcript = GAME_INTRO + NOTES_TO_THE_NARRATOR + AWAITING_INPUT
110
+ s_user_transcript = GAME_INTRO + AWAITING_INPUT
111
+
112
+ gr_narr_transcript = gr.Textbox(label="", value=s_narr_transcript, interactive=False, max_lines=9999) #, visible=False)
113
+ gr_user_transcript = gr.Textbox(label="", value=s_user_transcript, interactive=False, max_lines=9999)
114
 
 
 
 
115
  gr_markdown1 = gr.Markdown("After clicking Run Next Turn, please be patient as it may take up to a minute for the game state to update.")
116
  gr_user_input = gr.Textbox(label="", value="", placeholder="Describe your next action here...", interactive=True)
117
  gr_button1 = gr.Button(value="Run Next Turn")
118
+
119
+ gr_n_turns_elapsed = gr.Textbox(label="N Turns Elapsed", value="0", interactive=False)
120
 
121
  gr_button1.click(fn=run_1_game_turn,
122
+ inputs=[gr_narr_transcript, gr_n_turns_elapsed, gr_user_transcript, gr_user_input],
123
+ outputs=[gr_narr_transcript, gr_n_turns_elapsed, gr_user_transcript, gr_user_input])
124
 
125
  demo.launch()
decider_questions.py ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ QUESTION_IS_USER_HOME = "At the end of the above story, is the protagonist located at their destination?"
2
+ QUESTION_DOES_USER_STILL_HAVE_AT_LEAST_30_GOLD = "At the end of the above story, does the protagonist still have at least 30 gold pieces?"
3
+ QUESTION_IS_USER_ENGAGED_WITH_BANDITS = "At the end of the above story, is the protagonist currently still engaged in a standoff with bandits?"
4
+ QUESTION_IS_ACTION_LIKELY_LETHAL = "Is the action just described likely to result in anyone dying?"
5
+ QUESTION_IS_ACTION_RUNNING_AWAY = "Is the action just described an example of running away by sprinting?"
6
+ QUESTION_IS_ACTION_MAGIC = "Is the action just described an example of using supernatural magical spells / potions / etc?"
7
+ QUESTION_DID_PROTAGONIST_KILL = "In the story segment above, did the protagonist kill anyone?"
decider_utils.py CHANGED
@@ -1,5 +1,4 @@
1
  import openai
2
- from debug_logger import debug_print
3
 
4
  YES = True
5
  NO = False
@@ -7,9 +6,7 @@ NO = False
7
 
8
  def yesno(question, text, default):
9
  prompt = text + "\n\n" + question
10
- debug_print()
11
- debug_print()
12
- debug_print(prompt)
13
  hopefully_word_yes_or_no = openai.Completion.create(
14
  engine="text-davinci-002",
15
  prompt=prompt,
@@ -21,7 +18,6 @@ def yesno(question, text, default):
21
  )["choices"][0]["text"]
22
 
23
  hopefully_word_yes_or_no = hopefully_word_yes_or_no.upper().strip()
24
- debug_print(hopefully_word_yes_or_no)
25
 
26
  result = default
27
 
@@ -33,5 +29,4 @@ def yesno(question, text, default):
33
  if hopefully_word_yes_or_no.startswith("Y"):
34
  result = YES
35
 
36
- debug_print(f"Returning {result}")
37
  return result
 
1
  import openai
 
2
 
3
  YES = True
4
  NO = False
 
6
 
7
  def yesno(question, text, default):
8
  prompt = text + "\n\n" + question
9
+
 
 
10
  hopefully_word_yes_or_no = openai.Completion.create(
11
  engine="text-davinci-002",
12
  prompt=prompt,
 
18
  )["choices"][0]["text"]
19
 
20
  hopefully_word_yes_or_no = hopefully_word_yes_or_no.upper().strip()
 
21
 
22
  result = default
23
 
 
29
  if hopefully_word_yes_or_no.startswith("Y"):
30
  result = YES
31
 
 
32
  return result
game_content.py CHANGED
@@ -30,3 +30,27 @@ ATTEMPTS TO REACH HOME MUST ALWAYS BE FOILED BY GETTING LOST, ENCOUNTERING DANGE
30
 
31
  AWAITING_INPUT = '''Awaiting user input:
32
  '''
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
  AWAITING_INPUT = '''Awaiting user input:
32
  '''
33
+
34
+
35
+ # You can check if S_GAME_OVER is in either transcript, to see if the game is over.
36
+ S_GAME_OVER = "GAME OVER"
37
+
38
+ def game_over_victory_txt(s_reason):
39
+ return f'''
40
+ !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
41
+ !! {S_GAME_OVER}. !!
42
+ !! YOU WIN! !!
43
+ !! {s_reason}
44
+ !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
45
+
46
+ To play again, please refresh the page.'''
47
+
48
+
49
+ def game_over_fail_txt(s_reason):
50
+ return f'''//################################################################################################//
51
+ ## {S_GAME_OVER}. &&
52
+ ## {sreason}
53
+ ## YOU LOSE. &&
54
+ //################################################################################################//
55
+
56
+ To play again, please refresh the page.'''