marksverdhei commited on
Commit
7fe9bd6
1 Parent(s): 4cb4002

Make game replayable

Browse files
Files changed (5) hide show
  1. src/handlers.py +3 -1
  2. src/interface.py +10 -11
  3. src/reducer.py +21 -25
  4. src/shared.py +16 -0
  5. src/text.py +12 -3
src/handlers.py CHANGED
@@ -3,8 +3,10 @@ import logging
3
  from transformers import PreTrainedTokenizer
4
 
5
  from src.constants import MAX_ATTEMPTS
 
6
  from src.params import ReducerParams
7
  from src.shared import all_tokens
 
8
  from src.utils import get_current_prompt_text
9
 
10
  logger = logging.getLogger(__name__)
@@ -33,7 +35,7 @@ def handle_lm_win(params: ReducerParams) -> ReducerParams:
33
 
34
  def handle_out_of_attempts(params: ReducerParams) -> ReducerParams:
35
  params.button_label = "Next word"
36
- params.bottom_html = "Out of attempts. No one gets points!"
37
  return params
38
 
39
 
 
3
  from transformers import PreTrainedTokenizer
4
 
5
  from src.constants import MAX_ATTEMPTS
6
+ from src.constants import STARTING_INDEX
7
  from src.params import ReducerParams
8
  from src.shared import all_tokens
9
+ from src.shared import tokenizer
10
  from src.utils import get_current_prompt_text
11
 
12
  logger = logging.getLogger(__name__)
 
35
 
36
  def handle_out_of_attempts(params: ReducerParams) -> ReducerParams:
37
  params.button_label = "Next word"
38
+ params.bottom_html = f"Out of attempts. No one gets points! The correct word was: {tokenizer.decode(all_tokens[STARTING_INDEX + params.word_number])}"
39
  return params
40
 
41
 
src/interface.py CHANGED
@@ -1,9 +1,8 @@
1
  import gradio as gr
2
 
3
  from src import reducer
4
- from src import shared
5
  from src.constants import MAX_ATTEMPTS
6
- from src.constants import STARTING_INDEX
7
  from src.utils import get_current_lm_guess_str
8
 
9
 
@@ -24,24 +23,24 @@ def build_demo():
24
  )
25
  with gr.Row():
26
  prompt_text = gr.Textbox(
27
- value=shared.tokenizer.decode(shared.all_tokens[:STARTING_INDEX]),
28
  label="Context",
29
  interactive=False,
30
  )
31
  with gr.Row():
32
  with gr.Column():
33
- player_points = gr.Number(label="your points", interactive=False)
34
  with gr.Column():
35
- lm_points = gr.Number(label="LM points", interactive=False)
36
  with gr.Row():
37
  with gr.Column():
38
  remaining_attempts = gr.Number(
39
- value=MAX_ATTEMPTS,
40
  label="Remaining attempts",
41
  precision=0,
42
  interactive=False,
43
  )
44
- current_guesses = gr.Textbox(label="Your guesses")
45
  with gr.Column():
46
  lm_guesses = gr.Textbox(
47
  label="LM guesses", value=get_current_lm_guess_str(0, remaining_attempts=MAX_ATTEMPTS)
@@ -49,13 +48,13 @@ def build_demo():
49
 
50
  with gr.Row():
51
  with gr.Column():
52
- guess_field = gr.Textbox(label="")
53
- guess_button = gr.Button(value="Guess!")
54
 
55
  with gr.Row():
56
- bottom_html = gr.HTML()
57
  with gr.Row():
58
- word_number = gr.Number(label="Word no.", interactive=False, precision=0)
59
 
60
  guess_button.click(
61
  reducer.handle_guess,
 
1
  import gradio as gr
2
 
3
  from src import reducer
 
4
  from src.constants import MAX_ATTEMPTS
5
+ from src.shared import INITIAL_STATE
6
  from src.utils import get_current_lm_guess_str
7
 
8
 
 
23
  )
24
  with gr.Row():
25
  prompt_text = gr.Textbox(
26
+ value=INITIAL_STATE.prompt_text,
27
  label="Context",
28
  interactive=False,
29
  )
30
  with gr.Row():
31
  with gr.Column():
32
+ player_points = gr.Number(label="your points", interactive=False, value=INITIAL_STATE.player_points)
33
  with gr.Column():
34
+ lm_points = gr.Number(label="LM points", interactive=False, value=INITIAL_STATE.lm_points)
35
  with gr.Row():
36
  with gr.Column():
37
  remaining_attempts = gr.Number(
38
+ value=INITIAL_STATE.remaining_attempts,
39
  label="Remaining attempts",
40
  precision=0,
41
  interactive=False,
42
  )
43
+ current_guesses = gr.Textbox(label="Your guesses", value=INITIAL_STATE.current_guesses)
44
  with gr.Column():
45
  lm_guesses = gr.Textbox(
46
  label="LM guesses", value=get_current_lm_guess_str(0, remaining_attempts=MAX_ATTEMPTS)
 
48
 
49
  with gr.Row():
50
  with gr.Column():
51
+ guess_field = gr.Textbox(label="", value=INITIAL_STATE.guess_field)
52
+ guess_button = gr.Button(value=INITIAL_STATE.button_label)
53
 
54
  with gr.Row():
55
+ bottom_html = gr.HTML(value=INITIAL_STATE.bottom_html)
56
  with gr.Row():
57
+ word_number = gr.Number(label="Word no.", interactive=False, precision=0, value=INITIAL_STATE.word_number)
58
 
59
  guess_button.click(
60
  reducer.handle_guess,
src/reducer.py CHANGED
@@ -1,6 +1,7 @@
1
  import logging
2
 
3
  from src.constants import END_WORD_NUMBER
 
4
  from src.constants import STARTING_INDEX
5
  from src.handlers import handle_both_wrong
6
  from src.handlers import handle_lm_win
@@ -10,6 +11,7 @@ from src.handlers import handle_out_of_attempts
10
  from src.handlers import handle_player_win
11
  from src.handlers import handle_tie
12
  from src.params import ReducerParams
 
13
  from src.shared import token_predictions
14
  from src.shared import tokenizer
15
  from src.utils import get_current_lm_guess_str
@@ -29,6 +31,9 @@ def handle_guess(*args) -> ReducerParams:
29
 
30
 
31
  def _handle_guess(params: ReducerParams) -> ReducerParams:
 
 
 
32
  if params.word_number >= END_WORD_NUMBER:
33
  # FIXME: BAD PRACTICE
34
  if params.player_points > params.lm_points:
@@ -38,42 +43,33 @@ def _handle_guess(params: ReducerParams) -> ReducerParams:
38
  else:
39
  s = "CAN YOU BELIEVE IT!? A TIE"
40
 
41
- return ReducerParams(
42
- "",
43
- params.player_points,
44
- params.lm_points,
45
- "",
46
- "",
47
- None,
48
- "",
49
- "Restart",
50
- s,
51
- 0,
52
- )
53
 
54
  logger.debug(params)
55
  logger.debug(token_predictions[STARTING_INDEX + params.word_number])
 
56
  if params.button_label == "Next word":
57
  params = handle_next_word(params)
58
  elif not params.guess_field:
59
  params = handle_no_input(params)
60
  else:
61
-
62
- params.current_guesses += params.guess_field + "\n"
 
63
  player_correct = guess_is_correct(params, tokenizer)
64
  lm_correct = lm_is_correct(params)
65
 
66
- if params.remaining_attempts > 0:
67
- params.remaining_attempts -= 1
68
- if player_correct and lm_correct:
69
- params = handle_tie(params)
70
- elif player_correct and not lm_correct:
71
- params = handle_player_win(params)
72
- elif lm_correct and not player_correct:
73
- params = handle_lm_win(params)
74
- else:
75
- params = handle_both_wrong(params)
76
-
77
  else:
78
  params = handle_out_of_attempts(params)
79
 
 
1
  import logging
2
 
3
  from src.constants import END_WORD_NUMBER
4
+ from src.constants import MAX_ATTEMPTS
5
  from src.constants import STARTING_INDEX
6
  from src.handlers import handle_both_wrong
7
  from src.handlers import handle_lm_win
 
11
  from src.handlers import handle_player_win
12
  from src.handlers import handle_tie
13
  from src.params import ReducerParams
14
+ from src.shared import INITIAL_STATE
15
  from src.shared import token_predictions
16
  from src.shared import tokenizer
17
  from src.utils import get_current_lm_guess_str
 
31
 
32
 
33
  def _handle_guess(params: ReducerParams) -> ReducerParams:
34
+ if params.button_label == "Restart":
35
+ return INITIAL_STATE
36
+
37
  if params.word_number >= END_WORD_NUMBER:
38
  # FIXME: BAD PRACTICE
39
  if params.player_points > params.lm_points:
 
43
  else:
44
  s = "CAN YOU BELIEVE IT!? A TIE"
45
 
46
+ params.button_label = "Restart"
47
+ params.bottom_html = s
48
+ return params
 
 
 
 
 
 
 
 
 
49
 
50
  logger.debug(params)
51
  logger.debug(token_predictions[STARTING_INDEX + params.word_number])
52
+
53
  if params.button_label == "Next word":
54
  params = handle_next_word(params)
55
  elif not params.guess_field:
56
  params = handle_no_input(params)
57
  else:
58
+ params.current_guesses += (
59
+ params.guess_field if params.remaining_attempts == MAX_ATTEMPTS else "\n" + params.guess_field
60
+ )
61
  player_correct = guess_is_correct(params, tokenizer)
62
  lm_correct = lm_is_correct(params)
63
 
64
+ params.remaining_attempts -= 1
65
+ if player_correct and lm_correct:
66
+ params = handle_tie(params)
67
+ elif player_correct and not lm_correct:
68
+ params = handle_player_win(params)
69
+ elif lm_correct and not player_correct:
70
+ params = handle_lm_win(params)
71
+ elif params.remaining_attempts > 0:
72
+ params = handle_both_wrong(params)
 
 
73
  else:
74
  params = handle_out_of_attempts(params)
75
 
src/shared.py CHANGED
@@ -1,5 +1,8 @@
1
  from transformers import AutoTokenizer
2
 
 
 
 
3
  from src.predictions import make_predictions
4
  from src.text import get_text
5
 
@@ -7,3 +10,16 @@ tokenizer = AutoTokenizer.from_pretrained("gpt2")
7
  token_id_predictions, token_predictions = make_predictions(tokenizer)
8
  text = get_text()
9
  all_tokens = tokenizer.encode(text)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  from transformers import AutoTokenizer
2
 
3
+ from src.constants import MAX_ATTEMPTS
4
+ from src.constants import STARTING_INDEX
5
+ from src.params import ReducerParams
6
  from src.predictions import make_predictions
7
  from src.text import get_text
8
 
 
10
  token_id_predictions, token_predictions = make_predictions(tokenizer)
11
  text = get_text()
12
  all_tokens = tokenizer.encode(text)
13
+
14
+ INITIAL_STATE = ReducerParams(
15
+ prompt_text=tokenizer.decode(all_tokens[:STARTING_INDEX]),
16
+ player_points=0,
17
+ lm_points=0,
18
+ current_guesses="",
19
+ lm_guesses="",
20
+ remaining_attempts=MAX_ATTEMPTS,
21
+ guess_field="",
22
+ button_label="Guess!",
23
+ bottom_html="",
24
+ word_number=0,
25
+ )
src/text.py CHANGED
@@ -21,10 +21,19 @@
21
  # While this shell is said to be as hard as steel, a sudden, powerful impact could cause its liquid innards to pop out, leaving it completely exposed. Metapod generally remains motionless, rebuilding its cells for evolution.
22
  # If an enemy discovers Metapod, it is unable to do anything other than harden its outer shell. Metapod lives in temperate forests and jungles, often in groups. Pikipek is a natural predator of Metapod.
23
  # """
24
- target_text = """
25
- The Icelandic horse (Icelandic: íslenski hesturinn [ˈistlɛnscɪ ˈhɛstʏrɪn]) is a breed of horse developed in Iceland. Although the horses are small, at times pony-sized, most registries for the Icelandic refer to it as a horse. Icelandic horses are long-lived and hardy. In their native country they have few diseases; Icelandic law prevents horses from being imported into the country and exported animals are not allowed to return. The Icelandic displays two gaits in addition to the typical walk, trot, and canter/gallop commonly displayed by other breeds. The only breed of horse in Iceland, they are also popular internationally, and sizable populations exist in Europe and North America. The breed is still used for traditional sheepherding work in its native country, as well as for leisure, showing, and racing.
26
 
27
- Developed from ponies taken to Iceland by Norse settlers in the 9th and 10th centuries, the breed is mentioned in literature and historical records throughout Icelandic history; the first reference to a named horse appears in the 12th century. Horses were venerated in Norse mythology, a custom brought to Iceland by the country's earliest settlers. Selective breeding over the centuries has developed the breed into its current form. Natural selection has also played a role, as the harsh Icelandic climate eliminated many horses through exposure and malnourishment. In the 1780s, much of the breed was wiped out in the aftermath of a volcanic eruption at Laki. The first breed society for the Icelandic horse was created in Iceland in 1904, and today the breed is represented by organizations in 19 different nations, organized under a parent association, the International Federation of Icelandic Horse Associations.
 
 
 
 
 
 
 
 
 
28
  """
29
 
30
 
 
21
  # While this shell is said to be as hard as steel, a sudden, powerful impact could cause its liquid innards to pop out, leaving it completely exposed. Metapod generally remains motionless, rebuilding its cells for evolution.
22
  # If an enemy discovers Metapod, it is unable to do anything other than harden its outer shell. Metapod lives in temperate forests and jungles, often in groups. Pikipek is a natural predator of Metapod.
23
  # """
24
+ # target_text = """
25
+ # The Icelandic horse (Icelandic: íslenski hesturinn [ˈistlɛnscɪ ˈhɛstʏrɪn]) is a breed of horse developed in Iceland. Although the horses are small, at times pony-sized, most registries for the Icelandic refer to it as a horse. Icelandic horses are long-lived and hardy. In their native country they have few diseases; Icelandic law prevents horses from being imported into the country and exported animals are not allowed to return. The Icelandic displays two gaits in addition to the typical walk, trot, and canter/gallop commonly displayed by other breeds. The only breed of horse in Iceland, they are also popular internationally, and sizable populations exist in Europe and North America. The breed is still used for traditional sheepherding work in its native country, as well as for leisure, showing, and racing.
26
 
27
+ # Developed from ponies taken to Iceland by Norse settlers in the 9th and 10th centuries, the breed is mentioned in literature and historical records throughout Icelandic history; the first reference to a named horse appears in the 12th century. Horses were venerated in Norse mythology, a custom brought to Iceland by the country's earliest settlers. Selective breeding over the centuries has developed the breed into its current form. Natural selection has also played a role, as the harsh Icelandic climate eliminated many horses through exposure and malnourishment. In the 1780s, much of the breed was wiped out in the aftermath of a volcanic eruption at Laki. The first breed society for the Icelandic horse was created in Iceland in 1904, and today the breed is represented by organizations in 19 different nations, organized under a parent association, the International Federation of Icelandic Horse Associations.
28
+ # """
29
+ target_text = """
30
+ The dominant sequence transduction models are based on complex recurrent or convolutional neural networks in an encoder-decoder configuration.
31
+ The best performing models also connect the encoder and decoder through an attention mechanism.
32
+ We propose a new simple network architecture, the Transformer, based solely on attention mechanisms, dispensing with recurrence and convolutions entirely.
33
+ Experiments on two machine translation tasks show these models to be superior in quality while being more parallelizable and requiring significantly less time to train.
34
+ Our model achieves 28.4 BLEU on the WMT 2014 English-to-German translation task, improving over the existing best results, including ensembles by over 2 BLEU.
35
+ On the WMT 2014 English-to-French translation task, our model establishes a new single-model state-of-the-art BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small fraction of the training costs of the best models from the literature.
36
+ We show that the Transformer generalizes well to other tasks by applying it successfully to English constituency parsing both with large and limited training data.
37
  """
38
 
39