File size: 1,490 Bytes
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
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)