Jensen-holm commited on
Commit
31806c5
·
1 Parent(s): 59b5607

working on making it just a flask app, got rid of the main.py stuff

Browse files
Files changed (8) hide show
  1. app.py +36 -4
  2. build_dev.sh +2 -0
  3. main.py +0 -27
  4. neural_network/main.py +3 -19
  5. opts.py +3 -2
  6. run.sh +3 -0
  7. run_dev.sh +0 -30
  8. templates/index.html +29 -13
app.py CHANGED
@@ -1,12 +1,44 @@
1
- from flask import Flask, render_template
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
- app = Flask(__name__)
4
 
5
  @app.route("/")
6
- @app.route("/index")
7
  def index():
8
  return render_template("index.html")
9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  if __name__ == '__main__':
11
  app.run(debug=True)
12
-
 
1
+ from flask import Flask, render_template, request
2
+ import numpy as np
3
+
4
+ from opts import options
5
+
6
+ app = Flask(
7
+ __name__,
8
+ static_folder="static",
9
+ template_folder="templates",
10
+ )
11
+
12
+
13
+ def random_dataset(rows: int, features: int):
14
+ """
15
+ Initializes a training and a testing dataset in the form of numpy arrays
16
+ """
17
+ rng = np.random.default_rng()
18
+ X = rng.normal(size=(rows, features))
19
+ y = rng.integers(5, size=(rows, 1))
20
+ return X, y
21
 
 
22
 
23
  @app.route("/")
 
24
  def index():
25
  return render_template("index.html")
26
 
27
+
28
+ @app.route("/process_algorithm", methods=["GET", "POST"])
29
+ def process_algorithm():
30
+ alg = request.form.get('model-select')
31
+ func = options[alg]
32
+
33
+ # have a form for options based on the algorithm the user chose
34
+ # and set it as the args variable, make a 'go' button for this funcitonality
35
+ # to start the algorithm
36
+ args = 0
37
+ if args:
38
+ # create random numpy array dataset
39
+ X, y = random_dataset(100, 3)
40
+ func(X, y, args)
41
+
42
+
43
  if __name__ == '__main__':
44
  app.run(debug=True)
 
build_dev.sh ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ #!/bin/bash
2
+ npx tailwindcss -i ./static/src/input.css -o ./static/dist/css/output.css --watch
main.py DELETED
@@ -1,27 +0,0 @@
1
- from opts import options
2
- import numpy as np
3
-
4
-
5
- def random_dataset(rows: int, features: int):
6
- """
7
- Initializes a training and a testing dataset in the form of numpy arrays
8
- """
9
- rng = np.random.default_rng()
10
- X = rng.normal(size=(rows, features))
11
- y = rng.integers(5, size=(rows, 1))
12
- return X, y
13
-
14
-
15
- def main():
16
- method = input("\nChoose a method to test: ").lower()
17
- try:
18
- func = options[method]
19
- except KeyError:
20
- raise f"Invalid method \"{method}\". Try one of these\n{list(options.keys())}"
21
-
22
- X, y = random_dataset(rows=1000, features=10)
23
- func(X, y)
24
-
25
-
26
- if __name__ == "__main__":
27
- main()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
neural_network/main.py CHANGED
@@ -1,25 +1,10 @@
1
  from sklearn.model_selection import train_test_split
2
- import matplotlib.pyplot as plt
3
  import numpy as np
4
 
5
  from neural_network.opts import activation
6
  from neural_network.backprop import bp
7
  from neural_network.model import Network
8
- from neural_network.plot import loss_history_plt
9
-
10
-
11
- def get_args() -> dict:
12
- """
13
- returns a dictionary containing
14
- the arguments to be passed to
15
- the main function
16
- """
17
- return {
18
- "epochs": int(input("Enter the number of epochs: ")),
19
- "hidden_size": int(input("Enter the number of hidden nodes: ")),
20
- "learning_rate": float(input("Enter the learning rate: ")),
21
- "activation_func": input("Enter the activation function: "),
22
- }
23
 
24
 
25
  def init(X: np.array, hidden_size: int) -> dict:
@@ -38,8 +23,8 @@ def init(X: np.array, hidden_size: int) -> dict:
38
  def main(
39
  X: np.array,
40
  y: np.array,
 
41
  ) -> None:
42
- args = get_args()
43
  wb = init(X, args["hidden_size"])
44
  X_train, X_test, y_train, y_test = train_test_split(
45
  X,
@@ -65,5 +50,4 @@ def main(
65
  # plot predicted versus actual
66
  # also plot the training loss over epochs
67
  animated_loss_plt = loss_history_plt(loss_history)
68
- # eventually we will save this plot
69
- plt.show()
 
1
  from sklearn.model_selection import train_test_split
 
2
  import numpy as np
3
 
4
  from neural_network.opts import activation
5
  from neural_network.backprop import bp
6
  from neural_network.model import Network
7
+ from neural_network.plot import loss_history_plt, save_plt
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
 
10
  def init(X: np.array, hidden_size: int) -> dict:
 
23
  def main(
24
  X: np.array,
25
  y: np.array,
26
+ args,
27
  ) -> None:
 
28
  wb = init(X, args["hidden_size"])
29
  X_train, X_test, y_train, y_test = train_test_split(
30
  X,
 
50
  # plot predicted versus actual
51
  # also plot the training loss over epochs
52
  animated_loss_plt = loss_history_plt(loss_history)
53
+ save_plt(animated_loss_plt, "plt.svg", animated=True, fps=30)
 
opts.py CHANGED
@@ -2,6 +2,7 @@ from neural_network.main import main as nn
2
  from cluster.main import main as clust
3
 
4
  options = {
5
- "nn": nn,
6
- "cluster": clust,
 
7
  }
 
2
  from cluster.main import main as clust
3
 
4
  options = {
5
+ "neural-network": nn,
6
+ "kmeans-clustering": clust,
7
+ "kmedoid-clustering": clust,
8
  }
run.sh ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ #!/bin/bash
2
+ npx tailwindcss -i ./static/src/input.css -o ./static/dist/css/output.css
3
+ gunicorn --bind :3000 --workers 1 --threads 8 --timeout 0 app:app
run_dev.sh DELETED
@@ -1,30 +0,0 @@
1
- #!/bin/bash
2
-
3
- # Function to handle SIGINT signal
4
- function cleanup {
5
- echo "Cleaning up and exiting..."
6
- kill $TAILWIND_PID
7
- kill $FLASK_PID
8
- exit 0
9
- }
10
-
11
- # Register cleanup function to be called when SIGINT is received
12
- trap cleanup SIGINT
13
-
14
- # Start tailwindcss watch process in the background
15
- npx tailwindcss -i ./static/src/input.css -o ./static/dist/css/output.css --watch &
16
- TAILWIND_PID=$!
17
-
18
- # Wait for the CSS files to be generated before starting the Flask server
19
- sleep 2
20
-
21
- # Start the Flask server in the background
22
- python app.py &
23
- FLASK_PID=$!
24
-
25
- # Wait for both processes to finish
26
- wait $TAILWIND_PID $FLASK_PID
27
-
28
- # When the wait command completes, kill both processes
29
- kill $TAILWIND_PID
30
- kill $FLASK_PID
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
templates/index.html CHANGED
@@ -1,18 +1,34 @@
1
  <!DOCTYPE html>
2
  <html lang="en">
3
- <head>
4
  <meta charset="UTF-8">
5
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
6
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
7
- <title>Flowbite Flask</title>
8
- <link rel="stylesheet" href="{{url_for('static',filename='dist/css/output.css')}}">
9
- </head>
10
- <body>
11
- <div class="flex items-center h-screen">
12
- <div class="mx-auto">
13
- <h1 class="text-3xl font-bold">Centered Text</h1>
14
- </div>
15
  </div>
16
- </body>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  </html>
18
-
 
1
  <!DOCTYPE html>
2
  <html lang="en">
3
+ <head>
4
  <meta charset="UTF-8">
5
+ <title>Data Mining From Scratch</title>
6
+ <link rel="stylesheet" href="{{ url_for('static', filename='dist/css/output.css') }}">
7
+ <!-- Add Tailwind CSS -->
8
+ <link rel="stylesheet" href="https://cdn.tailwindcss.com/css/tailwind.min.css">
9
+ </head>
10
+ <body class="bg-gray-100">
11
+ <div class="container mx-auto flex flex-col items-center justify-center h-screen">
12
+ <div class="text-center mb-8">
13
+ <h1 class="text-4xl font-bold">Data Mining From Scratch</h1>
14
+ <p class="text-gray-500">By: Jensen Holm</p>
15
  </div>
16
+ <div class="w-full md:w-1/3">
17
+ <form method="POST" action="{{ url_for('process_algorithm') }}">
18
+ <label class="block font-medium text-gray-700 mb-2" for="model-select">
19
+ Select Algorithm
20
+ </label>
21
+ <select id="model-select" name="model-select"
22
+ class="form-select block w-full mt-1 rounded-md shadow-sm border-gray-300 focus:border-indigo-300 focus:ring focus:ring-indigo-200 focus:ring-opacity-50">
23
+ <option value="neural-network">Neural Network</option>
24
+ <option value="kmeans-clustering">Kmeans Clustering</option>
25
+ <option value="kmedoids-clustering">Kmedoids Clustering</option>
26
+ </select>
27
+ <button type="submit" class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded mt-4">
28
+ Go
29
+ </button>
30
+ </form>
31
+ </div>
32
+ </div>
33
+ </body>
34
  </html>