Spaces:
Runtime error
Runtime error
session works!
Browse files- app.py +41 -34
- modules/db_modules.py +1 -1
app.py
CHANGED
@@ -19,6 +19,7 @@ debate_bot_log_table = dynamodb.Table('debate_bot_log')
|
|
19 |
# Streamlit setting
|
20 |
#############################################
|
21 |
st.header("DEBATE BOT")
|
|
|
22 |
|
23 |
if 'generated' not in st.session_state:
|
24 |
st.session_state['generated'] = []
|
@@ -40,15 +41,29 @@ if 'debate_topic' not in st.session_state:
|
|
40 |
if 'session_num' not in st.session_state:
|
41 |
st.session_state.session_num = 0
|
42 |
|
43 |
-
if '
|
44 |
-
st.session_state.
|
45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
|
47 |
-
|
48 |
-
#session_num = st.session_state.session_num
|
49 |
-
st.session_state.session_num += 1
|
50 |
-
print("st.session_state.session_num", st.session_state.session_num)
|
51 |
-
st.session_state.session_first = False
|
52 |
|
53 |
|
54 |
with st.form("first_form"):
|
@@ -56,12 +71,15 @@ with st.form("first_form"):
|
|
56 |
#############################################
|
57 |
# User id
|
58 |
#############################################
|
|
|
|
|
|
|
59 |
user_id = st.text_input(
|
60 |
"Enter Your User ID",
|
61 |
st.session_state.user_id, # For remain the id
|
62 |
key='user_id'
|
63 |
)
|
64 |
-
|
65 |
#############################################
|
66 |
# Debate Theme
|
67 |
#############################################
|
@@ -159,7 +177,7 @@ with st.form("first_form"):
|
|
159 |
bot_role_list
|
160 |
)
|
161 |
|
162 |
-
# user_id๊ฐ ์๋ ๊ฒฝ์ฐ
|
163 |
if user_id != "":
|
164 |
# user์ id์์ ๊ฐ์ฅ ์ต์ ๋ฐ์ดํฐ 1๊ฐ๋ง ์ฟผ๋ฆฌํจ
|
165 |
item = get_lastest_item(
|
@@ -172,42 +190,31 @@ with st.form("first_form"):
|
|
172 |
|
173 |
# ์ฒ์ ๋ค์ด์จ ์ ์ ๋ผ๋ฉด
|
174 |
if item == []:
|
175 |
-
|
176 |
# ์ด๋ฏธ ๋ฐ์ดํฐ๊ฐ ์๋ ์ ์ ๋ผ๋ฉด, session_num์ 1์ ์ถ๊ฐํ๊ธฐ(๊ฐฑ์ )
|
177 |
else:
|
178 |
-
|
179 |
-
|
180 |
-
elif st.session_state.session_first == False:
|
181 |
-
st.session_state.session_num = st.session_state.session_num
|
182 |
-
#session_num = st.session_state.session_num
|
183 |
-
#print("session_num", session_num)
|
184 |
-
print("st.session_state.session_num", st.session_state.session_num)
|
185 |
else:
|
186 |
print("User_name์ ์
๋ ฅํด์ฃผ์ธ์.")
|
187 |
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
)
|
192 |
-
|
193 |
-
|
194 |
-
#############################################
|
195 |
-
# Chatbot
|
196 |
-
#############################################
|
197 |
-
if 'debate_subject' not in st.session_state:
|
198 |
-
st.session_state.debate_subject = ""
|
199 |
-
|
200 |
-
with st.form('form', clear_on_submit=True):
|
201 |
-
|
202 |
user_input = st.text_input(
|
203 |
'Message',
|
204 |
'',
|
205 |
key='input'
|
206 |
)
|
207 |
-
|
208 |
-
|
|
|
|
|
209 |
|
210 |
-
|
|
|
|
|
|
|
211 |
|
212 |
output = query(
|
213 |
db_table=debate_bot_log_table,
|
|
|
19 |
# Streamlit setting
|
20 |
#############################################
|
21 |
st.header("DEBATE BOT")
|
22 |
+
st.text("If you want to reset your session, refresh the page.")
|
23 |
|
24 |
if 'generated' not in st.session_state:
|
25 |
st.session_state['generated'] = []
|
|
|
41 |
if 'session_num' not in st.session_state:
|
42 |
st.session_state.session_num = 0
|
43 |
|
44 |
+
if 'session_history_exist' not in st.session_state:
|
45 |
+
st.session_state.history_exist = False
|
46 |
|
47 |
+
if 'debate_subject' not in st.session_state:
|
48 |
+
st.session_state.debate_subject = ""
|
49 |
+
|
50 |
+
|
51 |
+
def form_callback(history_exist):
|
52 |
+
|
53 |
+
# ๊ณผ๊ฑฐ ํ์คํ ๋ฆฌ๊ฐ ์๋ค๋ฉด, ์ธ์
๋๋ฒ๋ฅผ 0์ผ๋ก ์ด๊ธฐํ
|
54 |
+
if history_exist == False:
|
55 |
+
st.session_state.session_num = 0
|
56 |
+
# ๊ณผ๊ฑฐ ํ์คํ ๋ฆฌ๊ฐ ์๋ค๋ฉด, ์ ์ ์ ๋ฐ์ดํฐ์์ session_num์ ๊ฐ์ ธ์์ ์ฌ์ฉํจ
|
57 |
+
else:
|
58 |
+
# ๋ง์ฝ session_state์ ์ด์ ๋ํ ๊ธฐ๋ก์ด ์๋ค๋ฉด, session_num์ 1์ ์ถ๊ฐํด์ ์
๋ฐ์ดํธํจ
|
59 |
+
if st.session_state.past == []:
|
60 |
+
st.session_state.session_num += 1
|
61 |
+
# ๋ง์ฝ session_state์ ์ด์ ๋ํ ๊ธฐ๋ก์ด ์๋ค๋ฉด, ์
๋ฐ์ดํธ๊ฐ ํ์์์ผ๋ฏ๋ก session_num์ ์
๋ฐ์ดํธํ์ง ์์ผ
|
62 |
+
else:
|
63 |
+
st.session_state.session_num = st.session_state.session_num
|
64 |
+
#st.experimental_rerun()
|
65 |
|
66 |
+
print("st.session_state.session_num(form_callback)", st.session_state.session_num)
|
|
|
|
|
|
|
|
|
67 |
|
68 |
|
69 |
with st.form("first_form"):
|
|
|
71 |
#############################################
|
72 |
# User id
|
73 |
#############################################
|
74 |
+
|
75 |
+
user_id = ""
|
76 |
+
|
77 |
user_id = st.text_input(
|
78 |
"Enter Your User ID",
|
79 |
st.session_state.user_id, # For remain the id
|
80 |
key='user_id'
|
81 |
)
|
82 |
+
|
83 |
#############################################
|
84 |
# Debate Theme
|
85 |
#############################################
|
|
|
177 |
bot_role_list
|
178 |
)
|
179 |
|
180 |
+
# # user_id๊ฐ ์๋ ๊ฒฝ์ฐ
|
181 |
if user_id != "":
|
182 |
# user์ id์์ ๊ฐ์ฅ ์ต์ ๋ฐ์ดํฐ 1๊ฐ๋ง ์ฟผ๋ฆฌํจ
|
183 |
item = get_lastest_item(
|
|
|
190 |
|
191 |
# ์ฒ์ ๋ค์ด์จ ์ ์ ๋ผ๋ฉด
|
192 |
if item == []:
|
193 |
+
st.session_state.history_exist = False
|
194 |
# ์ด๋ฏธ ๋ฐ์ดํฐ๊ฐ ์๋ ์ ์ ๋ผ๋ฉด, session_num์ 1์ ์ถ๊ฐํ๊ธฐ(๊ฐฑ์ )
|
195 |
else:
|
196 |
+
st.session_state.history_exist = True
|
197 |
+
st.session_state.session_num = item[0]['session_num']
|
|
|
|
|
|
|
|
|
|
|
198 |
else:
|
199 |
print("User_name์ ์
๋ ฅํด์ฃผ์ธ์.")
|
200 |
|
201 |
+
#############################################
|
202 |
+
# User input
|
203 |
+
#############################################
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
204 |
user_input = st.text_input(
|
205 |
'Message',
|
206 |
'',
|
207 |
key='input'
|
208 |
)
|
209 |
+
form_submitted = st.form_submit_button(
|
210 |
+
'Send',
|
211 |
+
on_click=form_callback(st.session_state.history_exist)
|
212 |
+
)
|
213 |
|
214 |
+
#############################################
|
215 |
+
# Query
|
216 |
+
#############################################
|
217 |
+
if form_submitted and user_input:
|
218 |
|
219 |
output = query(
|
220 |
db_table=debate_bot_log_table,
|
modules/db_modules.py
CHANGED
@@ -52,7 +52,7 @@ def get_lastest_item(table, name_of_partition_key, value_of_partition_key, limit
|
|
52 |
|
53 |
response = table.query (
|
54 |
KeyConditionExpression=Key(name_of_partition_key).eq(value_of_partition_key),
|
55 |
-
ScanIndexForward=
|
56 |
Limit=limit_num
|
57 |
)
|
58 |
|
|
|
52 |
|
53 |
response = table.query (
|
54 |
KeyConditionExpression=Key(name_of_partition_key).eq(value_of_partition_key),
|
55 |
+
ScanIndexForward=False,
|
56 |
Limit=limit_num
|
57 |
)
|
58 |
|