Spaces:
Build error
Build error
Commit
·
6f847ac
1
Parent(s):
c2ccf60
setting it up as a straightforward rest api
Browse files
app.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
-
from flask import Flask, request
|
| 2 |
-
import numpy as np
|
| 3 |
|
|
|
|
| 4 |
from opts import options
|
| 5 |
|
| 6 |
app = Flask(
|
|
@@ -10,25 +10,43 @@ app = Flask(
|
|
| 10 |
)
|
| 11 |
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
|
| 17 |
-
def is_valid():
|
| 18 |
-
if request.method == "GET":
|
| 19 |
-
return True
|
| 20 |
return False
|
| 21 |
|
| 22 |
-
if not
|
| 23 |
-
return
|
|
|
|
| 24 |
|
| 25 |
-
# parse arguments
|
| 26 |
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
return
|
| 32 |
|
| 33 |
|
| 34 |
if __name__ == '__main__':
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify, make_response
|
|
|
|
| 2 |
|
| 3 |
+
from dataset.random import random_dataset
|
| 4 |
from opts import options
|
| 5 |
|
| 6 |
app = Flask(
|
|
|
|
| 10 |
)
|
| 11 |
|
| 12 |
|
| 13 |
+
def is_valid(params: dict):
|
| 14 |
+
"""
|
| 15 |
+
is_valid() simply checks if
|
| 16 |
+
an incoming response is valid
|
| 17 |
+
for this api or not. Key to
|
| 18 |
+
avoiding errors
|
| 19 |
+
"""
|
| 20 |
+
if not request.json():
|
| 21 |
+
return False
|
| 22 |
|
| 23 |
+
if "Algorithm" not in params:
|
|
|
|
|
|
|
|
|
|
| 24 |
return False
|
| 25 |
|
| 26 |
+
if params["Algorithm"] not in options:
|
| 27 |
+
return False
|
| 28 |
+
return True
|
| 29 |
|
|
|
|
| 30 |
|
| 31 |
+
@app.route("/", methods=["GET"])
|
| 32 |
+
def index():
|
| 33 |
+
|
| 34 |
+
params = request.json()
|
| 35 |
+
if not is_valid(params=params):
|
| 36 |
+
return make_response(400, "bad request")
|
| 37 |
|
| 38 |
+
# parse arguments
|
| 39 |
+
algorithm = options[params["Algorithm"]]
|
| 40 |
+
args = options[params["Arguments"]]
|
| 41 |
+
|
| 42 |
+
X, y = random_dataset()
|
| 43 |
+
results = algorithm.main(
|
| 44 |
+
X=X,
|
| 45 |
+
y=y,
|
| 46 |
+
args=args,
|
| 47 |
+
)
|
| 48 |
|
| 49 |
+
return jsonify(results)
|
| 50 |
|
| 51 |
|
| 52 |
if __name__ == '__main__':
|