-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
243 lines (166 loc) · 8.22 KB
/
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import streamlit as st
from streamlit_custom_chat import ChatContainer
from streamlit_custom_input import ChatInput
from key_generator.key_generator import generate
from helper_functions import set_bg_hack, set_page_container_style, refresh
import json
from backend_functions import get_evaluation, get_random_question, get_technichal_question, get_technichal_evaluation, messageFromChatBot, config
from left_column import left_column
def main_app(questionType="Behavioural"):
set_bg_hack("assets/images/pastel3.jpg")
set_page_container_style()
# Initialize session variables
if "messages" not in st.session_state:
# st.set_page_config(layout="wide")
st.session_state.messages = []
if "rateAnswer" not in st.session_state:
st.session_state.rateAnswer = False
if "getQuestion" not in st.session_state:
st.session_state.getQuestion = False
if "userAnswer" not in st.session_state:
st.session_state.userAnswer = ""
if "responding" not in st.session_state:
st.session_state.responding = False
if "skip" not in st.session_state:
st.session_state.skip = ""
if "questionType" not in st.session_state:
st.session_state.questionType = questionType
titlecol1, titlecol2, titlecol3 = st.columns([8, 5, 3])
with titlecol1:
st.markdown('#')
with titlecol2:
st.title('Mentor Bot')
with titlecol3:
# st.markdown(st.session_state.category)
st.markdown('#')
if st.session_state.questionType == 'Technical':
# get a question to ask the user
new_question = get_technichal_question(st.session_state.category)
# if there are no messages in the session add
if len(st.session_state.messages) == 0:
st.session_state.messages.append(
{"role": "assistant", "content": config['technical_message'], "key": 0})
st.session_state.messages.append(
{"role": "assistant", "content": new_question, "key": 1})
col1, col2 = st.columns([2, 13])
with col1:
left_column()
with col2:
# component that displays the messages
ChatContainer(messages=st.session_state.messages,
key="chatcontainer")
key = generate()
# check if the agent is still streaming
if st.session_state.responding:
# call function that recieves the agent stream
messageFromChatBot()
# after the streaming is done reset the responding falg
st.session_state.responding = False
# change the flag to true to make the agent ask a new question
st.session_state.getQuestion = True
# reset the number of tokens to skip
st.session_state.skip = ""
# check if the user submitted an answer to review
elif st.session_state.rateAnswer and st.session_state.userAnswer is not None:
# prepare the prompt for the agent
content = '{question: '+st.session_state.messages[-1]["content"] + \
', answer: '+st.session_state.userAnswer+'}'
# call the function that sends the prompt to the agent
get_technichal_evaluation(content)
# reset user answer
st.session_state.userAnswer = None
# reset rate flag
st.session_state.rateAnswer = False
# make the question flag true to fetch a new question
st.session_state.getQuestion = True
refresh('chatcontainer')
# check if a new question should be displayed
if st.session_state.getQuestion:
# call function to get a new question
new_question = get_technichal_question(
st.session_state.category)
# add the new question to the session state
st.session_state.messages.append(
{"role": "assistant", "content": new_question, "key": "assistant-"+key.get_key()})
# reset question flag
st.session_state.getQuestion = False
st.session_state.userAnswer = None
refresh('chatcontainer')
# recieve input from the user
if answer := ChatInput(initialValue="", key="inputButton"):
# add user message to chat history
st.session_state.messages.append(
{"role": "user", "content": answer, "key": "user-"+key.get_key()})
st.session_state.userAnswer = answer
# make the rate flag true to send the question to the agent
st.session_state.rateAnswer = True
# reset question and responding flags
st.session_state.getQuestion = False
st.session_state.responding = False
answer=None
refresh('inputButton')
else:
new_question = get_random_question()
# if there are no messages in the session add
if len(st.session_state.messages) == 0:
st.session_state.messages.append(
{"role": "assistant", "content": config['openning_message'], "key": 0})
st.session_state.messages.append(
{"role": "assistant", "content": new_question, "key": 1})
col1, col2 = st.columns([2, 13])
with col1:
left_column()
with col2:
# component that displays the messages
ChatContainer(messages=st.session_state.messages,
key="chatcontainer")
key = generate()
# check if the agent is still streaming
if st.session_state.responding:
# call function that recieves the agent stream
messageFromChatBot()
# after the streaming is done reset the responding falg
st.session_state.responding = False
# change the flag to true to make the agent ask a new question
st.session_state.getQuestion = True
# reset the number of tokens to skip
st.session_state.skip = ""
# check if the user submitted an answer to review
elif st.session_state.rateAnswer:
# prepare the prompt for the agent
content = '{question: '+new_question + \
', answer: '+st.session_state.userAnswer+'}'
# call the function that sends the prompt to the agent
get_evaluation(content)
# reset user answer
st.session_state.userAnswer = None
# reset rate flag
st.session_state.rateAnswer = False
# make the question flag true to fetch a new question
st.session_state.getQuestion = True
refresh('chatcontainer')
# check if a new question should be displayed
if st.session_state.getQuestion:
# call function to get a new question
new_question = get_random_question()
# add the new question to the session state
st.session_state.messages.append(
{"role": "assistant", "content": new_question, "key": "assistant-"+key.get_key()})
# reset question flag
st.session_state.getQuestion = False
st.session_state.userAnswer = None
refresh('chatcontainer')
# recieve input from the user
if answer := ChatInput(initialValue="", key="inputButton"):
# add user message to chat history
st.session_state.messages.append(
{"role": "user", "content": answer, "key": "user-"+key.get_key()})
st.session_state.userAnswer = answer
# make the rate flag true to send the question to the agent
st.session_state.rateAnswer = True
# reset question and responding flags
st.session_state.getQuestion = False
st.session_state.responding = False
answer=None
refresh('inputButton')
main_app()