Spaces:
Sleeping
Sleeping
File size: 1,228 Bytes
192a1c0 31806c5 6377159 31806c5 e92ba0d 6f847ac e92ba0d 30dd301 e92ba0d 24f2542 192a1c0 6f847ac f04bbb4 6377159 f04bbb4 6377159 04b61ad f04bbb4 3cab2dd ba1e2db 3cab2dd 6377159 3cab2dd c2ccf60 31806c5 3563daa 04b61ad |
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 |
from flask import Flask, request, jsonify, make_response, render_template
from dataset.iris import iris
from opts import options
app = Flask(
__name__,
template_folder="templates",
)
def not_valid(params: dict):
"""
is_valid() simply checks if
an incoming response is valid
for this api or not. Key to
avoiding errors
"""
if "algorithm" not in params:
return "User did not specify the algorithm parameter"
if params["algorithm"] not in options:
return f"Invalid algorithm '{params['algorithm']}' is invalid."
return False
@app.route("/", methods=["POST", "GET"])
def index():
if request.method == "GET":
return render_template("index.html")
error_message = not_valid(params=request.json)
if error_message:
return make_response(error_message, 400)
# parse arguments
algorithm = options[request.json["algorithm"]]
args = request.json["arguments"]
args["algorithm"] = request.json["algorithm"]
# using the iris data set for every algorithm
X, y = iris()
result = algorithm(
X=X,
y=y,
args=args,
)
return jsonify(result)
if __name__ == "__main__":
app.run(debug=True)
|