Spaces:
Sleeping
Sleeping
Commit
·
28d5b3d
1
Parent(s):
33e7a34
instead of returning the image bytes to the api user, we are moving towards hosting them on the backend but we need to be careful moving forwards and make sure that we delete the images after use
Browse files- app.py +1 -4
- cluster/clusterer.py +1 -1
- cluster/kmeans.py +1 -1
- cluster/plot.py +13 -9
- example/neural_network.py +1 -2
- neural_network/neural_network.py +2 -2
- neural_network/plot.py +12 -6
- plt_id.py +5 -0
app.py
CHANGED
@@ -12,10 +12,7 @@ app = Flask(
|
|
12 |
template_folder="templates",
|
13 |
)
|
14 |
|
15 |
-
CORS(
|
16 |
-
app,
|
17 |
-
origins="*",
|
18 |
-
)
|
19 |
|
20 |
|
21 |
@app.route("/", methods=["GET"])
|
|
|
12 |
template_folder="templates",
|
13 |
)
|
14 |
|
15 |
+
CORS(app, origins="*")
|
|
|
|
|
|
|
16 |
|
17 |
|
18 |
@app.route("/", methods=["GET"])
|
cluster/clusterer.py
CHANGED
@@ -5,7 +5,7 @@ from typing import Callable
|
|
5 |
@dataclass
|
6 |
class Clusterer:
|
7 |
cluster_func: Callable
|
8 |
-
|
9 |
|
10 |
def eval(
|
11 |
self,
|
|
|
5 |
@dataclass
|
6 |
class Clusterer:
|
7 |
cluster_func: Callable
|
8 |
+
plot_key = None
|
9 |
|
10 |
def eval(
|
11 |
self,
|
cluster/kmeans.py
CHANGED
@@ -74,5 +74,5 @@ class Kmeans(Clusterer):
|
|
74 |
"k": self.k,
|
75 |
"max_iter": self.max_iter,
|
76 |
"clusters": cluster_data,
|
77 |
-
"
|
78 |
}
|
|
|
74 |
"k": self.k,
|
75 |
"max_iter": self.max_iter,
|
76 |
"clusters": cluster_data,
|
77 |
+
"plot_key": self.plot_key,
|
78 |
}
|
cluster/plot.py
CHANGED
@@ -1,14 +1,17 @@
|
|
1 |
-
import io
|
2 |
-
import base64
|
3 |
-
import numpy as np
|
4 |
import matplotlib
|
5 |
-
import matplotlib.pyplot as plt
|
6 |
import seaborn as sns
|
|
|
|
|
7 |
|
8 |
|
9 |
matplotlib.use("Agg")
|
10 |
sns.set()
|
11 |
|
|
|
|
|
|
|
|
|
12 |
def plot(clusterer, X) -> None:
|
13 |
cluster_data = clusterer.to_dict(X)["clusters"]
|
14 |
# plot the clusters and data points
|
@@ -32,11 +35,12 @@ def plot(clusterer, X) -> None:
|
|
32 |
ax.set_title("K-means Clustering")
|
33 |
ax.set_ylabel("Normalized Petal Length (cm)")
|
34 |
ax.set_xlabel("Normalized Petal Length (cm)")
|
35 |
-
clusterer.plot = plt_bytes(fig)
|
36 |
|
|
|
37 |
|
38 |
-
|
39 |
-
|
40 |
-
fig.savefig(
|
41 |
plt.close(fig)
|
42 |
-
|
|
|
|
|
|
|
|
|
|
1 |
import matplotlib
|
2 |
+
import matplotlib.pyplot as plt
|
3 |
import seaborn as sns
|
4 |
+
from plt_id import generate_image_key
|
5 |
+
import os
|
6 |
|
7 |
|
8 |
matplotlib.use("Agg")
|
9 |
sns.set()
|
10 |
|
11 |
+
# Replace with the desired upload folder path
|
12 |
+
UPLOAD_FOLDER = '/path/to/upload/folder'
|
13 |
+
|
14 |
+
|
15 |
def plot(clusterer, X) -> None:
|
16 |
cluster_data = clusterer.to_dict(X)["clusters"]
|
17 |
# plot the clusters and data points
|
|
|
35 |
ax.set_title("K-means Clustering")
|
36 |
ax.set_ylabel("Normalized Petal Length (cm)")
|
37 |
ax.set_xlabel("Normalized Petal Length (cm)")
|
|
|
38 |
|
39 |
+
image_key = generate_image_key() # Generate a unique key for the image
|
40 |
|
41 |
+
# Save the plot as an image file with the key in the filename
|
42 |
+
plot_filename = os.path.join(UPLOAD_FOLDER, f"{image_key}.png")
|
43 |
+
fig.savefig(plot_filename, format="png")
|
44 |
plt.close(fig)
|
45 |
+
|
46 |
+
clusterer.plot_key = image_key
|
example/neural_network.py
CHANGED
@@ -4,10 +4,9 @@ import requests
|
|
4 |
import json
|
5 |
|
6 |
|
7 |
-
ENDPOINT: str = "http://127.0.0.1:5000/"
|
8 |
|
9 |
request_params = {
|
10 |
-
"algorithm": "neural-network",
|
11 |
"arguments": {
|
12 |
"epochs": 100,
|
13 |
"activation_func": "tanh",
|
|
|
4 |
import json
|
5 |
|
6 |
|
7 |
+
ENDPOINT: str = "http://127.0.0.1:5000/neural-network"
|
8 |
|
9 |
request_params = {
|
|
|
10 |
"arguments": {
|
11 |
"epochs": 100,
|
12 |
"activation_func": "tanh",
|
neural_network/neural_network.py
CHANGED
@@ -19,7 +19,7 @@ class NeuralNetwork:
|
|
19 |
loss_history: list = field(
|
20 |
default_factory=lambda: [],
|
21 |
)
|
22 |
-
|
23 |
|
24 |
def predict(self, x: np.array) -> np.array:
|
25 |
n1 = self.compute_node(x, self.w1, self.b1, self.activation_func)
|
@@ -54,5 +54,5 @@ class NeuralNetwork:
|
|
54 |
# not returning this because we are making our own
|
55 |
# plots and this can be a lot of data
|
56 |
# "loss_history": self.loss_history,
|
57 |
-
"
|
58 |
}
|
|
|
19 |
loss_history: list = field(
|
20 |
default_factory=lambda: [],
|
21 |
)
|
22 |
+
plot_key = None
|
23 |
|
24 |
def predict(self, x: np.array) -> np.array:
|
25 |
n1 = self.compute_node(x, self.w1, self.b1, self.activation_func)
|
|
|
54 |
# not returning this because we are making our own
|
55 |
# plots and this can be a lot of data
|
56 |
# "loss_history": self.loss_history,
|
57 |
+
"plot_id": self.plot_id,
|
58 |
}
|
neural_network/plot.py
CHANGED
@@ -1,12 +1,15 @@
|
|
1 |
import numpy as np
|
2 |
-
import base64
|
3 |
-
import io
|
4 |
import seaborn as sns
|
5 |
import matplotlib
|
6 |
import matplotlib.pyplot as plt
|
|
|
|
|
7 |
|
8 |
matplotlib.use("Agg")
|
9 |
|
|
|
|
|
|
|
10 |
def plot(model) -> None:
|
11 |
sns.set()
|
12 |
fig, ax = plt.subplots()
|
@@ -18,8 +21,11 @@ def plot(model) -> None:
|
|
18 |
plt.ylabel("Loss")
|
19 |
plt.xlabel("Epoch")
|
20 |
plt.title("Loss / Epoch")
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
23 |
plt.close(fig)
|
24 |
-
|
25 |
-
model.
|
|
|
1 |
import numpy as np
|
|
|
|
|
2 |
import seaborn as sns
|
3 |
import matplotlib
|
4 |
import matplotlib.pyplot as plt
|
5 |
+
from plt_id import generate_image_key
|
6 |
+
import os
|
7 |
|
8 |
matplotlib.use("Agg")
|
9 |
|
10 |
+
UPLOAD_FOLDER = "/plots"
|
11 |
+
|
12 |
+
|
13 |
def plot(model) -> None:
|
14 |
sns.set()
|
15 |
fig, ax = plt.subplots()
|
|
|
21 |
plt.ylabel("Loss")
|
22 |
plt.xlabel("Epoch")
|
23 |
plt.title("Loss / Epoch")
|
24 |
+
|
25 |
+
image_key = generate_image_key()
|
26 |
+
|
27 |
+
plot_filename = os.path.join(UPLOAD_FOLDER, f"{image_key}.png")
|
28 |
+
fig.savefig(plot_filename, format="png")
|
29 |
plt.close(fig)
|
30 |
+
|
31 |
+
model.plot_key = image_key
|
plt_id.py
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import uuid
|
2 |
+
|
3 |
+
|
4 |
+
def generate_image_key() -> str:
|
5 |
+
return str(uuid.uuid4())
|