christopher commited on
Commit
9612e8f
·
1 Parent(s): 7ad253b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -52
app.py CHANGED
@@ -48,58 +48,25 @@ templates = Jinja2Templates(directory="templates")
48
  def read_root(request: Request):
49
  return templates.TemplateResponse("index.html", {"request": request})
50
 
51
-
52
-
53
- # @app.post("/search")
54
- # async def search(data: dict):
55
- # start = time.time()
56
- # board = chess.Board(data['fen'])
57
- # query_tokens = board_to_tokens(board)
58
- # matches = query_positions(index, metadata, query_tokens)
59
-
60
- # seen_puzzles = {}
61
- # for pos_id, (puzzle_row, move_idx) in matches:
62
- # if puzzle_row not in seen_puzzles:
63
- # seen_puzzles[puzzle_row] = (pos_id, move_idx)
64
-
65
- # results = []
66
- # for puzzle_row, (pos_id, move_idx) in seen_puzzles.items():
67
- # row = dset[puzzle_row]
68
- # positions = get_puzzle_positions(row['FEN'], row['Moves'])
69
- # matched_board = positions[move_idx]
70
-
71
- # results.append({
72
- # "PuzzleId": row['PuzzleId'],
73
- # "FEN": matched_board.fen(),
74
- # "Moves": row['Moves'],
75
- # "Rating": row['Rating'],
76
- # "Popularity": row['Popularity'],
77
- # "Themes": row['Themes'],
78
- # "MatchedMove": move_idx
79
- # })
80
-
81
- # elapsed_ms = (time.time() - start) * 1000
82
- # return {"count": len(results), "results": results, "time_ms": elapsed_ms}
83
-
84
-
85
  @app.post("/search")
86
  async def search(data: dict):
87
- async def generate():
88
- board = chess.Board(data['fen'])
89
- query_tokens = board_to_tokens(board)
90
- matches = query_positions(index, metadata, query_tokens)
91
-
92
- seen_puzzles = {}
93
- for pos_id, (puzzle_row, move_idx) in matches:
94
- if puzzle_row not in seen_puzzles:
95
- seen_puzzles[puzzle_row] = (pos_id, move_idx)
 
 
 
 
 
 
96
 
97
- for puzzle_row, (pos_id, move_idx) in seen_puzzles.items():
98
- row = dset[puzzle_row]
99
- positions = get_puzzle_positions(row['FEN'], row['Moves'])
100
- matched_board = positions[move_idx]
101
-
102
- result = {
103
  "PuzzleId": row['PuzzleId'],
104
  "FEN": matched_board.fen(),
105
  "Moves": row['Moves'],
@@ -107,7 +74,7 @@ async def search(data: dict):
107
  "Popularity": row['Popularity'],
108
  "Themes": row['Themes'],
109
  "MatchedMove": move_idx
110
- }
111
- yield json.dumps(result) + "\n"
112
 
113
- return StreamingResponse(generate(), media_type="application/x-ndjson")
 
 
48
  def read_root(request: Request):
49
  return templates.TemplateResponse("index.html", {"request": request})
50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  @app.post("/search")
52
  async def search(data: dict):
53
+ start = time.time()
54
+ board = chess.Board(data['fen'])
55
+ query_tokens = board_to_tokens(board)
56
+ matches = query_positions(index, metadata, query_tokens)
57
+
58
+ seen_puzzles = {}
59
+ for pos_id, (puzzle_row, move_idx) in matches:
60
+ if puzzle_row not in seen_puzzles:
61
+ seen_puzzles[puzzle_row] = (pos_id, move_idx)
62
+
63
+ results = []
64
+ for puzzle_row, (pos_id, move_idx) in seen_puzzles.items():
65
+ row = dset[puzzle_row]
66
+ positions = get_puzzle_positions(row['FEN'], row['Moves'])
67
+ matched_board = positions[move_idx]
68
 
69
+ results.append({
 
 
 
 
 
70
  "PuzzleId": row['PuzzleId'],
71
  "FEN": matched_board.fen(),
72
  "Moves": row['Moves'],
 
74
  "Popularity": row['Popularity'],
75
  "Themes": row['Themes'],
76
  "MatchedMove": move_idx
77
+ })
 
78
 
79
+ elapsed_ms = (time.time() - start) * 1000
80
+ return {"count": len(results), "results": results, "time_ms": elapsed_ms}