puzzler / app.py
christopher's picture
Update app.py
dde8525 verified
import gradio as gr
import chess.pgn
from io import StringIO
from chess_puzzler import Generator, open_engine
from chess_puzzler.tagger import cook
def find_puzzles(pgn_text):
game = chess.pgn.read_game(StringIO(pgn_text))
if not game:
return None
engine = open_engine("/usr/games/stockfish", threads=2)
try:
puzzles = Generator(engine).analyze_game(game, tier=10, all_puzzles=True)
if not puzzles:
return None
for puzzle in puzzles:
puzzle.tags = cook(puzzle, engine=engine)
return [puzzle.to_dict() for puzzle in puzzles]
finally:
engine.close()
gr.Interface(
fn=find_puzzles,
inputs=gr.Textbox(label="Paste PGN", lines=10),
outputs=gr.JSON(label="Puzzle"),
).launch()