Unggi commited on
Commit
6f94f48
โ€ข
1 Parent(s): ee57972

session works!

Browse files
Files changed (2) hide show
  1. app.py +41 -34
  2. 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 'session_first' not in st.session_state:
44
- st.session_state.session_first = True
45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
 
47
- def form_callback():
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
- session_num = 0
176
  # ์ด๋ฏธ ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ๋Š” ์œ ์ €๋ผ๋ฉด, session_num์— 1์„ ์ถ”๊ฐ€ํ•˜๊ธฐ(๊ฐฑ์‹ )
177
  else:
178
- if st.session_state.session_first == True:
179
- st.session_state.session_num = int(item[0]['session_num']) + 1
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
- submitted = st.form_submit_button(
189
- 'Send',
190
- on_click=form_callback
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
- submitted = st.form_submit_button('Send')
208
-
 
 
209
 
210
- if submitted and user_input:
 
 
 
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=True,
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