Spaces:
Runtime error
Runtime error
Commit
•
827bcf6
1
Parent(s):
337925d
Upload 3 files
Browse files- app.py +52 -151
- model.h5 +3 -0
- model.json +0 -0
app.py
CHANGED
@@ -1,151 +1,52 @@
|
|
1 |
-
from
|
2 |
-
from
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
)
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
)
|
53 |
-
|
54 |
-
|
55 |
-
def server(input: Inputs, output: Outputs, session: Session):
|
56 |
-
@reactive.Calc
|
57 |
-
def filtered_df() -> pd.DataFrame:
|
58 |
-
"""Returns a Pandas data frame that includes only the desired rows"""
|
59 |
-
|
60 |
-
# This calculation "req"uires that at least one species is selected
|
61 |
-
req(len(input.species()) > 0)
|
62 |
-
|
63 |
-
# Filter the rows so we only include the desired species
|
64 |
-
return df[df["Species"].isin(input.species())]
|
65 |
-
|
66 |
-
@output
|
67 |
-
@render.plot
|
68 |
-
def scatter():
|
69 |
-
"""Generates a plot for Shiny to display to the user"""
|
70 |
-
|
71 |
-
# The plotting function to use depends on whether margins are desired
|
72 |
-
plotfunc = sns.jointplot if input.show_margins() else sns.scatterplot
|
73 |
-
|
74 |
-
plotfunc(
|
75 |
-
data=filtered_df(),
|
76 |
-
x=input.xvar(),
|
77 |
-
y=input.yvar(),
|
78 |
-
palette=palette,
|
79 |
-
hue="Species" if input.by_species() else None,
|
80 |
-
hue_order=species,
|
81 |
-
legend=False,
|
82 |
-
)
|
83 |
-
|
84 |
-
@output
|
85 |
-
@render.ui
|
86 |
-
def value_boxes():
|
87 |
-
df = filtered_df()
|
88 |
-
|
89 |
-
def penguin_value_box(title: str, count: int, bgcol: str, showcase_img: str):
|
90 |
-
return ui.value_box(
|
91 |
-
title,
|
92 |
-
count,
|
93 |
-
{"class_": "pt-1 pb-0"},
|
94 |
-
showcase=ui.fill.as_fill_item(
|
95 |
-
ui.tags.img(
|
96 |
-
{"style": "object-fit:contain;"},
|
97 |
-
src=showcase_img,
|
98 |
-
)
|
99 |
-
),
|
100 |
-
theme_color=None,
|
101 |
-
style=f"background-color: {bgcol};",
|
102 |
-
)
|
103 |
-
|
104 |
-
if not input.by_species():
|
105 |
-
return penguin_value_box(
|
106 |
-
"Penguins",
|
107 |
-
len(df.index),
|
108 |
-
bg_palette["default"],
|
109 |
-
# Artwork by @allison_horst
|
110 |
-
showcase_img="penguins.png",
|
111 |
-
)
|
112 |
-
|
113 |
-
value_boxes = [
|
114 |
-
penguin_value_box(
|
115 |
-
name,
|
116 |
-
len(df[df["Species"] == name]),
|
117 |
-
bg_palette[name],
|
118 |
-
# Artwork by @allison_horst
|
119 |
-
showcase_img=f"{name}.png",
|
120 |
-
)
|
121 |
-
for name in species
|
122 |
-
# Only include boxes for _selected_ species
|
123 |
-
if name in input.species()
|
124 |
-
]
|
125 |
-
|
126 |
-
return ui.layout_column_wrap(*value_boxes, width = 1 / len(value_boxes))
|
127 |
-
|
128 |
-
|
129 |
-
# "darkorange", "purple", "cyan4"
|
130 |
-
colors = [[255, 140, 0], [160, 32, 240], [0, 139, 139]]
|
131 |
-
colors = [(r / 255.0, g / 255.0, b / 255.0) for r, g, b in colors]
|
132 |
-
|
133 |
-
palette: Dict[str, Tuple[float, float, float]] = {
|
134 |
-
"Adelie": colors[0],
|
135 |
-
"Chinstrap": colors[1],
|
136 |
-
"Gentoo": colors[2],
|
137 |
-
"default": sns.color_palette()[0], # type: ignore
|
138 |
-
}
|
139 |
-
|
140 |
-
bg_palette = {}
|
141 |
-
# Use `sns.set_style("whitegrid")` to help find approx alpha value
|
142 |
-
for name, col in palette.items():
|
143 |
-
# Adjusted n_colors until `axe` accessibility did not complain about color contrast
|
144 |
-
bg_palette[name] = mpl_colors.to_hex(sns.light_palette(col, n_colors=7)[1]) # type: ignore
|
145 |
-
|
146 |
-
|
147 |
-
app = App(
|
148 |
-
app_ui,
|
149 |
-
server,
|
150 |
-
static_assets=str(www_dir),
|
151 |
-
)
|
|
|
1 |
+
from flask import Flask, request, jsonify, render_template
|
2 |
+
from tensorflow.keras.models import model_from_json
|
3 |
+
from PIL import Image
|
4 |
+
import numpy as np
|
5 |
+
|
6 |
+
app = Flask(__name__)
|
7 |
+
|
8 |
+
# Load model architecture from JSON file
|
9 |
+
with open("model.json", "r") as json_file:
|
10 |
+
loaded_model_json = json_file.read()
|
11 |
+
|
12 |
+
Model = model_from_json(loaded_model_json)
|
13 |
+
|
14 |
+
Model.load_weights("model.h5")
|
15 |
+
|
16 |
+
print("Loaded model from disk")
|
17 |
+
|
18 |
+
|
19 |
+
# predict
|
20 |
+
def preprocess_image(image):
|
21 |
+
img = Image.open(image)
|
22 |
+
img = img.resize((224, 224))
|
23 |
+
img_array = np.expand_dims(img, axis=0)
|
24 |
+
return img_array
|
25 |
+
|
26 |
+
|
27 |
+
@app.route('/')
|
28 |
+
def index():
|
29 |
+
return render_template('index.html')
|
30 |
+
|
31 |
+
|
32 |
+
@app.route('/predict', methods=['POST'])
|
33 |
+
def predict():
|
34 |
+
if 'image' not in request.files:
|
35 |
+
return jsonify({'error': 'No file part'})
|
36 |
+
|
37 |
+
file = request.files['image']
|
38 |
+
|
39 |
+
if file.filename == '':
|
40 |
+
return jsonify({'error': 'No selected file'})
|
41 |
+
|
42 |
+
if file:
|
43 |
+
img = preprocess_image(file)
|
44 |
+
predictions = Model.predict(img)
|
45 |
+
predicted_class_index = int(np.argmax(predictions, axis=1)[0]) # Convert to int
|
46 |
+
class_labels = ['pituitary', 'notumor', 'meningioma', 'glioma']
|
47 |
+
predicted_class_label = class_labels[predicted_class_index]
|
48 |
+
return jsonify({'class': predicted_class_label})
|
49 |
+
|
50 |
+
|
51 |
+
if __name__ == '__main__':
|
52 |
+
app.run(debug=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
model.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:729c25c413cfa8db4ea48157a127c321040d702e40266bac2b3a79662eb62d1d
|
3 |
+
size 253655064
|
model.json
ADDED
The diff for this file is too large to render.
See raw diff
|
|