Silas-Blanchard commited on
Commit
739be90
1 Parent(s): c8c0f09

Update app.py

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