santit96's picture
Creation of an api module with a rest endpoint /suggest which receives a list of words and states and return a suggestion
1c007bb
raw
history blame
1.49 kB
from a3c.play import get_play_model_path, suggest
from flask import Flask, request, jsonify
from wordle_env.words import target_vocabulary
from wordle_env.wordle import get_env
app = Flask(__name__)
def validate_params(words, states):
# Check if the input lists are valid (i.e. all elements have length 5 and numbers are between 0 and 2 inclusive)
if not all(len(w) == 5 and w in target_vocabulary for w in words):
return True, 'Invalid input, words must be 5 characters long and must be an eligible word'
if not all(len(n) == 5 and all(c.isdigit() and 0 <= int(c) <= 2 for c in n) for n in states):
return True, 'Invalid input, states must be 5 characters long and the numbers between 0 and 2 inclusive'
return False, ''
@app.route('/suggest', methods=['GET'])
def get_suggestion():
# Get the list of words and list of number strings from the request
words = [word.strip().upper()
for word in request.args.get('words').split(',')]
states = [state.strip() for state in request.args.get('states').split(',')]
print(states)
error, msge = validate_params(words, states)
if error:
return jsonify({'error': msge}), 400
env = get_env()
model_path = get_play_model_path()
# Call the suggest function with the input lists and return the result
suggestion = suggest(env, words, states, model_path)
return jsonify({'suggestion': suggestion})
if __name__ == '__main__':
app.run(debug=True)