Spaces:
Runtime error
Runtime error
Silas Blanchard commited on
Update model-runner.py
Browse files- src/model-runner.py +31 -13
src/model-runner.py
CHANGED
|
@@ -1,28 +1,44 @@
|
|
| 1 |
import chess
|
| 2 |
import random
|
| 3 |
|
| 4 |
-
def cleanup_output(text, prompt):
|
| 5 |
-
section = text[len(prompt):
|
| 6 |
-
|
| 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 <
|
| 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 |
#same as moves but for pawns
|
| 20 |
countr = 0
|
| 21 |
-
while countr <
|
| 22 |
if(section[countr] in valid_letters and section[countr+1] in valid_numbers):
|
| 23 |
#print(section[countr:countr+2])
|
| 24 |
return ' ' + section[countr:countr+2]
|
| 25 |
countr+=1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
return ' e8'
|
| 28 |
|
|
@@ -34,14 +50,16 @@ class AInstance:
|
|
| 34 |
|
| 35 |
#All this does it take the gamestate and add the ai-generated result to it
|
| 36 |
def move(self, game_state):
|
| 37 |
-
if(type == "
|
| 38 |
prompt = "1-0 2700 1350 " + game_state
|
|
|
|
| 39 |
else:
|
| 40 |
prompt = game_state
|
|
|
|
| 41 |
countr = 0
|
| 42 |
while True:
|
| 43 |
-
generated_text = self.generator(prompt, max_length=len(prompt) +
|
| 44 |
-
selected_move = cleanup_output(generated_text, prompt)
|
| 45 |
|
| 46 |
#if this move is valid then return it
|
| 47 |
proposed_board = game_state + selected_move
|
|
@@ -58,7 +76,7 @@ class AInstance:
|
|
| 58 |
|
| 59 |
def verify_move(string):
|
| 60 |
board = chess.Board()
|
| 61 |
-
print("Board: %s" % string)
|
| 62 |
for move in string.split():
|
| 63 |
#if this move makes no sense it will return false and the game will try again to generate a good move
|
| 64 |
try:
|
|
@@ -85,7 +103,7 @@ def make_move(instance, game_state):
|
|
| 85 |
return_state = instance.move(game_state)
|
| 86 |
game_ongoing = True
|
| 87 |
if(instance.check_if_end()):
|
| 88 |
-
print("This player claims
|
| 89 |
game_ongoing = False
|
| 90 |
if(check_mate(return_state)):
|
| 91 |
print("This player claims mates: %s" % instance.type)
|
|
@@ -94,14 +112,14 @@ def make_move(instance, game_state):
|
|
| 94 |
|
| 95 |
|
| 96 |
def main():
|
| 97 |
-
if(random.
|
| 98 |
white = AInstance("gpt2", generator)
|
| 99 |
black = AInstance("gpt2-medium-chess", generator2)
|
| 100 |
-
print("Gpt2 is White and Gpt2 Optimized is Black")
|
| 101 |
else:
|
| 102 |
white = AInstance("gpt2-medium-chess", generator2)
|
| 103 |
black = AInstance("gpt2", generator)
|
| 104 |
-
print("Gpt2 is Black and Gpt2 Optimized is White")
|
| 105 |
|
| 106 |
game_state = "e4 e5"
|
| 107 |
game_ongoing = True
|
|
|
|
| 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 |
|
|
|
|
| 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
|
|
|
|
| 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:
|
|
|
|
| 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)
|
|
|
|
| 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
|