File size: 2,141 Bytes
8e80adf
 
 
 
 
 
 
 
 
 
 
edd4bcc
 
 
8e80adf
b9c9b71
8e80adf
ed44d00
8e80adf
 
 
b9c9b71
8e80adf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
edd4bcc
ed44d00
8e80adf
 
 
ed44d00
8e80adf
 
 
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
from flask import Flask, request, jsonify, send_from_directory
from flask_cors import CORS
import os

from puzzle_dataset import get_puzzle_by_index
from solver import solve_puzzle

BASE_DIR = os.path.dirname(os.path.abspath(__file__))
example_path = os.path.join(BASE_DIR, "Example.txt")
with open(example_path, "r", encoding="utf-8") as f:
    DEFAULT_SYS_CONTENT = f.read()
sat_cnt_path = os.path.join(BASE_DIR, "Sat_cnt.txt")
with open(sat_cnt_path, "r", encoding="utf-8") as f:
    SAT_CNT_CONTENT = f.read()

# Only initialize once
app = Flask(__name__, static_folder='static', static_url_path='')
CORS(app)

@app.route('/')
def index():
    # Return built static index.html
    return send_from_directory(app.static_folder, 'index.html')

@app.route("/get_puzzle", methods=["GET"])
def get_puzzle():
    idx_str = request.args.get("index", "0")
    try:
        idx = int(idx_str)
    except ValueError:
        return jsonify({"success": False, "error": "Index must be an integer"}), 400

    puzzle, solution = get_puzzle_by_index(idx)
    if puzzle is None or solution is None:
        return jsonify({"success": False, "error": "Invalid puzzle index"}), 404

    return jsonify({
        "success": True,
        "index": idx,
        "puzzle": puzzle,
        "expected_solution": solution
    })

@app.route("/solve", methods=["POST"])
def solve():
    data = request.get_json()
    puzzle_index = data.get("index")
    puzzle_text = data.get("puzzle")
    expected_solution = data.get("expected_solution")
    sys_content = data.get("sys_content", DEFAULT_SYS_CONTENT)

    if puzzle_index is None or puzzle_text is None or expected_solution is None:
        return jsonify({"success": False, "error": "Missing puzzle data"}), 400

    result = solve_puzzle(puzzle_index, puzzle_text, expected_solution, sys_content, SAT_CNT_CONTENT)
    return jsonify({"success": True, "result": result})

@app.route("/default_sys_content", methods=["GET"])
def get_default_sys_content():
    return jsonify({"success": True, "sysContent": DEFAULT_SYS_CONTENT})

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=7860, debug=False)