codingchild commited on
Commit
ee57972
1 Parent(s): 2932f05

session change

Browse files
app.py CHANGED
@@ -1,27 +1,23 @@
1
 
2
  import streamlit as st
3
  from streamlit_chat import message
4
- import requests
5
- from time import time
6
- import json
7
- import boto3
8
 
9
- from modules.db_modules import get_db, put_item, get_item, get_lastest_item
10
- from modules.gpt_modules import gpt_call
11
- from bots.debate_bot import debate_bot
 
12
 
13
- import os
14
 
15
-
16
-
17
- # DB
18
  dynamodb = get_db()
19
  debate_bot_log_table = dynamodb.Table('debate_bot_log')
20
 
21
 
22
- ###########
23
- # streamlit
24
- ###########
25
  st.header("DEBATE BOT")
26
 
27
  if 'generated' not in st.session_state:
@@ -31,53 +27,41 @@ if 'past' not in st.session_state:
31
  st.session_state['past'] = []
32
 
33
 
34
- def query(user_id, prompt, debate_subject, bot_role):
35
-
36
- history_list = get_lastest_item(
37
- table=debate_bot_log_table,
38
- name_of_partition_key="user_id",
39
- value_of_partition_key=user_id,
40
- )
41
-
42
- # history가 없다면,
43
- if history_list==[]:
44
- history = ""
45
- # history가 있다면, [{}, {}]
46
- else:
47
- history = ""
48
- history_dummy_list = []
49
- for dic in history_list:
50
- history_dummy_list.append(dic['prompt'])
51
-
52
- history = "\n".join(history_dummy_list)
53
-
54
-
55
- bot_result = debate_bot(prompt, history, debate_subject, bot_role)
56
-
57
- # put_db
58
- save_prompt = "\n".join([
59
- "User: " + prompt,
60
- "Cicero Bot: " + bot_result
61
- ])
62
-
63
- item = {
64
- 'user_id': user_id,
65
- 'prompt': save_prompt,
66
- 'time_stamp': str(time()),
67
- 'debate_subject': debate_subject,
68
- }
69
-
70
- put_item(debate_bot_log_table, item)
71
-
72
- return bot_result
73
-
74
  #############################################
75
  # Setting Form
76
  #############################################
 
 
 
 
77
  if 'debate_topic' not in st.session_state:
78
  st.session_state.debate_topic = ""
79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
  with st.form("first_form"):
 
 
 
 
 
 
 
 
 
 
81
  #############################################
82
  # Debate Theme
83
  #############################################
@@ -174,27 +158,47 @@ with st.form("first_form"):
174
  '3. Choose Role of Bot',
175
  bot_role_list
176
  )
177
-
178
- submitted = st.form_submit_button('Send')
179
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
180
 
181
 
182
  #############################################
183
  # Chatbot
184
  #############################################
185
-
186
- if 'user_id' not in st.session_state:
187
- st.session_state.user_id = ""
188
-
189
  if 'debate_subject' not in st.session_state:
190
  st.session_state.debate_subject = ""
191
 
192
  with st.form('form', clear_on_submit=True):
193
- user_id = st.text_input(
194
- "Enter Your User ID",
195
- st.session_state.user_id, # For remain the id
196
- key='user_id'
197
- )
198
  user_input = st.text_input(
199
  'Message',
200
  '',
@@ -205,7 +209,14 @@ with st.form('form', clear_on_submit=True):
205
 
206
  if submitted and user_input:
207
 
208
- output = query(user_id, user_input, debate_topic, bot_role)
 
 
 
 
 
 
 
209
 
210
  st.session_state.past.append(user_input)
211
  st.session_state.generated.append(output)
 
1
 
2
  import streamlit as st
3
  from streamlit_chat import message
 
 
 
 
4
 
5
+ # modules
6
+ from modules.db_modules import get_db
7
+ from modules.db_modules import get_lastest_item
8
+ from modules.query_modules import query
9
 
 
10
 
11
+ #############################################
12
+ # DB connection
13
+ #############################################
14
  dynamodb = get_db()
15
  debate_bot_log_table = dynamodb.Table('debate_bot_log')
16
 
17
 
18
+ #############################################
19
+ # Streamlit setting
20
+ #############################################
21
  st.header("DEBATE BOT")
22
 
23
  if 'generated' not in st.session_state:
 
27
  st.session_state['past'] = []
28
 
29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  #############################################
31
  # Setting Form
32
  #############################################
33
+
34
+ if 'user_id' not in st.session_state:
35
+ st.session_state.user_id = ""
36
+
37
  if 'debate_topic' not in st.session_state:
38
  st.session_state.debate_topic = ""
39
 
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"):
55
+
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
  #############################################
 
158
  '3. Choose Role of Bot',
159
  bot_role_list
160
  )
 
 
161
 
162
+ # user_id가 있는 경우
163
+ if user_id != "":
164
+ # user의 id에서 가장 최신 데이터 1개만 쿼리함
165
+ item = get_lastest_item(
166
+ table=debate_bot_log_table,
167
+ name_of_partition_key='user_id',
168
+ value_of_partition_key=user_id,
169
+ limit_num=1
170
+ )
171
+ print("item", item)
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
  '',
 
209
 
210
  if submitted and user_input:
211
 
212
+ output = query(
213
+ db_table=debate_bot_log_table,
214
+ user_id=user_id,
215
+ prompt=user_input,
216
+ debate_subject=debate_topic,
217
+ bot_role=bot_role,
218
+ session_num=st.session_state.session_num
219
+ )
220
 
221
  st.session_state.past.append(user_input)
222
  st.session_state.generated.append(output)
modules/db_modules.py CHANGED
@@ -48,12 +48,12 @@ def get_item(table, item):
48
  print('Item not found')
49
  return None
50
 
51
- def get_lastest_item(table, name_of_partition_key, value_of_partition_key):
52
 
53
  response = table.query (
54
  KeyConditionExpression=Key(name_of_partition_key).eq(value_of_partition_key),
55
  ScanIndexForward=True,
56
- Limit=10 # 1
57
  )
58
 
59
  return response['Items']
 
48
  print('Item not found')
49
  return None
50
 
51
+ def get_lastest_item(table, name_of_partition_key, value_of_partition_key, limit_num=10):
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
 
59
  return response['Items']
modules/history_modules.py ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ from modules.db_modules import get_db, put_item, get_item, get_lastest_item
3
+
4
+ def get_history(
5
+ table,
6
+ name_of_partition_key,
7
+ value_of_partition_key,
8
+ session_num
9
+ ):
10
+
11
+ history_list = get_lastest_item(
12
+ table=table,
13
+ name_of_partition_key=name_of_partition_key,
14
+ value_of_partition_key=value_of_partition_key,
15
+ )
16
+
17
+ if history_list==[]:
18
+ history = ""
19
+ else:
20
+ history = ""
21
+ history_dummy_list = []
22
+ for dic in history_list:
23
+ if dic['session_num'] == session_num:
24
+ history_dummy_list.append("User: " + dic['user_prompt'])
25
+ history_dummy_list.append("Bot: " + dic['bot_response'])
26
+
27
+ history = "\n".join(history_dummy_list)
28
+
29
+ return history
modules/query_modules.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ from time import time
3
+ from datetime import datetime
4
+
5
+ # modules
6
+ from modules.db_modules import put_item, get_lastest_item
7
+ from modules.history_modules import get_history
8
+
9
+ # bots
10
+ from bots.debate_bot import debate_bot
11
+
12
+
13
+ def query(
14
+ db_table,
15
+ user_id,
16
+ prompt,
17
+ debate_subject,
18
+ bot_role,
19
+ session_num
20
+ ):
21
+
22
+ print("query session", session_num)
23
+
24
+ history = get_history(
25
+ db_table,
26
+ name_of_partition_key="user_id",
27
+ value_of_partition_key=user_id,
28
+ session_num=session_num
29
+ )
30
+ print("history", history)
31
+
32
+ bot_result = debate_bot(
33
+ prompt,
34
+ history,
35
+ debate_subject,
36
+ bot_role
37
+ )
38
+
39
+ time_stamp = str(datetime.fromtimestamp(time()))
40
+
41
+ item = {
42
+ 'user_id': user_id,
43
+ 'time_stamp': time_stamp,
44
+ 'user_prompt': prompt,
45
+ 'bot_response': bot_result,
46
+ 'debate_subject': debate_subject,
47
+ 'session_num': session_num,
48
+ 'bot_role': bot_role
49
+ }
50
+
51
+ put_item(db_table, item)
52
+
53
+ return bot_result