Repository navigation
Expand file tree
/
Copy pathweb_demo.py
More file actions
75 lines (57 loc) · 2.07 KB
/
Copy pathweb_demo.py
File metadata and controls
75 lines (57 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from flask import Flask
from flask import jsonify, request
from flask import redirect, url_for
import go
import web_player
import myconf
import k2net as dual_net
from absl import logging, flags
import absl.app
static_path = '/Users/hyu/PycharmProjects/dlgo/deep_learning_and_the_game_of_go/code/dlgo/httpfrontend/static'
app = Flask(__name__, static_folder=static_path)
player = None # type: web_player.WebPlayer
def init():
# needs flags
global player
model_id = 'model12_2' # almost elo5k
model_id = 'model8_4' # elo4k
model_id = 'model9_4'
model_fname = '/Users/hyu/PycharmProjects/a0-jax/exp-go9/tfmodel/model-218'
model_fname = f'{myconf.EXP_HOME}/../9x9-exp2/checkpoints/{model_id}.mlpackage'
dnn = dual_net.load_net(model_fname)
logging.info('mcts %s #readouts=%d', model_id, flags.FLAGS.num_readouts)
player = web_player.WebPlayer(dnn)
@app.route('/')
def index():
# return redirect(url_for('static', filename='play_agents_5.html'))
return redirect(url_for('static', filename='play_agents_9.html'))
@app.route('/select-move/<bot_name>', methods=['POST'])
def select_move(bot_name):
content = request.json
board_size, moves_history = content['board_size'], content['moves']
assert board_size == go.N
move, info = player.select_move(moves_history)
return jsonify({
'bot_move': move,
'info': info
})
@app.route('/save-sgf/<fname>', methods=['POST'])
def save_sgf(fname):
from sgf_wrapper import make_sgf_from_gtp_moves
content = request.json
board_size, gtp_moves = content['board_size'], content['moves']
assert board_size == go.N
sgf_str = make_sgf_from_gtp_moves(gtp_moves, 0)
fname = '/Users/hyu/Downloads/web_demo.save.sgf'
logging.info(f'Saving to {fname}: %d moves', len(gtp_moves))
with open(fname, 'w') as f:
f.write(sgf_str)
return jsonify(({
'filename': fname
}))
def main(argv):
init()
app.run(port=5000, threaded=False)
# app.run(host='0.0.0.0', port=9999, threaded=False)
if __name__ == '__main__':
absl.app.run(main)