Jason commited on
Commit
02d8c91
·
1 Parent(s): 6037dff

first commit

Browse files
Files changed (6) hide show
  1. .gitignore +1 -0
  2. app.py +10 -0
  3. config.ini +8 -0
  4. features/captcha.py +52 -0
  5. features/predict.py +49 -0
  6. requirements.txt +3 -0
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ __pycache__
app.py ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from features import predict, captcha
3
+
4
+ navigation = st.sidebar.selectbox(
5
+ label='Pilih Halaman', options=('Predict', 'Captcha'))
6
+
7
+ if navigation == 'Predict':
8
+ predict.run()
9
+ elif navigation == 'Captcha':
10
+ captcha.run()
config.ini ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ [DEFAULT]
2
+ prod = False
3
+
4
+ [DEVELOPMENT]
5
+ URL = http://127.0.0.1:5000
6
+
7
+ [PRODUCTION]
8
+ URL = https://shaky-rosie-kidfrom.koyeb.app
features/captcha.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import requests
3
+ from configparser import ConfigParser
4
+ from PIL import Image
5
+ from io import BytesIO
6
+ from base64 import decodebytes
7
+ import matplotlib.pyplot as plt
8
+
9
+ config = ConfigParser()
10
+ config.read("./config.ini")
11
+
12
+ if config['DEFAULT']['prod'] == 'True':
13
+ URL = config['PRODUCTION']['URL']
14
+ else:
15
+ URL = config['DEVELOPMENT']['URL']
16
+
17
+ def req(filename):
18
+ r = requests.post(f'{URL}/captcha', json={'target': filename})
19
+
20
+ if r.status_code == 200:
21
+ res = r.json()
22
+
23
+ st.write(f'Prediction: {res["result"]}')
24
+
25
+ def run():
26
+ st.title("Captcha")
27
+
28
+ r = requests.get(f'{URL}/captcha')
29
+
30
+ if r.status_code == 200:
31
+ res = r.json()
32
+
33
+ st.write(f'Select {res["target"]}')
34
+
35
+ fig, axes = plt.subplots(6,4, layout="constrained", figsize=(15,15))
36
+
37
+ for option, ax, index in zip(res['options'], axes.ravel(), range(23)):
38
+ filename = option[0]
39
+ image_base64 = option[1]
40
+ image = Image.open(BytesIO(decodebytes(bytes(image_base64, "ascii"))))
41
+ ax.imshow(image)
42
+ ax.set_title(index + 1)
43
+
44
+ for ax in axes.ravel():
45
+ ax.get_xaxis().set_visible(False)
46
+ ax.get_yaxis().set_visible(False)
47
+
48
+ st.pyplot(fig)
49
+
50
+ for index, option in enumerate(res['options']):
51
+ filename = option[0]
52
+ st.button(label=f'Click image {index + 1}', key=index+1, on_click=req, kwargs={"filename": filename})
features/predict.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import requests
3
+ from PIL import Image
4
+ from configparser import ConfigParser
5
+ from io import BytesIO
6
+ import matplotlib.pyplot as plt
7
+
8
+ config = ConfigParser()
9
+ config.read("./config.ini")
10
+
11
+ if config['DEFAULT']['prod'] == 'True':
12
+ URL = config['PRODUCTION']['URL']
13
+ else:
14
+ URL = config['DEVELOPMENT']['URL']
15
+
16
+ def run():
17
+ st.title("Image Predictor")
18
+
19
+ with st.form(key="image_predictor"):
20
+ uploaded_file = st.file_uploader(
21
+ label="Image", accept_multiple_files=False)
22
+
23
+ submitted = st.form_submit_button(label="Predict")
24
+
25
+ if submitted:
26
+ if uploaded_file is not None:
27
+ # bytes_data = uploaded_file.getvalue()
28
+
29
+ files = {'file': uploaded_file}
30
+ post = requests.post(f'{URL}/predict', files=files)
31
+
32
+ if post.status_code == 200:
33
+ res = post.json()
34
+
35
+ st.write(f'Prediction {res["prediction"]}')
36
+
37
+ get = requests.get(f'{URL}/{res["similar"]}')
38
+
39
+ if get.status_code == 200:
40
+ fig, axes = plt.subplots(1,2, layout="constrained")
41
+ for title, image, ax in zip(
42
+ ["Uploaded", "Similar"],
43
+ [Image.open(BytesIO(uploaded_file.getvalue())),
44
+ Image.open(BytesIO(get.content))],
45
+ axes.ravel()):
46
+ ax.imshow(image)
47
+ ax.set_title(title)
48
+
49
+ st.pyplot(fig)
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ streamlit
2
+ matplotlib
3
+ pillow