Inkcap commited on
Commit
3425e94
1 Parent(s): a1df8df

Renamed model_runner, changed print to st.write()

Browse files
Files changed (3) hide show
  1. app.py +133 -5
  2. model-runner.py +0 -135
  3. src/interface_experiment.py +7 -2
app.py CHANGED
@@ -1,8 +1,136 @@
 
 
1
  import streamlit as st
2
 
3
- from transformers import pipeline
4
- generator2 = pipeline('text-generation', model='gpt2')
 
 
 
 
5
 
6
- prompt = "Return an opening move in chess."
7
- generated_text = generator2(prompt, max_length=100, num_return_sequences=1)[0]['generated_text']
8
- st.write(generated_text)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import chess
2
+ import random
3
  import streamlit as st
4
 
5
+ def cleanup_output(text, prompt, extra_len):
6
+ section = text[len(prompt):]
7
+ st.write("Proposed Move: " + section)
8
+ valid_letters = ['A','a','B','b','C','c','D','d','E','e','F','f','G','g','H','h']
9
+ valid_pieces = ['p','P','k','K','q','Q','r','R','b','B', 'n', 'N']
10
+ valid_numbers = ['1','2','3','4','5','6','7','8']
11
 
12
+ #if there are any syntatically moves in this string for pieces
13
+ countr = 0
14
+ while countr < len(section) - 3:
15
+ if(section[countr] in valid_pieces and section[countr + 1] in valid_letters and section[countr + 2] in valid_numbers):
16
+ #print(section[countr:countr+3])
17
+ return ' ' + section[countr:countr+3]
18
+ countr+=1
19
+
20
+ #variant for capturing!
21
+ countr = 0
22
+ while countr < len(section) - 4:
23
+ if(section[countr] in valid_pieces and section[countr + 1] == 'x' and section[countr + 2] in valid_letters and section[countr + 3] in valid_numbers):
24
+ #print(section[countr:countr+3])
25
+ return ' ' + section[countr:countr+5]
26
+ countr+=1
27
+
28
+ #same as moves but for pawns
29
+ countr = 0
30
+ while countr < len(section) - 2:
31
+ if(section[countr] in valid_letters and section[countr+1] in valid_numbers):
32
+ #print(section[countr:countr+2])
33
+ return ' ' + section[countr:countr+2]
34
+ countr+=1
35
+
36
+ #variant for capturing!
37
+ countr = 0
38
+ while countr < len(section) -4:
39
+ if(section[countr] in valid_letters and section[countr+1] == 'x' and section[countr+2] in valid_letters and section[countr + 3] in valid_numbers):
40
+ #print(section[countr:countr+2])
41
+ return ' ' + section[countr:countr+4]
42
+ countr+=1
43
+
44
+ return ' e8'
45
+
46
+ class AInstance:
47
+ def __init__(self, type, generator):
48
+ self.type = type
49
+ self.game_end = False
50
+ self.generator = generator
51
+
52
+ #All this does it take the gamestate and add the ai-generated result to it
53
+ def move(self, game_state):
54
+ if(self.type == "gpt2-medium-chess"):
55
+ prompt = "1-0 2700 1350 " + game_state
56
+ extra_len = 7
57
+ else:
58
+ prompt = game_state
59
+ extra_len = 5
60
+ countr = 0
61
+ while True:
62
+ generated_text = self.generator(prompt, max_length=len(prompt) + extra_len, num_return_sequences=1)[0]['generated_text']
63
+ selected_move = cleanup_output(generated_text, prompt, extra_len)
64
+
65
+ #if this move is valid then return it
66
+ proposed_board = game_state + selected_move
67
+ if(verify_move(proposed_board)):
68
+ return proposed_board
69
+ countr+=1
70
+ #goes fifty times until the AInstance object flags itself as "ended" (fundamentally unable to make a valid move)
71
+ if(countr > 50):
72
+ self.game_end = True
73
+ break
74
+
75
+ def check_if_end(self):
76
+ return self.game_end
77
+
78
+ def verify_move(string):
79
+ board = chess.Board()
80
+ st.write("Board: " + string + "\n")
81
+ for move in string.split():
82
+ #if this move makes no sense it will return false and the game will try again to generate a good move
83
+ try:
84
+ board.push_san(move)
85
+ except:
86
+ return False
87
+ if(board.is_valid):
88
+ return True
89
+ return False
90
+
91
+ def check_mate(string):
92
+ #simulates mate idk
93
+ if(random.randrange(0,100) == 4):
94
+ st.write("H")
95
+ return True
96
+ return False
97
+
98
+ def print_game(string):
99
+ st.write("Some kind of visualization for the chess board based on this string: " + string)
100
+
101
+ def make_move(instance, game_state):
102
+ st.write(instance.type + "'s move")
103
+ return_state = game_state
104
+ return_state = instance.move(game_state)
105
+ game_ongoing = True
106
+ if(instance.check_if_end()):
107
+ st.write("This player claims they can't make a valid move after 50 tries: " + instance.type)
108
+ game_ongoing = False
109
+ if(check_mate(return_state)):
110
+ st.write("This player claims mates: " + instance.type)
111
+ game_ongoing = False
112
+ return(return_state, game_ongoing)
113
+
114
+
115
+ def main():
116
+ if(random.randint(0,1) == 1):
117
+ white = AInstance("gpt2", generator)
118
+ black = AInstance("gpt2-medium-chess", generator2)
119
+ st.write("Gpt2 is White and Gpt2 Optimized is Black\n")
120
+ else:
121
+ white = AInstance("gpt2-medium-chess", generator2)
122
+ black = AInstance("gpt2", generator)
123
+ st.write("Gpt2 is Black and Gpt2 Optimized is White\n")
124
+
125
+ game_state = "e4 e5"
126
+ game_ongoing = True
127
+ while game_ongoing:
128
+ game_state, game_ongoing = make_move(white, game_state)
129
+ if not game_ongoing:
130
+ print_game(game_state)
131
+ break
132
+ game_state, game_ongoing = make_move(black, game_state)
133
+ if not game_ongoing:
134
+ print_game(game_state)
135
+ break
136
+ main()
model-runner.py DELETED
@@ -1,135 +0,0 @@
1
- import chess
2
- import random
3
-
4
- def cleanup_output(text, prompt, extra_len):
5
- section = text[len(prompt):]
6
- print("Proposed Move: %s" % section)
7
- valid_letters = ['A','a','B','b','C','c','D','d','E','e','F','f','G','g','H','h']
8
- valid_pieces = ['p','P','k','K','q','Q','r','R','b','B', 'n', 'N']
9
- valid_numbers = ['1','2','3','4','5','6','7','8']
10
-
11
- #if there are any syntatically moves in this string for pieces
12
- countr = 0
13
- while countr < len(section) - 3:
14
- if(section[countr] in valid_pieces and section[countr + 1] in valid_letters and section[countr + 2] in valid_numbers):
15
- #print(section[countr:countr+3])
16
- return ' ' + section[countr:countr+3]
17
- countr+=1
18
-
19
- #variant for capturing!
20
- countr = 0
21
- while countr < len(section) - 4:
22
- if(section[countr] in valid_pieces and section[countr + 1] == 'x' and section[countr + 2] in valid_letters and section[countr + 3] in valid_numbers):
23
- #print(section[countr:countr+3])
24
- return ' ' + section[countr:countr+5]
25
- countr+=1
26
-
27
- #same as moves but for pawns
28
- countr = 0
29
- while countr < len(section) - 2:
30
- if(section[countr] in valid_letters and section[countr+1] in valid_numbers):
31
- #print(section[countr:countr+2])
32
- return ' ' + section[countr:countr+2]
33
- countr+=1
34
-
35
- #variant for capturing!
36
- countr = 0
37
- while countr < len(section) -4:
38
- if(section[countr] in valid_letters and section[countr+1] == 'x' and section[countr+2] in valid_letters and section[countr + 3] in valid_numbers):
39
- #print(section[countr:countr+2])
40
- return ' ' + section[countr:countr+4]
41
- countr+=1
42
-
43
- return ' e8'
44
-
45
- class AInstance:
46
- def __init__(self, type, generator):
47
- self.type = type
48
- self.game_end = False
49
- self.generator = generator
50
-
51
- #All this does it take the gamestate and add the ai-generated result to it
52
- def move(self, game_state):
53
- if(self.type == "gpt2-medium-chess"):
54
- prompt = "1-0 2700 1350 " + game_state
55
- extra_len = 7
56
- else:
57
- prompt = game_state
58
- extra_len = 5
59
- countr = 0
60
- while True:
61
- generated_text = self.generator(prompt, max_length=len(prompt) + extra_len, num_return_sequences=1)[0]['generated_text']
62
- selected_move = cleanup_output(generated_text, prompt, extra_len)
63
-
64
- #if this move is valid then return it
65
- proposed_board = game_state + selected_move
66
- if(verify_move(proposed_board)):
67
- return proposed_board
68
- countr+=1
69
- #goes fifty times until the AInstance object flags itself as "ended" (fundamentally unable to make a valid move)
70
- if(countr > 50):
71
- self.game_end = True
72
- break
73
-
74
- def check_if_end(self):
75
- return self.game_end
76
-
77
- def verify_move(string):
78
- board = chess.Board()
79
- print("Board: %s\n" % string)
80
- for move in string.split():
81
- #if this move makes no sense it will return false and the game will try again to generate a good move
82
- try:
83
- board.push_san(move)
84
- except:
85
- return False
86
- if(board.is_valid):
87
- return True
88
- return False
89
-
90
- def check_mate(string):
91
- #simulates mate idk
92
- if(random.randrange(0,100) == 4):
93
- print("H")
94
- return True
95
- return False
96
-
97
- def print_game(string):
98
- print("Some kind of visualization for the chess board based on this string: %s" % string)
99
-
100
- def make_move(instance, game_state):
101
- print("%s's move" % instance.type)
102
- return_state = game_state
103
- return_state = instance.move(game_state)
104
- game_ongoing = True
105
- if(instance.check_if_end()):
106
- print("This player claims they can't make a valid move after 50 tries: %s" % instance.type)
107
- game_ongoing = False
108
- if(check_mate(return_state)):
109
- print("This player claims mates: %s" % instance.type)
110
- game_ongoing = False
111
- return(return_state, game_ongoing)
112
-
113
-
114
- def main():
115
- if(random.randint(0,1) == 1):
116
- white = AInstance("gpt2", generator)
117
- black = AInstance("gpt2-medium-chess", generator2)
118
- print("Gpt2 is White and Gpt2 Optimized is Black\n")
119
- else:
120
- white = AInstance("gpt2-medium-chess", generator2)
121
- black = AInstance("gpt2", generator)
122
- print("Gpt2 is Black and Gpt2 Optimized is White\n")
123
-
124
- game_state = "e4 e5"
125
- game_ongoing = True
126
- while game_ongoing:
127
- game_state, game_ongoing = make_move(white, game_state)
128
- if not game_ongoing:
129
- print_game(game_state)
130
- break
131
- game_state, game_ongoing = make_move(black, game_state)
132
- if not game_ongoing:
133
- print_game(game_state)
134
- break
135
- main()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
src/interface_experiment.py CHANGED
@@ -4,5 +4,10 @@ model_id = "BlueSunflower/gpt2-medium-chess"
4
  text_generator = transformers.pipeline("text-generation", model = model_id)
5
  input_text = "1-0 2995 3110 1.e4 e5 2.Nf3 Nc6"
6
 
7
- generated = text_generator(input_text, max_new_tokens = 500)[0]["generated_text"]
8
- print(generated)
 
 
 
 
 
 
4
  text_generator = transformers.pipeline("text-generation", model = model_id)
5
  input_text = "1-0 2995 3110 1.e4 e5 2.Nf3 Nc6"
6
 
7
+ for i in range(5):
8
+ generated = text_generator(input_text, max_new_tokens = 10)[0]["generated_text"]
9
+ new_string = str(generated).replace(input_text, "")
10
+ move = new_string.split(" ", maxsplit=1)[1]
11
+ input_text += move
12
+ print(input_text)
13
+