Jensen-holm commited on
Commit
f31d9cf
·
1 Parent(s): ecb0c79

splitting up rest api functionality to make more readable

Browse files
Files changed (2) hide show
  1. app.py +26 -28
  2. cluster/main.py +3 -3
app.py CHANGED
@@ -1,53 +1,51 @@
1
- from flask import Flask, request, jsonify, make_response, render_template
2
  from flask_cors import CORS
3
 
4
  from dataset.iris import iris
5
  from opts import options
6
 
 
 
 
7
  app = Flask(
8
  __name__,
9
  template_folder="templates",
10
  )
11
 
12
- CORS(app, origins="*")
13
-
14
-
15
- def not_valid(params: dict):
16
- """
17
- is_valid() simply checks if
18
- an incoming response is valid
19
- for this api or not. Key to
20
- avoiding errors
21
- """
22
- if "algorithm" not in params:
23
- return "User did not specify the algorithm parameter"
24
-
25
- if params["algorithm"] not in options:
26
- return f"Invalid algorithm '{params['algorithm']}' is invalid."
27
- return False
28
 
29
 
30
- @app.route("/", methods=["POST", "GET"])
31
  def index():
32
- if request.method == "GET":
33
- return render_template("index.html")
34
 
35
- error_message = not_valid(params=request.json)
36
- if error_message:
37
- return make_response(error_message, 400)
38
 
 
 
39
  # parse arguments
40
- algorithm = options[request.json["algorithm"]]
41
  args = request.json["arguments"]
42
 
43
- if "cluster" in request.json["algorithm"]:
44
- args["algorithm"] = request.json["algorithm"]
 
 
 
 
 
 
 
 
 
 
45
 
46
- # using the iris data set for every algorithm
47
- X, y = iris()
48
  result = algorithm(
49
  X=X,
50
  y=y,
 
51
  args=args,
52
  )
53
  return jsonify(result)
 
1
+ from flask import Flask, request, jsonify, render_template
2
  from flask_cors import CORS
3
 
4
  from dataset.iris import iris
5
  from opts import options
6
 
7
+ # using the iris data set for every algorithm
8
+ X, y = iris()
9
+
10
  app = Flask(
11
  __name__,
12
  template_folder="templates",
13
  )
14
 
15
+ CORS(
16
+ app=app,
17
+ origins="*",
18
+ )
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
 
21
+ @app.route("/", methods=["GET"])
22
  def index():
23
+ return render_template("index.html")
 
24
 
 
 
 
25
 
26
+ @app.route("/neural-network", methods=["POST", "GET"])
27
+ def neural_network():
28
  # parse arguments
29
+ algorithm = options["neural-network"]
30
  args = request.json["arguments"]
31
 
32
+ result = algorithm(
33
+ X=X,
34
+ y=y,
35
+ args=args,
36
+ )
37
+ return jsonify(result)
38
+
39
+
40
+ @app.route("/kmeans-clustering", methods=["POST"])
41
+ def kmeans():
42
+ algorithm = options["kmeans-clustering"]
43
+ args = request.json["arguments"]
44
 
 
 
45
  result = algorithm(
46
  X=X,
47
  y=y,
48
+ clusterer="kmeans-clustering",
49
  args=args,
50
  )
51
  return jsonify(result)
cluster/main.py CHANGED
@@ -9,12 +9,12 @@ from cluster.plot import plot
9
  def main(
10
  X: np.array,
11
  y: np.array,
 
12
  args: dict,
13
  ) -> dict:
14
- cluster_func = args.pop("algorithm")
15
- cluster_alg: Clusterer = clustering_methods[cluster_func]
16
 
17
- args.update({"cluster_func": cluster_func})
18
  alg = cluster_alg.from_dict(args)
19
 
20
  alg.build(X)
 
9
  def main(
10
  X: np.array,
11
  y: np.array,
12
+ clusterer: str,
13
  args: dict,
14
  ) -> dict:
15
+ cluster_alg: Clusterer = clustering_methods[clusterer]
 
16
 
17
+ args.update({"cluster_func": cluster_alg})
18
  alg = cluster_alg.from_dict(args)
19
 
20
  alg.build(X)