Spaces:
Runtime error
Runtime error
feat: implement validation for input & optimizing prompt
Browse files- 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 |
-
|
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
|
|
|
293 |
else:
|
294 |
st.session_state.case1 = case1
|
295 |
st.session_state.case2 = case2
|
296 |
st.session_state.case3 = case3
|
297 |
-
|
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['
|
333 |
-
|
334 |
-
st.session_state['
|
|
|
|
|
335 |
return response
|
336 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
337 |
def page4():
|
338 |
|
339 |
# time
|
@@ -362,23 +364,24 @@ def page4():
|
|
362 |
value=result,
|
363 |
height=150)
|
364 |
|
365 |
-
|
366 |
-
|
367 |
-
|
368 |
-
|
369 |
-
|
370 |
-
|
371 |
-
|
372 |
-
|
373 |
-
|
374 |
-
|
375 |
-
|
376 |
-
|
377 |
-
|
378 |
-
|
379 |
-
|
380 |
-
|
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
|
412 |
-
|
413 |
-
|
414 |
-
|
415 |
-
|
416 |
-
|
417 |
-
|
418 |
-
|
419 |
-
|
420 |
-
|
421 |
-
|
422 |
-
|
423 |
-
|
424 |
-
|
425 |
-
|
426 |
-
|
427 |
-
|
428 |
-
|
429 |
-
|
430 |
-
|
431 |
-
|
432 |
-
|
433 |
-
|
434 |
-
|
435 |
-
|
436 |
-
|
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",
|