Ridealist commited on
Commit
e4bbff5
1 Parent(s): 3c1ac4e

refactor: apply new business logic in main app

Browse files
Files changed (1) hide show
  1. app.py +76 -23
app.py CHANGED
@@ -2,6 +2,7 @@ import streamlit as st
2
  import numpy as np
3
  import pprint
4
  import time
 
5
 
6
  from decimal import Decimal
7
  from gtts import gTTS
@@ -98,6 +99,10 @@ if "disabled" not in st.session_state:
98
  if "session_num" not in st.session_state:
99
  st.session_state.session_num = 0
100
 
 
 
 
 
101
  #########################################################
102
  # Page Controller
103
  #########################################################
@@ -128,13 +133,26 @@ def page2_tab_controller():
128
  #########################################################
129
  # Page 1
130
  #########################################################
131
- def validate_user_id(id_input):
132
- table = dynamodb.Table('DEBO_user')
133
- users_set = get_all_items(table, 'user_id')
134
- if id_input in users_set:
135
- return True
136
- else:
 
 
 
 
 
 
 
 
 
 
 
137
  return False
 
 
138
 
139
  def save_info(user_id):
140
  # You can add the code to save the submitted info (e.g., to a database)
@@ -157,25 +175,56 @@ def save_info(user_id):
157
  st.session_state.session_num = debate_setting[0]['session_num']
158
 
159
  def page1():
 
 
 
160
  st.header('User Info')
161
- user_input = st.text_input(
 
162
  label='User ID',
163
- max_chars=100,
164
- placeholder="Enter user ID",
165
  )
166
- message = st.empty()
167
-
168
- if user_input:
169
- if validate_user_id(user_input):
170
- save_info(user_input)
171
- message.success('User ID successfully verified!', icon="")
172
- st.session_state.disabled = False
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
173
  else:
174
- message.error('Please fill in correct User ID', icon="🚨")
 
 
 
175
  st.session_state.disabled = True
176
  else:
177
  st.session_state.disabled = True
178
 
 
 
 
179
  st.button(
180
  label='Next',
181
  type='primary',
@@ -439,7 +488,7 @@ def page4():
439
  result = st.session_state.ask_gpt_prev_response
440
  else:
441
  try:
442
- result = gpt_call(user_input)
443
  st.session_state.ask_gpt_prev_response = result
444
  except:
445
  st.warning('Chat-GPT Error : The engine is currently overloaded. Please click "Rerun" button below.', icon="⚠️")
@@ -479,7 +528,7 @@ def generate_response(prompt):
479
  response = "Please speak longer!"
480
  else:
481
  try:
482
- response = gpt_call_context(st.session_state['total_debate_history'])
483
  except:
484
  raise RuntimeError("ChatGPT API Error")
485
 
@@ -498,7 +547,7 @@ def execute_stt(audio):
498
  wav_file.write(audio.tobytes())
499
 
500
  try:
501
- user_input = whisper_transcribe(wav_file)
502
  wav_file.close()
503
  return user_input
504
  except:
@@ -526,7 +575,7 @@ def page5():
526
  result = st.session_state.ask_gpt_prev_response
527
  else:
528
  try:
529
- result = gpt_call(user_input)
530
  st.session_state.ask_gpt_prev_response = result
531
  except:
532
  st.warning('Chat-GPT Error : The engine is currently overloaded. Please click "Rerun" button below.', icon="⚠️")
@@ -577,7 +626,7 @@ def page5():
577
  first_prompt = "Now we're going to start. Summarize the subject and your role. And ask user ready to begin."
578
 
579
  try:
580
- response = gpt_call(debate_preset + "\n" + first_prompt, role="system")
581
  except:
582
  st.warning('Chat-GPT Error : The engine is currently overloaded. Please click "Rerun" button below.', icon="⚠️")
583
  time.sleep(1)
@@ -606,9 +655,13 @@ def page5():
606
 
607
  with container:
608
  with st.form(key='my_form', clear_on_submit=True):
 
 
 
 
609
  user_input = None
610
  # record voice
611
- audio = audiorecorder("⏺️ Click to record", "⏹️ Recording...")
612
  if np.array_equal(st.session_state['pre_audio'], audio):
613
  audio = np.array([])
614
 
 
2
  import numpy as np
3
  import pprint
4
  import time
5
+ import openai
6
 
7
  from decimal import Decimal
8
  from gtts import gTTS
 
99
  if "session_num" not in st.session_state:
100
  st.session_state.session_num = 0
101
 
102
+ # OpenAI API Key
103
+ if "OPENAI_API_KEY" not in st.session_state:
104
+ st.session_state.OPENAI_API_KEY = ""
105
+
106
  #########################################################
107
  # Page Controller
108
  #########################################################
 
133
  #########################################################
134
  # Page 1
135
  #########################################################
136
+ # def validate_user_id(id_input):
137
+ # table = dynamodb.Table('DEBO_user')
138
+ # users_set = get_all_items(table, 'user_id')
139
+ # if id_input in users_set:
140
+ # return False
141
+ # else:
142
+ # return True
143
+
144
+ def validate_openai_api_key(api_key):
145
+ openai.api_key = api_key
146
+ try:
147
+ response = openai.Completion.create(
148
+ engine="davinci",
149
+ prompt="This is a test.",
150
+ max_tokens=5
151
+ )
152
+ except:
153
  return False
154
+ else:
155
+ return True
156
 
157
  def save_info(user_id):
158
  # You can add the code to save the submitted info (e.g., to a database)
 
175
  st.session_state.session_num = debate_setting[0]['session_num']
176
 
177
  def page1():
178
+ val_id = False
179
+ val_api_key = False
180
+
181
  st.header('User Info')
182
+ st.caption('Please enter User ID and OpenAI API Key both:)')
183
+ user_id = st.text_input(
184
  label='User ID',
185
+ max_chars=20,
186
+ placeholder="Enter user ID (anything you want)",
187
  )
188
+ # message_id = st.empty()
189
+ openai_api_key = st.text_input(
190
+ label='OpenAI API Key',
191
+ placeholder="Paste your OpenAI API key (sk-...)",
192
+ help='You can get your API key from https://platform.openai.com/account/api-keys.',
193
+ type="password",
194
+ )
195
+ message_api_key = st.empty()
196
+
197
+ if user_id:
198
+ save_info(user_id)
199
+ val_id = True
200
+ # if validate_user_id(user_id):
201
+ # message_id.success('User ID successfully verified!', icon="✅")
202
+ # save_info(user_id)
203
+ # val_id = True
204
+ # else:
205
+ # message_id.error('Please fill in correct User ID.', icon="🚨")
206
+ # st.session_state.disabled = True
207
+ else:
208
+ # message_id.error('Please fill in User ID.', icon="🚨")
209
+ st.session_state.disabled = True
210
+
211
+ if openai_api_key:
212
+ if validate_openai_api_key(openai_api_key):
213
+ message_api_key.success('OpenAI API Key successfully verified!', icon="✅")
214
+ st.session_state["OPENAI_API_KEY"] = openai_api_key
215
+ val_api_key = True
216
  else:
217
+ message_api_key.error(
218
+ f'AuthenticationError: Incorrect API key provided: "{openai_api_key}".'
219
+ '\nYou can find your API key at https://platform.openai.com/account/api-keys.', icon="🚨"
220
+ )
221
  st.session_state.disabled = True
222
  else:
223
  st.session_state.disabled = True
224
 
225
+ if val_id and val_api_key:
226
+ st.session_state.disabled = False
227
+
228
  st.button(
229
  label='Next',
230
  type='primary',
 
488
  result = st.session_state.ask_gpt_prev_response
489
  else:
490
  try:
491
+ result = gpt_call(st.session_state['OPENAI_API_KEY'], user_input)
492
  st.session_state.ask_gpt_prev_response = result
493
  except:
494
  st.warning('Chat-GPT Error : The engine is currently overloaded. Please click "Rerun" button below.', icon="⚠️")
 
528
  response = "Please speak longer!"
529
  else:
530
  try:
531
+ response = gpt_call_context(st.session_state['OPENAI_API_KEY'], st.session_state['total_debate_history'])
532
  except:
533
  raise RuntimeError("ChatGPT API Error")
534
 
 
547
  wav_file.write(audio.tobytes())
548
 
549
  try:
550
+ user_input = whisper_transcribe(st.session_state['OPENAI_API_KEY'], wav_file)
551
  wav_file.close()
552
  return user_input
553
  except:
 
575
  result = st.session_state.ask_gpt_prev_response
576
  else:
577
  try:
578
+ result = gpt_call(st.session_state['OPENAI_API_KEY'], user_input)
579
  st.session_state.ask_gpt_prev_response = result
580
  except:
581
  st.warning('Chat-GPT Error : The engine is currently overloaded. Please click "Rerun" button below.', icon="⚠️")
 
626
  first_prompt = "Now we're going to start. Summarize the subject and your role. And ask user ready to begin."
627
 
628
  try:
629
+ response = gpt_call(st.session_state['OPENAI_API_KEY'], debate_preset + "\n" + first_prompt, role="system")
630
  except:
631
  st.warning('Chat-GPT Error : The engine is currently overloaded. Please click "Rerun" button below.', icon="⚠️")
632
  time.sleep(1)
 
655
 
656
  with container:
657
  with st.form(key='my_form', clear_on_submit=True):
658
+ st.caption("1. Click '⏺️ Record' button and it turn into '⏹️ Recording...' and say something.")
659
+ st.caption("2. After finish your utterance, click '⏹️ Recording...' button again and it turn off.")
660
+ st.caption("3. Click '💬 Send' button and DEBO process your input in short time and give you response.")
661
+
662
  user_input = None
663
  # record voice
664
+ audio = audiorecorder("⏺️ Record", "⏹️ Recording...")
665
  if np.array_equal(st.session_state['pre_audio'], audio):
666
  audio = np.array([])
667