Jonas Wiesli commited on
Commit
b8ac00f
1 Parent(s): 2dbfe8d

implemented question limit

Browse files
Files changed (1) hide show
  1. app.py +43 -33
app.py CHANGED
@@ -35,86 +35,96 @@ with gr.Blocks() as iface:
35
  msg.append(gr.Textbox())
36
  i += 1
37
 
38
- questionCounter = gr.Label(value="Remaining Questions: 24")
39
 
40
  def user(user_message, history):
41
  return "", history + [[user_message, None]]
42
 
43
  # this is an ugly workaround, you cannot pass regular integers to gradio functions, so i had to do it this way
44
- def bot0(history):
45
- bot_message = generateAIMessage(history, 0)
46
  history[-1][1] = bot_message
47
  return history
48
 
49
- def bot1(history):
50
- bot_message = generateAIMessage(history, 1)
51
  history[-1][1] = bot_message
52
  return history
53
 
54
- def bot2(history):
55
- bot_message = generateAIMessage(history, 2)
56
  history[-1][1] = bot_message
57
  return history
58
 
59
- def bot3(history):
60
- bot_message = generateAIMessage(history, 3)
61
  history[-1][1] = bot_message
62
  return history
63
 
64
- def bot4(history):
65
- bot_message = generateAIMessage(history, 4)
66
  history[-1][1] = bot_message
67
  return history
68
 
69
- def generateAIMessage(history, characterId):
70
- message_history = [
71
- {"role": "system", "content": initText[characterId] + "Stay in character. Use natural language. Don't "
72
- "reveal all of the information in a single message,"
73
- " and leave hints. "}
74
- ]
75
- for pair in history:
76
- message_history.append({"role": "user", "content": pair[0]})
77
- print()
78
- if pair[1] is not None:
79
- message_history.append({"role": "assistant", "content": pair[1]})
80
- completion = openai.ChatCompletion.create(
81
- model="gpt-3.5-turbo",
82
- messages=message_history
83
- )
84
- return completion.choices[0].message.content
 
 
 
85
 
86
  def updateQuestions(prevText):
87
  remainingQuestions = int(prevText["label"].split(": ")[1])
88
  remainingQuestions -= 1
89
- return "Remaining Questions: " + str(remainingQuestions)
 
 
 
 
 
 
 
90
 
91
  # again, ugly workaround
92
  msg[0].submit(user, [msg[0], chatbot[0]], [msg[0], chatbot[0]], queue=False).then(
93
- bot0, chatbot[0], chatbot[0]
94
  ).then(
95
  updateQuestions, questionCounter, questionCounter
96
  )
97
 
98
  msg[1].submit(user, [msg[1], chatbot[1]], [msg[1], chatbot[1]], queue=False).then(
99
- bot1, chatbot[1], chatbot[1]
100
  ).then(
101
  updateQuestions, questionCounter, questionCounter
102
  )
103
 
104
  msg[2].submit(user, [msg[2], chatbot[2]], [msg[2], chatbot[2]], queue=False).then(
105
- bot2, chatbot[2], chatbot[2]
106
  ).then(
107
  updateQuestions, questionCounter, questionCounter
108
  )
109
 
110
  msg[3].submit(user, [msg[3], chatbot[3]], [msg[3], chatbot[3]], queue=False).then(
111
- bot3, chatbot[3], chatbot[3]
112
  ).then(
113
  updateQuestions, questionCounter, questionCounter
114
  )
115
 
116
  msg[4].submit(user, [msg[4], chatbot[4]], [msg[4], chatbot[4]], queue=False).then(
117
- bot4, chatbot[4], chatbot[4]
118
  ).then(
119
  updateQuestions, questionCounter, questionCounter
120
  )
 
35
  msg.append(gr.Textbox())
36
  i += 1
37
 
38
+ questionCounter = gr.Label(value="Remaining Questions: 4")
39
 
40
  def user(user_message, history):
41
  return "", history + [[user_message, None]]
42
 
43
  # this is an ugly workaround, you cannot pass regular integers to gradio functions, so i had to do it this way
44
+ def bot0(history, prevText):
45
+ bot_message = generateAIMessage(history, prevText, 0)
46
  history[-1][1] = bot_message
47
  return history
48
 
49
+ def bot1(history, prevText):
50
+ bot_message = generateAIMessage(history, prevText, 1)
51
  history[-1][1] = bot_message
52
  return history
53
 
54
+ def bot2(history, prevText):
55
+ bot_message = generateAIMessage(history, prevText, 2)
56
  history[-1][1] = bot_message
57
  return history
58
 
59
+ def bot3(history, prevText):
60
+ bot_message = generateAIMessage(history, prevText, 3)
61
  history[-1][1] = bot_message
62
  return history
63
 
64
+ def bot4(history, prevText):
65
+ bot_message = generateAIMessage(history, prevText, 4)
66
  history[-1][1] = bot_message
67
  return history
68
 
69
+ def generateAIMessage(history, prevText, characterId):
70
+ if checkIfQuestionsLeft(prevText):
71
+ message_history = [
72
+ {"role": "system", "content": initText[characterId] + "Stay in character. Use natural language. Don't "
73
+ "reveal all of the information in a single message,"
74
+ " and leave hints. "}
75
+ ]
76
+ for pair in history:
77
+ message_history.append({"role": "user", "content": pair[0]})
78
+ print()
79
+ if pair[1] is not None:
80
+ message_history.append({"role": "assistant", "content": pair[1]})
81
+ completion = openai.ChatCompletion.create(
82
+ model="gpt-3.5-turbo",
83
+ messages=message_history
84
+ )
85
+ return completion.choices[0].message.content
86
+ else:
87
+ return "--- YOU'VE RUN OUT OF QUESTIONS, MAKE YOUR CHOICE ---"
88
 
89
  def updateQuestions(prevText):
90
  remainingQuestions = int(prevText["label"].split(": ")[1])
91
  remainingQuestions -= 1
92
+ if remainingQuestions > 0:
93
+ return "Remaining Questions: " + str(remainingQuestions)
94
+ else:
95
+ return "You've used up your questions, now choose the culprit!"
96
+
97
+ def checkIfQuestionsLeft(prevText):
98
+ remainingQuestions = int(prevText["label"].split(": ")[1])
99
+ return remainingQuestions > 0
100
 
101
  # again, ugly workaround
102
  msg[0].submit(user, [msg[0], chatbot[0]], [msg[0], chatbot[0]], queue=False).then(
103
+ bot0, [chatbot[0], questionCounter], chatbot[0]
104
  ).then(
105
  updateQuestions, questionCounter, questionCounter
106
  )
107
 
108
  msg[1].submit(user, [msg[1], chatbot[1]], [msg[1], chatbot[1]], queue=False).then(
109
+ bot1, [chatbot[1], questionCounter], chatbot[1]
110
  ).then(
111
  updateQuestions, questionCounter, questionCounter
112
  )
113
 
114
  msg[2].submit(user, [msg[2], chatbot[2]], [msg[2], chatbot[2]], queue=False).then(
115
+ bot2, [chatbot[2], questionCounter], chatbot[2]
116
  ).then(
117
  updateQuestions, questionCounter, questionCounter
118
  )
119
 
120
  msg[3].submit(user, [msg[3], chatbot[3]], [msg[3], chatbot[3]], queue=False).then(
121
+ bot3, [chatbot[3], questionCounter], chatbot[3]
122
  ).then(
123
  updateQuestions, questionCounter, questionCounter
124
  )
125
 
126
  msg[4].submit(user, [msg[4], chatbot[4]], [msg[4], chatbot[4]], queue=False).then(
127
+ bot4, [chatbot[4], questionCounter], chatbot[4]
128
  ).then(
129
  updateQuestions, questionCounter, questionCounter
130
  )