-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_app.py
211 lines (186 loc) · 6.85 KB
/
main_app.py
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
"""
Streamlit App for Machine Learning Deployment (MBTI Inference)
"""
import os
import json
import pickle
import numpy as np
import streamlit as st
RELATIVE_PATH = os.getenv("DOCKER_OPERATE", "mbti_ipip/streamlit/")
st.set_page_config(layout="wide")
st.title("MBTI Machine Learning Inference 🚀")
st.caption("powered by streamlit, scikit-learn, Azure App Service, and patcharanat🌚")
st.header("What is MBTI 🤔")
st.markdown("""
The MBTI® assessment is designed to help people identify and gain some understanding around how they take in information and make decisions, the patterns of perception and judgment, as seen in normal, healthy behavior.
""")
st.subheader("Take 12 questions to get prediction of your MBTI 🥳")
st.caption("from the full test having approxiamately 94 questions 😱")
st.caption(
"Model: Logistic Regression - [Accuracy = 43.77 % / F1-Score = 40.01 % / Baseline (Random Guess) = 3.125 %]"
)
map_input_dict = {":blue[1]": 1, ":green[2]": 2, "3": 3, ":orange[4]": 4, ":red[5]": 5}
# read input list
with open(f"{RELATIVE_PATH}models/input_format.json", encoding="utf8") as json_file:
input_format = json.load(json_file)
qa_list = [qa_code for qa_code in input_format.keys()]
with st.form("ml_input"):
qa_1 = st.radio(
label=f"1.) {input_format[qa_list[0]]}",
options=[":blue[1]", ":green[2]", "3", ":orange[4]", ":red[5]"],
horizontal=True,
index=None,
)
qa_2 = st.radio(
label=f"2.) {input_format[qa_list[1]]}",
options=[":blue[1]", ":green[2]", "3", ":orange[4]", ":red[5]"],
horizontal=True,
index=None,
)
qa_3 = st.radio(
label=f"3.) {input_format[qa_list[2]]}",
options=[":blue[1]", ":green[2]", "3", ":orange[4]", ":red[5]"],
horizontal=True,
index=None,
)
qa_4 = st.radio(
label=f"4.) {input_format[qa_list[3]]}",
options=[":blue[1]", ":green[2]", "3", ":orange[4]", ":red[5]"],
horizontal=True,
index=None,
)
qa_5 = st.radio(
label=f"5.) {input_format[qa_list[4]]}",
options=[":blue[1]", ":green[2]", "3", ":orange[4]", ":red[5]"],
horizontal=True,
index=None,
)
qa_6 = st.radio(
label=f"6.) {input_format[qa_list[5]]}",
options=[":blue[1]", ":green[2]", "3", ":orange[4]", ":red[5]"],
horizontal=True,
index=None,
)
qa_7 = st.radio(
label=f"7.) {input_format[qa_list[6]]}",
options=[":blue[1]", ":green[2]", "3", ":orange[4]", ":red[5]"],
horizontal=True,
index=None,
)
qa_8 = st.radio(
label=f"8.) {input_format[qa_list[7]]}",
options=[":blue[1]", ":green[2]", "3", ":orange[4]", ":red[5]"],
horizontal=True,
index=None,
)
qa_9 = st.radio(
label=f"9.) {input_format[qa_list[8]]}",
options=[":blue[1]", ":green[2]", "3", ":orange[4]", ":red[5]"],
horizontal=True,
index=None,
)
qa_10 = st.radio(
label=f"10.) {input_format[qa_list[9]]}",
options=[":blue[1]", ":green[2]", "3", ":orange[4]", ":red[5]"],
horizontal=True,
index=None,
)
qa_11 = st.radio(
label=f"11.) {input_format[qa_list[11]]}",
options=[":blue[1]", ":green[2]", "3", ":orange[4]", ":red[5]"],
horizontal=True,
index=None,
)
qa_12 = st.radio(
label=f"12.) {input_format[qa_list[10]]}",
options=[":blue[1]", ":green[2]", "3", ":orange[4]", ":red[5]"],
horizontal=True,
index=None,
)
submitted = st.form_submit_button("Submit")
if submitted:
raw_input = [
qa_1,
qa_2,
qa_3,
qa_4,
qa_5,
qa_6,
qa_7,
qa_8,
qa_9,
qa_10,
qa_11,
qa_12,
]
if None not in raw_input:
input_data = [map_input_dict[choice] for choice in raw_input]
with st.spinner("wait for it..."):
# TODO: change this to dynamic from inference
# pre-processing
input_data = np.array(input_data).reshape(1, -1)
# load model and predict
# time.sleep(5)
model = pickle.load(
open(f"{RELATIVE_PATH}models/logistic_regression_mbti.bin", "rb")
)
decoder = pickle.load(open(f"{RELATIVE_PATH}models/encoder.pkl", "rb"))
raw_personality = model.predict(input_data)
personality = str(decoder.inverse_transform(raw_personality))[2:-2]
# personality = "ISFJ-T"
st.balloons()
st.success(f"Done! your MBTI is '{personality}' 🚀", icon="✅")
# extract data
with open(
f"{RELATIVE_PATH}mbti_info/letters_info.json", encoding="utf8"
) as json_file:
letters_info = json.load(json_file)
with open(
f"{RELATIVE_PATH}mbti_info/mbti_info.json", encoding="utf8"
) as json_file:
mbti_info = json.load(json_file)
letter_1 = personality[0]
letter_2 = personality[1]
letter_3 = personality[2]
letter_4 = personality[3]
letter_5 = personality[4:]
personality_short = personality[:-2]
# fill in details about personality
st.image(
f"{RELATIVE_PATH}picture/{personality_short.lower()}.png",
caption="MBTI Character",
)
st.write(f"""
- **Group**: {mbti_info.get(personality_short).get("group")}
- **Role**: {mbti_info.get(personality_short).get("role")}
- **Summary**:
- {mbti_info.get(personality_short).get("description")}
""")
st.link_button(
":blue[More about your MBTI]",
mbti_info.get(personality_short).get("link"),
)
st.subheader(f"Personality Detail: {personality} 🌟")
st.markdown(f"""
- 📍 {letters_info.get(letter_1)}
- {letters_info.get("IE")}
- 📍 {letters_info.get(letter_2)}
- 📍 {letters_info.get(letter_3)}
- {letters_info.get("TF")}
- 📍 {letters_info.get(letter_4)}
- 📍 {letters_info.get(letter_5)}
""")
else:
st.error("Questions are not fully answered.")
# appendix
columns = st.columns(2)
with columns[0]:
st.link_button(
":green[Take the Full Test from Official Site. 📊]",
"https://www.16personalities.com/free-personality-test",
)
with columns[1]:
st.link_button(
"Check Out for other Personalities 🌎",
"https://www.16personalities.com/personality-types",
)