Upload run.py
Browse files
run.py
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import json
|
| 3 |
+
import random
|
| 4 |
+
from transformers import pipeline
|
| 5 |
+
|
| 6 |
+
generator = pipeline("text-generation", model="gpt2", max_length=60)
|
| 7 |
+
|
| 8 |
+
with open("wordlist.json") as wordlist_json:
|
| 9 |
+
wordlist = json.load(wordlist_json)
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def autocomplete(text):
|
| 13 |
+
end_text = " ".join(text.split(" ")[-30:])
|
| 14 |
+
generated_text = generator(
|
| 15 |
+
end_text, return_full_text=False, clean_up_tokenization_spaces=True
|
| 16 |
+
)[0]["generated_text"]
|
| 17 |
+
generated_text = generated_text.replace("\n", ". ")
|
| 18 |
+
generated_text = " ".join(generated_text.split(" ")[:-1])
|
| 19 |
+
return generated_text
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
with gr.Blocks() as demo:
|
| 23 |
+
gr.Markdown(
|
| 24 |
+
"""
|
| 25 |
+
# GPT Golf
|
| 26 |
+
How many turns will it take you to get GPT to say the target word?
|
| 27 |
+
Here are the rules of the game:
|
| 28 |
+
- Your goal is to get GPT to say a target word in as few turns as possible.
|
| 29 |
+
- Each turn, you add up to 5 words to its dialogue.
|
| 30 |
+
- When you click submit, your prompt will be added to the dialogue. Then GPT will also add to the dialogue.
|
| 31 |
+
- You can't say the target word, but as soon as GPT does, you win!
|
| 32 |
+
"""
|
| 33 |
+
)
|
| 34 |
+
error_box = gr.Textbox(label="Error", elem_id="error", visible=False)
|
| 35 |
+
dialogue_var = gr.Variable(value=[])
|
| 36 |
+
|
| 37 |
+
start_btn = gr.Button("Start", variant="primary")
|
| 38 |
+
with gr.Column(visible=False) as game:
|
| 39 |
+
with gr.Row() as stats:
|
| 40 |
+
target_word_box = gr.Textbox(
|
| 41 |
+
label="Target Word", elem_id="target", interactive=False
|
| 42 |
+
)
|
| 43 |
+
num_turns_box = gr.Number(0, label="# of Turns so Far", elem_id="num_turns")
|
| 44 |
+
dialogue_box = gr.HighlightedText(label="Dialogue")
|
| 45 |
+
with gr.Column() as prompt_set:
|
| 46 |
+
prompt_box = gr.Textbox(label="Prompt", placeholder="Enter Next 5 Words...")
|
| 47 |
+
submit_btn = gr.Button("Submit").style(full_width=True)
|
| 48 |
+
win = gr.HTML(
|
| 49 |
+
"<div style='width: 100%; padding: 3rem; font-size: 4rem; color: green; text-align: center; font-weight: bold'>You Won!</div>",
|
| 50 |
+
visible=False,
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
def start_game():
|
| 54 |
+
return {
|
| 55 |
+
start_btn: gr.update(visible=False),
|
| 56 |
+
game: gr.update(visible=True),
|
| 57 |
+
target_word_box: random.choice(wordlist),
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
start_btn.click(start_game, inputs=None, outputs=[start_btn, game, target_word_box])
|
| 61 |
+
|
| 62 |
+
def submit(prompt, target_word, dialogue, num_turns):
|
| 63 |
+
if len(prompt.split(" ")) > 5:
|
| 64 |
+
return {
|
| 65 |
+
error_box: gr.update(
|
| 66 |
+
visible=True, value="Prompt must be a maximum of 5 words!"
|
| 67 |
+
)
|
| 68 |
+
}
|
| 69 |
+
if target_word in prompt:
|
| 70 |
+
return {
|
| 71 |
+
error_box: gr.update(
|
| 72 |
+
visible=True, value="You can't use the target word in the prompt!"
|
| 73 |
+
)
|
| 74 |
+
}
|
| 75 |
+
dialogue.append(prompt)
|
| 76 |
+
response = autocomplete(" ".join(dialogue))
|
| 77 |
+
dialogue.append(response)
|
| 78 |
+
labeled_dialogue = [
|
| 79 |
+
(text, None if i % 2 == 0 else "gpt") for i, text in enumerate(dialogue)
|
| 80 |
+
]
|
| 81 |
+
if target_word in response:
|
| 82 |
+
return {
|
| 83 |
+
dialogue_box: labeled_dialogue,
|
| 84 |
+
prompt_set: gr.update(visible=False),
|
| 85 |
+
win: gr.update(visible=True),
|
| 86 |
+
num_turns_box: num_turns + 1,
|
| 87 |
+
dialogue_var: dialogue,
|
| 88 |
+
error_box: gr.update(visible=False),
|
| 89 |
+
}
|
| 90 |
+
else:
|
| 91 |
+
return {
|
| 92 |
+
dialogue_box: labeled_dialogue,
|
| 93 |
+
prompt_box: "",
|
| 94 |
+
num_turns_box: num_turns + 1,
|
| 95 |
+
dialogue_var: dialogue,
|
| 96 |
+
error_box: gr.update(visible=False),
|
| 97 |
+
}
|
| 98 |
+
|
| 99 |
+
submit_btn.click(
|
| 100 |
+
submit,
|
| 101 |
+
inputs=[prompt_box, target_word_box, dialogue_var, num_turns_box],
|
| 102 |
+
outputs=[
|
| 103 |
+
dialogue_var,
|
| 104 |
+
dialogue_box,
|
| 105 |
+
prompt_box,
|
| 106 |
+
num_turns_box,
|
| 107 |
+
error_box,
|
| 108 |
+
prompt_set,
|
| 109 |
+
win,
|
| 110 |
+
],
|
| 111 |
+
)
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
demo.launch(debug=True)
|