Spaces:
Runtime error
Runtime error
Use gr.State to manage session state.
Browse files
app.py
CHANGED
@@ -20,38 +20,42 @@ class Game:
|
|
20 |
def prompt(self):
|
21 |
return f"Try to guess the word! Either enter the word or ask a hint. The word will be one of {self.word_list}"
|
22 |
|
|
|
|
|
|
|
23 |
|
24 |
with gr.Blocks(theme='gstaff/xkcd') as demo:
|
25 |
-
|
26 |
title = gr.Markdown("# Guessing Game")
|
27 |
-
chatbot = gr.Chatbot(value=[(None,
|
28 |
msg = gr.Textbox()
|
29 |
restart = gr.Button("Restart")
|
30 |
|
31 |
-
def user(user_message, history):
|
32 |
-
return "", history + [[user_message, None]]
|
33 |
|
34 |
-
def bot(history):
|
35 |
user_input = history[-1][0]
|
36 |
-
if game.secret_word
|
37 |
history[-1][1] = f"You win, the word was {game.secret_word}!"
|
38 |
-
|
|
|
39 |
if user_input.strip().lower() in game.word_list:
|
40 |
history[-1][1] = "Wrong guess, try again."
|
41 |
-
return history
|
42 |
instructions = f"The secret word is {game.secret_word}. Answer this: {user_input}"
|
43 |
bot_message = model(instructions, max_length=256, do_sample=True)[0]['generated_text']
|
44 |
response = bot_message.replace(game.secret_word, "?????").replace(game.secret_word.title(), "?????")
|
45 |
history[-1][1] = response
|
46 |
-
return history
|
47 |
|
48 |
-
def restart_game():
|
49 |
game.reset()
|
50 |
-
return [(None, game.prompt)]
|
51 |
|
52 |
-
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
53 |
-
bot, chatbot, chatbot
|
54 |
)
|
55 |
-
restart.click(restart_game,
|
56 |
|
57 |
demo.launch()
|
|
|
20 |
def prompt(self):
|
21 |
return f"Try to guess the word! Either enter the word or ask a hint. The word will be one of {self.word_list}"
|
22 |
|
23 |
+
def __str__(self):
|
24 |
+
return f"word_list: {self.word_list}, secret_word: {self.secret_word}"
|
25 |
+
|
26 |
|
27 |
with gr.Blocks(theme='gstaff/xkcd') as demo:
|
28 |
+
game_state = gr.State(Game())
|
29 |
title = gr.Markdown("# Guessing Game")
|
30 |
+
chatbot = gr.Chatbot(value=[(None, game_state.value.prompt)])
|
31 |
msg = gr.Textbox()
|
32 |
restart = gr.Button("Restart")
|
33 |
|
34 |
+
def user(user_message, history, game):
|
35 |
+
return "", history + [[user_message, None]], game
|
36 |
|
37 |
+
def bot(history, game):
|
38 |
user_input = history[-1][0]
|
39 |
+
if game.secret_word in user_input.strip().lower().split():
|
40 |
history[-1][1] = f"You win, the word was {game.secret_word}!"
|
41 |
+
print(history)
|
42 |
+
return history, game
|
43 |
if user_input.strip().lower() in game.word_list:
|
44 |
history[-1][1] = "Wrong guess, try again."
|
45 |
+
return history, game
|
46 |
instructions = f"The secret word is {game.secret_word}. Answer this: {user_input}"
|
47 |
bot_message = model(instructions, max_length=256, do_sample=True)[0]['generated_text']
|
48 |
response = bot_message.replace(game.secret_word, "?????").replace(game.secret_word.title(), "?????")
|
49 |
history[-1][1] = response
|
50 |
+
return history, game
|
51 |
|
52 |
+
def restart_game(game):
|
53 |
game.reset()
|
54 |
+
return [(None, game.prompt)], game
|
55 |
|
56 |
+
msg.submit(user, [msg, chatbot, game_state], [msg, chatbot, game_state], queue=False).then(
|
57 |
+
bot, [chatbot, game_state], [chatbot, game_state]
|
58 |
)
|
59 |
+
restart.click(restart_game, game_state, [chatbot, game_state], queue=False)
|
60 |
|
61 |
demo.launch()
|