Jonas Wiesli commited on
Commit
99d7111
1 Parent(s): bb63de8

formatting adjustments

Browse files
Files changed (1) hide show
  1. app.py +58 -41
app.py CHANGED
@@ -88,37 +88,37 @@ with gr.Blocks(theme=theme, css=css) as iface:
88
  return "", history + [[user_message, None]]
89
 
90
  # this is an ugly workaround, you cannot pass regular integers to gradio functions, so i had to do it this way
91
- def bot0(history, questionCounterText):
92
- bot_message = generateAIMessage(history, questionCounterText, 0)
93
  history[-1][1] = bot_message
94
  return history
95
 
96
- def bot1(history, questionCounterText):
97
- bot_message = generateAIMessage(history, questionCounterText, 1)
98
  history[-1][1] = bot_message
99
  return history
100
 
101
- def bot2(history, questionCounterText):
102
- bot_message = generateAIMessage(history, questionCounterText, 2)
103
  history[-1][1] = bot_message
104
  return history
105
 
106
- def bot3(history, questionCounterText):
107
- bot_message = generateAIMessage(history, questionCounterText, 3)
108
  history[-1][1] = bot_message
109
  return history
110
 
111
- def bot4(history, questionCounterText):
112
- bot_message = generateAIMessage(history, questionCounterText, 4)
113
  history[-1][1] = bot_message
114
  return history
115
 
116
- def generateAIMessage(history, questionCounterText, characterId):
117
- if checkIfQuestionsLeft(questionCounterText):
118
- initSysMessage = initText[characterId] + "Stay in character. Use natural language. Don't reveal all of " \
119
- "the information in a single message, and leave hints."
120
  message_history = [
121
- {"role": "system", "content": initSysMessage}
122
  ]
123
  for pair in history:
124
  message_history.append({"role": "user", "content": pair[0]})
@@ -133,55 +133,72 @@ with gr.Blocks(theme=theme, css=css) as iface:
133
  else:
134
  return "--- YOU'VE RUN OUT OF QUESTIONS, MAKE YOUR CHOICE ---"
135
 
136
- def updateQuestions(questionCounterText):
137
- remainingQuestions = int(questionCounterText["label"])
138
- if remainingQuestions > 0:
139
- remainingQuestions -= 1
140
- return str(remainingQuestions)
141
 
142
- def checkIfQuestionsLeft(questionCounterText):
143
- remainingQuestions = int(questionCounterText["label"])
144
- return remainingQuestions > 0
145
 
146
- def tryToSolve():
147
  return {solvingRow: gr.update(visible=True)}
148
- def showMap():
 
149
  return {mapRow: gr.update(visible=True)}
150
- def hideSolveBox():
 
151
  return {solvingRow: gr.update(visible=False)}
152
- def hideMap():
 
153
  return {mapRow: gr.update(visible=False)}
154
 
155
- def showResult():
156
- return {chatRow: gr.update(visible=False), solvingRow: gr.update(visible=False), resultRow: gr.update(visible=True)}
 
 
 
 
157
 
158
- def declareLoss():
159
  return "You failed! The real culprit got away... "
160
 
161
- def declareVictory():
162
  return "Congratulations, you found the killer! "
163
 
164
- botfunctions = [bot0, bot1, bot2, bot3, bot4]
165
  i = 0
166
  while i < len(roles):
167
- msg[i].submit(user, [msg[i], chatbot[i]], [msg[i], chatbot[i]], queue=False).then(
168
- botfunctions[i], [chatbot[i], questionCounter], chatbot[i]
169
  ).then(
170
- updateQuestions, questionCounter, questionCounter
 
 
171
  )
172
  i += 1
173
 
174
- mapButton.click(showMap, None, mapRow)
175
- closeMapButton.click(hideMap, None, mapRow)
176
- finishButton.click(tryToSolve, None, solvingRow)
177
- closeSolvingButton.click(hideSolveBox, None, solvingRow)
178
 
179
  i = 0
180
  while i < len(roles):
181
  if i == culpritId:
182
- accusationButtons[i].click(showResult, None, [chatRow, solvingRow, resultRow]).then(declareVictory, None, resultLabel)
 
 
 
 
183
  else:
184
- accusationButtons[i].click(showResult, None, [chatRow, solvingRow, resultRow]).then(declareLoss, None, resultLabel)
 
 
 
 
185
  i += 1
186
 
187
  iface.launch()
 
88
  return "", history + [[user_message, None]]
89
 
90
  # this is an ugly workaround, you cannot pass regular integers to gradio functions, so i had to do it this way
91
+ def bot0(history, question_counter_text):
92
+ bot_message = generate_ai_message(history, question_counter_text, 0)
93
  history[-1][1] = bot_message
94
  return history
95
 
96
+ def bot1(history, question_counter_text):
97
+ bot_message = generate_ai_message(history, question_counter_text, 1)
98
  history[-1][1] = bot_message
99
  return history
100
 
101
+ def bot2(history, question_counter_text):
102
+ bot_message = generate_ai_message(history, question_counter_text, 2)
103
  history[-1][1] = bot_message
104
  return history
105
 
106
+ def bot3(history, question_counter_text):
107
+ bot_message = generate_ai_message(history, question_counter_text, 3)
108
  history[-1][1] = bot_message
109
  return history
110
 
111
+ def bot4(history, question_counter_text):
112
+ bot_message = generate_ai_message(history, question_counter_text, 4)
113
  history[-1][1] = bot_message
114
  return history
115
 
116
+ def generate_ai_message(history, question_counter_text, character_id):
117
+ if check_if_questions_left(question_counter_text):
118
+ init_sys_message = initText[character_id] + "Stay in character. Use natural language. Don't reveal all of" \
119
+ " the information in a single message, and leave hints."
120
  message_history = [
121
+ {"role": "system", "content": init_sys_message}
122
  ]
123
  for pair in history:
124
  message_history.append({"role": "user", "content": pair[0]})
 
133
  else:
134
  return "--- YOU'VE RUN OUT OF QUESTIONS, MAKE YOUR CHOICE ---"
135
 
136
+ def update_questions(question_counter_text):
137
+ remaining_questions = int(question_counter_text["label"])
138
+ if remaining_questions > 0:
139
+ remaining_questions -= 1
140
+ return str(remaining_questions)
141
 
142
+ def check_if_questions_left(question_counter_text):
143
+ remaining_questions = int(question_counter_text["label"])
144
+ return remaining_questions > 0
145
 
146
+ def try_to_solve():
147
  return {solvingRow: gr.update(visible=True)}
148
+
149
+ def show_map():
150
  return {mapRow: gr.update(visible=True)}
151
+
152
+ def hide_solve_box():
153
  return {solvingRow: gr.update(visible=False)}
154
+
155
+ def hide_map():
156
  return {mapRow: gr.update(visible=False)}
157
 
158
+ def show_result():
159
+ return {
160
+ chatRow: gr.update(visible=False),
161
+ solvingRow: gr.update(visible=False),
162
+ resultRow: gr.update(visible=True)
163
+ }
164
 
165
+ def declare_loss():
166
  return "You failed! The real culprit got away... "
167
 
168
+ def declare_victory():
169
  return "Congratulations, you found the killer! "
170
 
171
+ bot_functions = [bot0, bot1, bot2, bot3, bot4]
172
  i = 0
173
  while i < len(roles):
174
+ msg[i].submit(
175
+ user, [msg[i], chatbot[i]], [msg[i], chatbot[i]], queue=False
176
  ).then(
177
+ bot_functions[i], [chatbot[i], questionCounter], chatbot[i]
178
+ ).then(
179
+ update_questions, questionCounter, questionCounter
180
  )
181
  i += 1
182
 
183
+ mapButton.click(show_map, None, mapRow)
184
+ closeMapButton.click(hide_map, None, mapRow)
185
+ finishButton.click(try_to_solve, None, solvingRow)
186
+ closeSolvingButton.click(hide_solve_box, None, solvingRow)
187
 
188
  i = 0
189
  while i < len(roles):
190
  if i == culpritId:
191
+ accusationButtons[i].click(
192
+ show_result, None, [chatRow, solvingRow, resultRow]
193
+ ).then(
194
+ declare_victory, None, resultLabel
195
+ )
196
  else:
197
+ accusationButtons[i].click(
198
+ show_result, None, [chatRow, solvingRow, resultRow]
199
+ ).then(
200
+ declare_loss, None, resultLabel
201
+ )
202
  i += 1
203
 
204
  iface.launch()