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)