File size: 1,738 Bytes
5e71af7
c412087
 
a202b6d
c412087
 
1c007bb
c412087
1c007bb
 
 
 
a202b6d
 
c412087
a202b6d
c412087
 
1c007bb
 
c412087
 
a202b6d
5e71af7
c412087
a202b6d
 
1c007bb
c412087
1c007bb
a202b6d
1c007bb
 
c10a05f
 
a202b6d
c412087
1c007bb
 
c412087
 
5e71af7
d5ef8f7
5e71af7
 
c412087
5e71af7
 
d5ef8f7
 
 
 
 
 
 
 
 
 
 
 
 
 
c412087
1c007bb
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
import random

from flask import Flask, jsonify, request
from flask_cors import cross_origin

from a3c.play import get_play_model_path, play
from wordle_env.wordle import get_env
from wordle_env.words import target_vocabulary

app = Flask(__name__)


def validate_goal_word(word):
    if not word:
        return True, "Goal word not provided"
    if word.upper() not in target_vocabulary:
        return True, "Goal word not in vocabulary"
    return False, ""


@app.route("/play_word", methods=["GET"])
@cross_origin(origin="*", headers=["Content-Type", "Authorization"])
def get_play():
    # Get the goal word from the request
    word = request.args.get("goal_word")

    error, msge = validate_goal_word(word)
    if error:
        return jsonify({"error": msge}), 400

    word = word.upper()
    env = get_env()
    model_path = get_play_model_path()
    # Call the play function with the goal word
    # and return the attempts and the result
    won, attempts = play(env, model_path, word)
    return jsonify({"attempts": attempts, "won": won})


@app.route("/word", methods=["GET"])
@cross_origin(origin="*", headers=["Content-Type", "Authorization"])
def get_word():
    # Get a random word from the target vocabulary used to train the model
    word = random.choice(target_vocabulary)
    word = word.upper()
    return jsonify({"word": word})


def create_app(settings_override=None):
    """
    Create a Flask application using the app factory pattern.
    :param settings_override: Override settings
    :return: Flask app
    """
    # app.config.from_object("config.settings")

    if settings_override:
        app.config.update(settings_override)

    return app


if __name__ == "__main__":
    app.run(debug=True)