Ridealist commited on
Commit
c63094c
1 Parent(s): 6d821b7

feat: implement validation for input & optimizing prompt

Browse files
Files changed (1) hide show
  1. vocal_app.py +73 -79
vocal_app.py CHANGED
@@ -1,4 +1,5 @@
1
  import streamlit as st
 
2
  import openai
3
 
4
  from gtts import gTTS
@@ -8,14 +9,12 @@ from streamlit_chat import message
8
  from dotenv import dotenv_values
9
  from bots.judgement_bot import debate_judgement
10
  from collections import Counter
11
- import re
12
- import math
13
  import time
14
 
15
  from audiorecorder import audiorecorder
16
 
17
  # modules
18
- from modules.gpt_modules import gpt_call
19
  #from modules.whisper_modules import transcribe
20
 
21
  config = dotenv_values(".env")
@@ -77,18 +76,8 @@ if "end_time" not in st.session_state:
77
  if "debate_time" not in st.session_state:
78
  st.session_state.debate_time = 0
79
 
80
-
81
- # Initialize session state variables
82
- if 'generated' not in st.session_state:
83
- st.session_state['generated'] = []
84
-
85
- if 'past' not in st.session_state:
86
- st.session_state['past'] = []
87
-
88
- if 'messages' not in st.session_state:
89
- st.session_state['messages'] = [
90
- {"role": "system", "content": "You are a helpful assistant."}
91
- ]
92
 
93
 
94
  # Save function (placeholder)
@@ -289,16 +278,17 @@ def page3():
289
 
290
  def validate_case(error_message):
291
  if not case1 or not case2 or not case3:
292
- case_error_message.error("Please enter above all", icon="🚨")
 
293
  else:
294
  st.session_state.case1 = case1
295
  st.session_state.case2 = case2
296
  st.session_state.case3 = case3
297
- page4_controller()
298
 
299
  if start:
300
- validate_case(case_error_message)
301
-
302
 
303
  with st.sidebar:
304
  st.sidebar.title('Ask to GPT')
@@ -329,11 +319,23 @@ def page3():
329
 
330
  # generate response
331
  def generate_response(prompt):
332
- st.session_state['messages'].append({"role": "user", "content": prompt})
333
- response = gpt_call(prompt)
334
- st.session_state['messages'].append({"role": "assistant", "content": response})
 
 
335
  return response
336
 
 
 
 
 
 
 
 
 
 
 
337
  def page4():
338
 
339
  # time
@@ -362,23 +364,24 @@ def page4():
362
  value=result,
363
  height=150)
364
 
365
- debate_preset = "\n".join([
366
- "Debate Rules: ",
367
- "1) This debate will be divided into two teams, pro and con, with two debates on each team.",
368
- "2) The order of speaking is: first debater for the pro side, first debater for the con side, second debater for the pro side, second debater for the con side.",
369
- "3) Answer logically with an introduction, body, and conclusion.", #add this one.
370
- "4) Your role : " + st.session_state["pros_and_cons"] + "side debator"
371
- "5) Debate subject: " + st.session_state['topic']
372
- ])
373
- first_prompt = "Now we're going to start. Summarize the subject and your role. And ask user ready to begin."
374
- st.session_state['messages'] = [
375
- {"role": "system", "content": debate_preset}
376
- ]
377
-
378
- response = gpt_call(debate_preset + "\n" + first_prompt, role="system")
379
- st.session_state['messages'].append({"role": "assistant", "content": response})
380
- st.session_state['generated'].append(response)
381
-
 
382
 
383
  # container for chat history
384
  response_container = st.container()
@@ -387,53 +390,44 @@ def page4():
387
 
388
  with container:
389
  with st.form(key='my_form', clear_on_submit=True):
 
 
390
  audio = audiorecorder("Click to record", "Recording...")
391
-
 
392
  print("audio", audio)
393
 
394
- if audio != []:
395
- user_input_exist=True
396
- wav_file = open("audio.wav", "wb")
397
- wav_file.write(audio.tobytes())
398
-
399
- audio_file= open("audio.wav", "rb")
400
-
401
- user_input = openai.Audio.transcribe("whisper-1", audio_file).text
402
- else:
403
- user_input_exist=False
404
- user_input = "Nothing to transcribe"
405
- print("Nothing to transcribe")
406
-
407
  #user_input = st.text_area("You:", key='input', height=100)
408
  submit_buttom = st.form_submit_button(label='Send')
 
409
 
410
  #if submit_buttom and user_input:
411
- if submit_buttom and user_input_exist:
412
- output = generate_response(user_input)
413
- st.session_state['user_debate_history'].append(user_input)
414
- st.session_state['bot_debate_history'].append(output)
415
- st.session_state['total_debate_history'].append(
416
- {
417
- "user" + str(len(st.session_state['user_debate_history'])): user_input,
418
- "bot" + str(len(st.session_state['bot_debate_history'])): output,
419
- }
420
- )
421
- st.session_state['past'].append(user_input)
422
- st.session_state['generated'].append(output)
423
-
424
- if st.session_state['generated']:
425
- with response_container:
426
- message(st.session_state["generated"][0], key=str(0))
427
- for i in range(len(st.session_state['past'])):
428
- message(st.session_state["past"][i], is_user=True, key=str(i) + '_user')
429
-
430
- text_to_speech = gTTS(text=st.session_state["generated"][i + 1], lang='en', slow=False)
431
- text_to_speech.save(f'audio/test_gtts_{str(i)}.mp3')
432
- audio_file = open(f'audio/test_gtts_{str(i)}.mp3', 'rb')
433
- audio_bytes = audio_file.read()
434
- st.audio(audio_bytes, format='audio/ogg')
435
-
436
- message(st.session_state["generated"][i + 1], key=str(i + 1))
437
 
438
  if st.button(
439
  label="Next",
 
1
  import streamlit as st
2
+ import numpy as np
3
  import openai
4
 
5
  from gtts import gTTS
 
9
  from dotenv import dotenv_values
10
  from bots.judgement_bot import debate_judgement
11
  from collections import Counter
 
 
12
  import time
13
 
14
  from audiorecorder import audiorecorder
15
 
16
  # modules
17
+ from modules.gpt_modules import gpt_call, gpt_call_context
18
  #from modules.whisper_modules import transcribe
19
 
20
  config = dotenv_values(".env")
 
76
  if "debate_time" not in st.session_state:
77
  st.session_state.debate_time = 0
78
 
79
+ if "pre_audio" not in st.session_state:
80
+ st.session_state.pre_audio = np.ndarray(())
 
 
 
 
 
 
 
 
 
 
81
 
82
 
83
  # Save function (placeholder)
 
278
 
279
  def validate_case(error_message):
280
  if not case1 or not case2 or not case3:
281
+ case_error_message.error("Please fill out above all", icon="🚨")
282
+ return False
283
  else:
284
  st.session_state.case1 = case1
285
  st.session_state.case2 = case2
286
  st.session_state.case3 = case3
287
+ return True
288
 
289
  if start:
290
+ if validate_case(case_error_message):
291
+ page4_controller()
292
 
293
  with st.sidebar:
294
  st.sidebar.title('Ask to GPT')
 
319
 
320
  # generate response
321
  def generate_response(prompt):
322
+ st.session_state['user_debate_history'].append(prompt)
323
+ st.session_state['total_debate_history'].append({"role": "user", "content": prompt})
324
+ response = gpt_call_context(st.session_state['total_debate_history'])
325
+ st.session_state['bot_debate_history'].append(response)
326
+ st.session_state['total_debate_history'].append({"role": "assistant", "content": response})
327
  return response
328
 
329
+ def execute_stt(audio):
330
+ wav_file = open("audio/audio.wav", "wb")
331
+ wav_file.write(audio.tobytes())
332
+ wav_file.close()
333
+
334
+ audio_file= open("audio/audio.wav", "rb")
335
+ user_input = openai.Audio.transcribe("whisper-1", audio_file).text
336
+ audio_file.close()
337
+ return user_input
338
+
339
  def page4():
340
 
341
  # time
 
364
  value=result,
365
  height=150)
366
 
367
+ # default system prompt settings
368
+ if not st.session_state['total_debate_history']:
369
+ debate_preset = "\n".join([
370
+ "Debate Rules: ",
371
+ "1) This debate will be divided into two teams, pro and con, with two debates on each team.",
372
+ "2) The order of speaking is: first debater for the pro side, first debater for the con side, second debater for the pro side, second debater for the con side.",
373
+ "3) Answer logically with an introduction, body, and conclusion.", #add this one.
374
+ "4) Your role : " + st.session_state["pros_and_cons"] + " side debator",
375
+ "5) Debate subject: " + st.session_state['topic'],
376
+ ])
377
+ first_prompt = "Now we're going to start. Summarize the subject and your role. And ask user ready to begin."
378
+
379
+ st.session_state['total_debate_history'] = [
380
+ {"role": "system", "content": debate_preset}
381
+ ]
382
+ response = gpt_call(debate_preset + "\n" + first_prompt, role="system")
383
+ st.session_state['total_debate_history'].append({"role": "assistant", "content": response})
384
+ st.session_state['bot_debate_history'].append(response)
385
 
386
  # container for chat history
387
  response_container = st.container()
 
390
 
391
  with container:
392
  with st.form(key='my_form', clear_on_submit=True):
393
+ user_input = None
394
+ # record voice
395
  audio = audiorecorder("Click to record", "Recording...")
396
+ if np.array_equal(st.session_state['pre_audio'], audio):
397
+ audio = np.ndarray(())
398
  print("audio", audio)
399
 
 
 
 
 
 
 
 
 
 
 
 
 
 
400
  #user_input = st.text_area("You:", key='input', height=100)
401
  submit_buttom = st.form_submit_button(label='Send')
402
+ send_error_message = st.empty()
403
 
404
  #if submit_buttom and user_input:
405
+ if submit_buttom:
406
+ if audio.any():
407
+ user_input = execute_stt(audio)
408
+ output = generate_response(user_input)
409
+ st.session_state['pre_audio'] = audio
410
+ else:
411
+ send_error_message.error("Please record your voice first", icon="🚨")
412
+ print("Nothing to transcribe")
413
+
414
+ #TODO 사용자 input이 없을 때도 reloading으로 buffering 걸리는 문제 해결
415
+ with response_container:
416
+ message(st.session_state['bot_debate_history'][0], key='0_bot')
417
+ text_to_speech = gTTS(text=st.session_state['bot_debate_history'][0], lang='en', slow=False)
418
+ text_to_speech.save(f'audio/test_gtts_0.mp3')
419
+ audio_file = open(f'audio/test_gtts_0.mp3', 'rb')
420
+ audio_bytes = audio_file.read()
421
+ st.audio(audio_bytes, format='audio/ogg')
422
+
423
+ for i in range(len(st.session_state['user_debate_history'])):
424
+ message(st.session_state['user_debate_history'][i], is_user=True, key=str(i)+'_user')
425
+ message(st.session_state['bot_debate_history'][i + 1], key=str(i + 1)+'_bot')
426
+ text_to_speech = gTTS(text=st.session_state['bot_debate_history'][i + 1], lang='en', slow=False)
427
+ text_to_speech.save(f'audio/test_gtts_{str(i + 1)}.mp3')
428
+ audio_file = open(f'audio/test_gtts_{str(i + 1)}.mp3', 'rb')
429
+ audio_bytes = audio_file.read()
430
+ st.audio(audio_bytes, format='audio/ogg')
431
 
432
  if st.button(
433
  label="Next",