import gradio as gr import random from game_manager import games, new_game import uuid with gr.Blocks(css="style.css") as app: game_var = gr.Variable() last_update = gr.Variable(0) with gr.Column() as opening: gr.Markdown("# GPT Who?") gr.Markdown( """ Welcome to 'GPT Who?', a game played with multiple humans and one AI. Here are the rules: - Every player in a room will submit a prompt. - For every prompt, there will be a round. Every player submits a response to the prompt of the round. - GPT will also respond to the prompt. - Every player will vote on which response they think is the AI's response. - Player's get 2 points for guessing which response was GPT, as well 1 point for every player that thought they were GPT. That's it! """ ) player_name = gr.Text(label="Player Name") player_prompt = gr.Text(label="Prompt", info="Pick something fun, like 'Write a joke involving a banana.' or 'Why are children so dumb?'.") random_room_btn = gr.Button("Join Random Room!") with gr.Row(): room_name = gr.Text(label="Room Name") open_room_btn = gr.Button("Create / Join Room!") with gr.Column(visible=False) as game_col: chat_history = gr.Chatbot(label="Game") start_game_btn = gr.Button("Start Game", visible=False) with gr.Row(visible=False) as text_row: text_input = gr.Textbox(label="Input") with gr.Row(): player_states = gr.DataFrame( label="Players", headers=["Player", "Status", "Score"] ) time_remaining = gr.Label(label="Time Remaining", elem_id="time_remaining") def add_submission(player, text, game): game.add_chat(player, text) return { text_input: gr.Textbox.update(value=""), } text_input.submit(add_submission, [player_name, text_input, game_var], [text_input]) def select_ai_response(game, player, selected: gr.SelectData): if selected.index[0] != 0 and selected.index[1] == 1: game.select_chat(player, selected.index[0]) chat_history.select(select_ai_response, [game_var, player_name], None) def open_room(data): game = new_game(data[room_name]) game.add_player(data[player_name], data[player_prompt]) return { opening: gr.Column.update(visible=False), game_col: gr.Column.update(visible=True), game_var: game, chat_history: gr.Chatbot.update(label=data[room_name]), start_game_btn: gr.Button.update(visible=len(game.players) == 1), } def random_room(): for game in random.sample(list(games.values()), len(games)): if not game.started: return game.room_name return "room_" + str(uuid.uuid4())[:8] def update_game(data): game = data[game_var] if data[last_update] == game.last_update_index: return {chat_history: gr.skip()} else: return { chat_history: game.format_chat(data[player_name]), text_row: gr.Row.update(visible=game.input_allowed), player_states: game.get_player_state(), time_remaining: game.stage_time_remaining, } def start_game_on(evt): evt( open_room, {room_name, player_name, player_prompt}, {opening, game_col, game_var, chat_history, start_game_btn}, ).then( update_game, {game_var, last_update, player_name}, {chat_history, text_row, player_states, time_remaining}, every=1, ) start_game_on(open_room_btn.click) start_game_on(random_room_btn.click(random_room, None, room_name).success) def start_game_click(data): game = data[game_var] game.start() return { start_game_btn: gr.Button.update(visible=False), } start_game_btn.click(start_game_click, {game_var}, {start_game_btn, text_input}) app.queue().launch()