Jensen-holm commited on
Commit
14841f9
1 Parent(s): 8c348c5

successful example

Browse files
app.py CHANGED
@@ -1,8 +1,10 @@
1
- from flask import Flask, request, jsonify
2
 
3
- import pandas as pd
4
  from nn.nn import NN
5
  from nn import train as train_nn
 
 
 
6
 
7
  app = Flask(__name__)
8
 
@@ -13,13 +15,30 @@ def neural_net():
13
 
14
  try:
15
  net = NN.from_dict(args)
16
- df = pd.read_csv(args.pop("data"))
17
  except Exception as e:
18
- return jsonify({
19
- "bad request": f"could not read csv data: {e}",
20
- })
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
- result = train_nn(nn=net)
23
  return jsonify(result)
24
 
25
 
 
1
+ from flask import Flask, request, jsonify, Response
2
 
 
3
  from nn.nn import NN
4
  from nn import train as train_nn
5
+ from nn import activation
6
+ import pandas as pd
7
+ import io
8
 
9
  app = Flask(__name__)
10
 
 
15
 
16
  try:
17
  net = NN.from_dict(args)
 
18
  except Exception as e:
19
+ return Response(
20
+ response=f"issue with request args: {e}",
21
+ status=400,
22
+ )
23
+
24
+ try:
25
+ df = pd.read_csv(io.StringIO(net.data))
26
+ net.set_df(df=df)
27
+ except Exception as e:
28
+ return Response(
29
+ response=f"error reading csv data: {e}",
30
+ status=400,
31
+ )
32
+
33
+ try:
34
+ activation.get_activation(nn=net)
35
+ except Exception as e:
36
+ return Response(
37
+ response="invalid activation function",
38
+ status=400,
39
+ )
40
 
41
+ result = train_nn.train(nn=net)
42
  return jsonify(result)
43
 
44
 
example/main.py CHANGED
@@ -14,10 +14,10 @@ ARGS = {
14
  "data": iris_data.decode("utf-8"),
15
  }
16
 
17
- r = requests.post(
18
- "http://127.0.0.1:5000/neural-network",
19
- json=ARGS,
20
- )
21
-
22
  if __name__ == "__main__":
23
- print(r.json())
 
 
 
 
 
 
14
  "data": iris_data.decode("utf-8"),
15
  }
16
 
 
 
 
 
 
17
  if __name__ == "__main__":
18
+ r = requests.post(
19
+ "http://127.0.0.1:5000/neural-network",
20
+ json=ARGS, # Send the data as a JSON object
21
+ )
22
+
23
+ print(r.text)
nn/__pycache__/activation.cpython-310.pyc ADDED
Binary file (1.3 kB). View file
 
nn/__pycache__/nn.cpython-310.pyc ADDED
Binary file (1.77 kB). View file
 
nn/__pycache__/train.cpython-310.pyc ADDED
Binary file (569 Bytes). View file
 
nn/activation.py CHANGED
@@ -18,7 +18,7 @@ def get_activation(nn: NN) -> Callable:
18
  }
19
 
20
  nn.set_func(funcs[a])
21
- nn.set_func_prime(funcs[a])
22
 
23
 
24
  def relu(x):
 
18
  }
19
 
20
  nn.set_func(funcs[a])
21
+ nn.set_func_prime(prime_funcs[a])
22
 
23
 
24
  def relu(x):
nn/nn.py CHANGED
@@ -14,11 +14,6 @@ class NN:
14
  features: list[str],
15
  target: str,
16
  data: str,
17
-
18
- wh: np.array,
19
- wo: np.array,
20
- bh: np.array,
21
- bo: np.array,
22
  ):
23
  self.epochs = epochs
24
  self.hidden_size = hidden_size
@@ -28,21 +23,22 @@ class NN:
28
  self.features = features
29
  self.target = target
30
  self.data = data
31
- self.wh: np.array = wh
32
- self.wo: np.array = wo
33
- self.bh: np.array = bh
34
- self.bo: np.array = bo
35
 
 
 
 
 
36
  self.func_prime: Callable = None
37
  self.func: Callable = None
38
  self.df: pd.DataFrame = None
39
  self.X: pd.DataFrame = None
40
  self.y: pd.DataFrame = None
41
 
42
- def read_csv(self) -> dict[str, str]:
43
- self.df = pd.read_csv(self.data)
44
- self.X = self.df[self.features]
45
- self.y = self.df[self.target]
 
46
 
47
  def set_func(self, f: Callable) -> None:
48
  assert isinstance(f, Callable)
 
14
  features: list[str],
15
  target: str,
16
  data: str,
 
 
 
 
 
17
  ):
18
  self.epochs = epochs
19
  self.hidden_size = hidden_size
 
23
  self.features = features
24
  self.target = target
25
  self.data = data
 
 
 
 
26
 
27
+ self.wh: np.array = None
28
+ self.wo: np.array = None
29
+ self.bh: np.array = None
30
+ self.bo: np.array = None
31
  self.func_prime: Callable = None
32
  self.func: Callable = None
33
  self.df: pd.DataFrame = None
34
  self.X: pd.DataFrame = None
35
  self.y: pd.DataFrame = None
36
 
37
+ def set_df(self, df: pd.DataFrame) -> None:
38
+ assert isinstance(df, pd.DataFrame)
39
+ self.df = df
40
+ self.X = df[self.features]
41
+ self.y = df[self.target]
42
 
43
  def set_func(self, f: Callable) -> None:
44
  assert isinstance(f, Callable)