import gradio as gr import json import random from transformers import pipeline generator = pipeline("text-generation", model="gpt2", max_length=60) with open("wordlist.json") as wordlist_json: wordlist = json.load(wordlist_json) def autocomplete(text): end_text = " ".join(text.split(" ")[-30]) generated_text = generator( end_text, return_full_text=False, clean_up_tokenization_spaces=True )[0]["generated_text"] generated_text = generated_text.replace("\n", " ") generated_text = " ".join(generated_text.split(" ")[:-1]) return generated_text with gr.Blocks() as demo: gr.Markdown( """ # GPT Golf How many turns will it take you to get GPT to say the target word? Here are the rules of the game: - Your goal is to get GPT to say a target word in as few turns as possible. - Each turn, you add up to 5 words to its dialogue. - When you click submit, your prompt will be added to the dialogue. Then GPT will also add to the dialogue. - You can't say the target word, but as soon as GPT does, you win! """ ) error_box = gr.Textbox(label="Error", elem_id="error", visible=False) dialogue_var = gr.Variable(value=[]) start_btn = gr.Button("Start", variant="primary") with gr.Column(visible=False) as game: with gr.Row() as stats: target_word_box = gr.Textbox( label="Target Word", elem_id="target", interactive=False ) num_turns_box = gr.Number(0, label="# of Turns so Far", elem_id="num_turns") dialogue_box = gr.HighlightedText(label="Dialogue") with gr.Column() as prompt_set: prompt_box = gr.Textbox(label="Prompt", placeholder="Enter Next 5 Words...") submit_btn = gr.Button("Submit").style(full_width=True) win = gr.HTML( "