marksverdhei commited on
Commit
a88f931
1 Parent(s): 09c334f

:sparkles: Create a semi-working buggy first version

Browse files
Files changed (7) hide show
  1. app.py +1 -1
  2. src/constants.py +2 -1
  3. src/handlers.py +5 -2
  4. src/interface.py +4 -1
  5. src/reducer.py +55 -20
  6. src/text.py +27 -11
  7. src/utils.py +15 -2
app.py CHANGED
@@ -6,7 +6,7 @@ logging.basicConfig(level="DEBUG")
6
 
7
 
8
  def main():
9
- demo = interface.get_demo(wip=True)
10
  demo.launch(debug=True)
11
 
12
 
 
6
 
7
 
8
  def main():
9
+ demo = interface.get_demo(wip=False)
10
  demo.launch(debug=True)
11
 
12
 
src/constants.py CHANGED
@@ -1,3 +1,4 @@
1
  MAX_ATTEMPTS = 3
2
 
3
- STARTING_INDEX = 20
 
 
1
  MAX_ATTEMPTS = 3
2
 
3
+ STARTING_INDEX = 50
4
+ END_WORD_NUMBER = 10
src/handlers.py CHANGED
@@ -2,6 +2,7 @@ import logging
2
 
3
  from transformers import PreTrainedTokenizer
4
 
 
5
  from src.params import ReducerParams
6
  from src.shared import all_tokens
7
  from src.utils import get_current_prompt_text
@@ -42,8 +43,7 @@ def handle_tie(params: ReducerParams) -> ReducerParams:
42
  return params
43
 
44
 
45
- def handle_next_attempt(params: ReducerParams) -> ReducerParams:
46
- params.remaining_attempts -= 1
47
  params.bottom_html = f"That was not it... {params.remaining_attempts} attempts left"
48
  return params
49
 
@@ -58,4 +58,7 @@ def handle_next_word(params: ReducerParams) -> ReducerParams:
58
  params.button_label = "Guess!"
59
  params.bottom_html = ""
60
  params.prompt_text = get_current_prompt_text(params.word_number)
 
 
 
61
  return params
 
2
 
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
 
43
  return params
44
 
45
 
46
+ def handle_both_wrong(params: ReducerParams) -> ReducerParams:
 
47
  params.bottom_html = f"That was not it... {params.remaining_attempts} attempts left"
48
  return params
49
 
 
58
  params.button_label = "Guess!"
59
  params.bottom_html = ""
60
  params.prompt_text = get_current_prompt_text(params.word_number)
61
+ params.current_guesses = ""
62
+ params.remaining_attempts = MAX_ATTEMPTS
63
+ params.guess_field = ""
64
  return params
src/interface.py CHANGED
@@ -4,6 +4,7 @@ from src import reducer
4
  from src import shared
5
  from src.constants import MAX_ATTEMPTS
6
  from src.constants import STARTING_INDEX
 
7
 
8
 
9
  def build_demo():
@@ -42,7 +43,9 @@ def build_demo():
42
  )
43
  current_guesses = gr.Textbox(label="Your guesses")
44
  with gr.Column():
45
- lm_guesses = gr.Textbox(label="LM guesses")
 
 
46
 
47
  with gr.Row():
48
  with gr.Column():
 
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
 
10
  def build_demo():
 
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)
48
+ )
49
 
50
  with gr.Row():
51
  with gr.Column():
src/reducer.py CHANGED
@@ -1,14 +1,19 @@
1
  import logging
2
 
 
 
 
3
  from src.handlers import handle_lm_win
4
- from src.handlers import handle_next_attempt
5
  from src.handlers import handle_next_word
6
  from src.handlers import handle_no_input
7
  from src.handlers import handle_out_of_attempts
8
  from src.handlers import handle_player_win
9
  from src.handlers import handle_tie
10
  from src.params import ReducerParams
 
11
  from src.shared import tokenizer
 
 
12
  from src.utils import guess_is_correct
13
  from src.utils import lm_is_correct
14
 
@@ -24,25 +29,55 @@ def handle_guess(*args) -> ReducerParams:
24
 
25
 
26
  def _handle_guess(params: ReducerParams) -> ReducerParams:
27
- logger.debug(params)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
 
 
 
29
  if params.button_label == "Next word":
30
- return handle_next_word(params)
31
-
32
- if not params.guess_field:
33
- return handle_no_input(params)
34
-
35
- params.current_guesses += "\n" + params.guess_field
36
- player_correct = guess_is_correct(params, tokenizer)
37
- lm_correct = lm_is_correct(params)
38
-
39
- if player_correct and lm_correct:
40
- return handle_tie(params)
41
- elif player_correct and not lm_correct:
42
- return handle_player_win(params)
43
- elif lm_correct and not player_correct:
44
- return handle_lm_win(params)
45
- elif params.remaining_attempts == 0:
46
- return handle_out_of_attempts(params)
47
  else:
48
- return handle_next_attempt(params)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
 
7
  from src.handlers import handle_next_word
8
  from src.handlers import handle_no_input
9
  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
16
+ from src.utils import get_current_prompt_text
17
  from src.utils import guess_is_correct
18
  from src.utils import lm_is_correct
19
 
 
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:
35
+ s = "YOU WIN"
36
+ elif params.player_points < params.lm_points:
37
+ s = "YOU LOSE"
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
+
80
+ params.lm_guesses = get_current_lm_guess_str(params.word_number, params.remaining_attempts)
81
+ params.prompt_text = get_current_prompt_text(params.word_number)
82
+
83
+ return params
src/text.py CHANGED
@@ -1,14 +1,30 @@
1
- target_text = """\
2
- Obama was born in Honolulu, Hawaii.
3
- After graduating from Columbia University in 1983, he worked as a community organizer in Chicago.
4
- In 1988, he enrolled in Harvard Law School, where he was the first black president of the Harvard Law Review.
5
- After graduating, he became a civil rights attorney and an academic, teaching constitutional law at the University of Chicago Law School from 1992 to 2004.
6
- Turning to elective politics, he represented the 13th district in the Illinois Senate from 1997 until 2004, when he ran for the U.S.
7
- Senate.
8
- Obama received national attention in 2004 with his March Senate primary win, his well-received July Democratic National Convention keynote address, and his landslide November election to the Senate.
9
- In 2008, after a close primary campaign against Hillary Clinton, he was nominated by the Democratic Party for president and chose Joe Biden as his running mate.
10
- Obama was elected over Republican nominee John McCain in the presidential election and was inaugurated on January 20, 2009.
11
- Nine months later, he was named the 2009 Nobel Peace Prize laureate, a decision that drew a mixture of praise and criticism.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  """
13
 
14
 
 
1
+ # target_text = """\
2
+ # Obama was born in Honolulu, Hawaii.
3
+ # After graduating from Columbia University in 1983, he worked as a community organizer in Chicago.
4
+ # In 1988, he enrolled in Harvard Law School, where he was the first black president of the Harvard Law Review.
5
+ # After graduating, he became a civil rights attorney and an academic, teaching constitutional law at the University of Chicago Law School from 1992 to 2004.
6
+ # Turning to elective politics, he represented the 13th district in the Illinois Senate from 1997 until 2004, when he ran for the U.S.
7
+ # Senate.
8
+ # Obama received national attention in 2004 with his March Senate primary win, his well-received July Democratic National Convention keynote address, and his landslide November election to the Senate.
9
+ # In 2008, after a close primary campaign against Hillary Clinton, he was nominated by the Democratic Party for president and chose Joe Biden as his running mate.
10
+ # Obama was elected over Republican nominee John McCain in the presidential election and was inaugurated on January 20, 2009.
11
+ # Nine months later, he was named the 2009 Nobel Peace Prize laureate, a decision that drew a mixture of praise and criticism.
12
+ # """
13
+ # target_text = """\
14
+ # Biology
15
+ # Metapod is an insect Pokémon that resembles a green chrysalis.
16
+ # Its body is crescent-shaped with several segments making up the lower point.
17
+ # The front of Metapod's shell resembles a face with heavy-lidded eyes and a sharply pointed nose.
18
+ # The back of its shell consists of several geometrically shaped portions and projections.
19
+
20
+ # Metapod's soft body is protected by a hard outer shell as it undergoes metamorphosis.
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
 
src/utils.py CHANGED
@@ -11,7 +11,19 @@ from src.shared import token_id_predictions
11
  logger = logging.getLogger(__name__)
12
 
13
 
 
 
 
 
 
 
 
 
 
 
 
14
  def get_current_prompt_text(word_number):
 
15
  return shared.tokenizer.decode(shared.all_tokens[: STARTING_INDEX + word_number])
16
 
17
 
@@ -34,8 +46,9 @@ def lm_is_correct(params: ReducerParams) -> bool:
34
 
35
  idx = MAX_ATTEMPTS - params.remaining_attempts
36
 
37
- current_guess = token_id_predictions[params.word_number][1][idx]
38
- current_target = token_id_predictions[params.word_number][0]
 
39
 
40
  return current_guess == current_target
41
 
 
11
  logger = logging.getLogger(__name__)
12
 
13
 
14
+ def get_current_lm_guess_str(word_number, remaining_attempts):
15
+ # FIXME: indexerror
16
+ guess_list = token_id_predictions[(STARTING_INDEX + word_number) - 1][1]
17
+ guess_list = [shared.tokenizer.decode(i) for i in guess_list]
18
+ censored_list = ["*****"] * MAX_ATTEMPTS
19
+ for i in range(MAX_ATTEMPTS - remaining_attempts):
20
+ censored_list[i] = guess_list[i]
21
+
22
+ return "\n".join(censored_list)
23
+
24
+
25
  def get_current_prompt_text(word_number):
26
+ # FIXME: indexerror
27
  return shared.tokenizer.decode(shared.all_tokens[: STARTING_INDEX + word_number])
28
 
29
 
 
46
 
47
  idx = MAX_ATTEMPTS - params.remaining_attempts
48
 
49
+ # FIXME: indexerror
50
+ current_guess = token_id_predictions[STARTING_INDEX + params.word_number - 1][1][idx]
51
+ current_target = token_id_predictions[STARTING_INDEX + params.word_number - 1][0]
52
 
53
  return current_guess == current_target
54