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