-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp_table.py
More file actions
103 lines (79 loc) · 2.42 KB
/
app_table.py
File metadata and controls
103 lines (79 loc) · 2.42 KB
1
2
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import dash
import dash_table
import pandas as pd
from dash.dependencies import Input, Output, State
import dash_core_components as dcc
import dash_html_components as html
import dash_bootstrap_components as dbc
import os
# USAGE
# python simple_request.py
# import the necessary packages
import requests
# initialize the Keras REST API endpoint URL along with the input
# image path
KERAS_REST_API_URL = "http://localhost:5000/predict"
script_dir = os.path.dirname(__file__) #<-- absolute dir the script is in
app = dash.Dash( external_stylesheets=[dbc.themes.BOOTSTRAP])
app.css.config.serve_locally = True
app.scripts.config.serve_locally = True
app.layout = html.Div([
dcc.Upload(
id='upload-data',
children=html.Div([
html.A('Run Inference')
]),
style={
'width': '50%',
'height': '60px',
'lineHeight': '60px',
'borderWidth': '1px',
'borderStyle': 'dashed',
'borderRadius': '5px',
'textAlign': 'center'
},
multiple=False
),
html.Div(id='output-data-upload')
])
@app.callback(dash.dependencies.Output('output-data-upload', 'children'),
[dash.dependencies.Input('upload-data', 'contents'),
dash.dependencies.Input('upload-data', 'filename')])
def update_output(contents, filename):
if contents is not None:
script_dir = os.path.dirname(__file__) #<-- absolute dir the script is in
rel_path = "/images/" + filename
IMAGE_PATH = script_dir + rel_path
# load the input image and construct the payload for the request
image = open(IMAGE_PATH, "rb").read()
payload = {"image": image}
# submit the request
r = requests.post(KERAS_REST_API_URL, files=payload).json()
# ensure the request was sucessful
if r["success"]:
# loop over the predictions and display them
df = pd.json_normalize(r["predictions"])
df["probability"] = 100 * df["probability"]
df = df.round({'probability': 2})
df = df.rename(str.upper, axis='columns')
return html.Div([
html.Hr(),
html.Img(src=contents),
html.Hr(),
dash_table.DataTable(
id='table',
columns=[{"name": i, "id": i} for i in df.columns],
data=df.to_dict("rows"),
style_cell={'width': '25px',
'fontSize':20,
'font-family':'sans-serif',
'height': '50px',
'color' : 'black',
'textAlign': 'center'}
),
],style={'columnCount': 1})
# otherwise, the request failed
else:
print("Request failed")
if __name__ == "__main__":
app.run_server(debug=True,threaded=True)