Spaces:
Sleeping
Sleeping
File size: 1,319 Bytes
192a1c0 2c9d2bf 31806c5 6377159 31806c5 2c9d2bf 31806c5 e92ba0d 6f847ac e92ba0d 30dd301 e92ba0d 24f2542 192a1c0 6f847ac f04bbb4 6377159 f04bbb4 6377159 deecb43 f04bbb4 3cab2dd ba1e2db 3cab2dd 6377159 3cab2dd c2ccf60 31806c5 3563daa b378fde |
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 |
from flask import Flask, request, jsonify, make_response, render_template
from flask_cors import CORS
from dataset.iris import iris
from opts import options
app = Flask(
__name__,
template_folder="templates",
)
CORS(app)
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"]
if "cluster" in request.json["algorithm"]:
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)
|