Ridealist commited on
Commit
a867b51
โ€ข
1 Parent(s): b4eff0d

refactor: apply gpt_module in page_view function & delete unused library import and variables

Browse files
Files changed (1) hide show
  1. vocal_app.py +21 -46
vocal_app.py CHANGED
@@ -1,19 +1,16 @@
1
  import streamlit as st
2
- import openai
3
 
4
- from dotenv import dotenv_values
 
5
  from streamlit_chat import message
6
  from modules.gpt_modules import gpt_call
7
- from langchain.prompts import PromptTemplate
8
  from bots.judgement_bot import debate_judgement
9
- import numpy as np
10
- from collections import Counter
11
- import re
12
- #import SessionState
13
 
14
  # Page Configuration
15
  st.set_page_config(page_title="Streamlit App")
16
 
 
17
  if "page" not in st.session_state:
18
  st.session_state.page = "Page 1"
19
 
@@ -39,7 +36,7 @@ if "page2_tab" not in st.session_state:
39
  st.session_state.page2_tab = "tab1"
40
 
41
  if "total_debate_history" not in st.session_state:
42
- st.session_state.total_debate_history = ""
43
 
44
  if "user_debate_history" not in st.session_state:
45
  st.session_state.user_debate_history = []
@@ -48,17 +45,17 @@ if "bot_debate_history" not in st.session_state:
48
  st.session_state.bot_debate_history = []
49
 
50
  if "user_debate_time" not in st.session_state:
51
- st.session_state.user_debate_time = []
52
 
53
  if "pros_and_cons" not in st.session_state:
54
  st.session_state.pros_and_cons = ""
55
 
56
-
57
- # Initialize session state variables
58
  if 'generated' not in st.session_state:
59
  st.session_state['generated'] = []
 
60
  if 'past' not in st.session_state:
61
  st.session_state['past'] = []
 
62
  if 'messages' not in st.session_state:
63
  st.session_state['messages'] = [
64
  {"role": "system", "content": "You are a helpful assistant."}
@@ -272,36 +269,13 @@ def page3():
272
  # Page4
273
  #########################################################
274
 
275
- config = dotenv_values(".env")
276
-
277
- openai.organization = config.get("OPENAI_ORGANIZATION")
278
- openai.api_key = config.get("OPENAI_API_KEY")
279
-
280
- def ask_gpt(promt):
281
- completion = openai.ChatCompletion.create(
282
- model = "gpt-3.5-turbo",
283
- messages = {
284
- "role": "assistant",
285
- "content": promt
286
- }
287
- )
288
- return completion.choices[0].message.content
289
-
290
  # generate response
291
  def generate_response(prompt):
292
  st.session_state['messages'].append({"role": "user", "content": prompt})
293
-
294
- completion = openai.ChatCompletion.create(
295
- model = "gpt-3.5-turbo",
296
- messages = st.session_state['messages']
297
- )
298
- response = completion.choices[0].message.content
299
  st.session_state['messages'].append({"role": "assistant", "content": response})
300
-
301
  return response
302
 
303
- #TODO ์ „์ฒด ์œ ์ €๊ฐ€ ๋ฐœํ™”ํ•œ ์‹œ๊ฐ„ ๊ธฐ๋กํ•˜๊ธฐ -> ์„ธ์…˜์— ์ €์žฅ
304
-
305
  def page4():
306
 
307
  with st.sidebar:
@@ -335,20 +309,11 @@ def page4():
335
  {"role": "system", "content": debate_preset}
336
  ]
337
 
338
- completion = openai.ChatCompletion.create(
339
- model = "gpt-3.5-turbo",
340
- messages = [
341
- {
342
- "role": "system",
343
- "content": debate_preset + "\n" + first_prompt
344
- }
345
- ]
346
- )
347
-
348
- response = completion.choices[0].message.content
349
  st.session_state['messages'].append({"role": "assistant", "content": response})
350
  st.session_state['generated'].append(response)
351
 
 
352
  # container for chat history
353
  response_container = st.container()
354
  # container for text box
@@ -379,10 +344,20 @@ def page4():
379
  message(st.session_state["generated"][0], key=str(0))
380
  for i in range(len(st.session_state['past'])):
381
  message(st.session_state["past"][i], is_user=True, key=str(i) + '_user')
 
 
 
 
 
 
 
382
  message(st.session_state["generated"][i + 1], key=str(i + 1))
383
 
 
384
 
 
385
  print(st.session_state)
 
386
 
387
  #########################################################
388
  # Page5 - Total Debate Evaluation
 
1
  import streamlit as st
 
2
 
3
+ from gtts import gTTS
4
+ from collections import Counter
5
  from streamlit_chat import message
6
  from modules.gpt_modules import gpt_call
 
7
  from bots.judgement_bot import debate_judgement
8
+
 
 
 
9
 
10
  # Page Configuration
11
  st.set_page_config(page_title="Streamlit App")
12
 
13
+ # Initialize session state variables
14
  if "page" not in st.session_state:
15
  st.session_state.page = "Page 1"
16
 
 
36
  st.session_state.page2_tab = "tab1"
37
 
38
  if "total_debate_history" not in st.session_state:
39
+ st.session_state.total_debate_history = []
40
 
41
  if "user_debate_history" not in st.session_state:
42
  st.session_state.user_debate_history = []
 
45
  st.session_state.bot_debate_history = []
46
 
47
  if "user_debate_time" not in st.session_state:
48
+ st.session_state.user_debate_time = ""
49
 
50
  if "pros_and_cons" not in st.session_state:
51
  st.session_state.pros_and_cons = ""
52
 
 
 
53
  if 'generated' not in st.session_state:
54
  st.session_state['generated'] = []
55
+
56
  if 'past' not in st.session_state:
57
  st.session_state['past'] = []
58
+
59
  if 'messages' not in st.session_state:
60
  st.session_state['messages'] = [
61
  {"role": "system", "content": "You are a helpful assistant."}
 
269
  # Page4
270
  #########################################################
271
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
272
  # generate response
273
  def generate_response(prompt):
274
  st.session_state['messages'].append({"role": "user", "content": prompt})
275
+ response = gpt_call(prompt)
 
 
 
 
 
276
  st.session_state['messages'].append({"role": "assistant", "content": response})
 
277
  return response
278
 
 
 
279
  def page4():
280
 
281
  with st.sidebar:
 
309
  {"role": "system", "content": debate_preset}
310
  ]
311
 
312
+ response = gpt_call(debate_preset + "\n" + first_prompt, role="system")
 
 
 
 
 
 
 
 
 
 
313
  st.session_state['messages'].append({"role": "assistant", "content": response})
314
  st.session_state['generated'].append(response)
315
 
316
+
317
  # container for chat history
318
  response_container = st.container()
319
  # container for text box
 
344
  message(st.session_state["generated"][0], key=str(0))
345
  for i in range(len(st.session_state['past'])):
346
  message(st.session_state["past"][i], is_user=True, key=str(i) + '_user')
347
+
348
+ text_to_speech = gTTS(text=st.session_state["generated"][i + 1], lang='en', slow=False)
349
+ text_to_speech.save(f'audio/test_gtts_{str(i)}.mp3')
350
+ audio_file = open(f'audio/test_gtts_{str(i)}.mp3', 'rb')
351
+ audio_bytes = audio_file.read()
352
+ st.audio(audio_bytes, format='audio/ogg')
353
+
354
  message(st.session_state["generated"][i + 1], key=str(i + 1))
355
 
356
+ #TODO ์ „์ฒด ์œ ์ €๊ฐ€ ๋ฐœํ™”ํ•œ ์‹œ๊ฐ„ ๊ธฐ๋กํ•˜๊ธฐ -> ์„ธ์…˜์— ์ €์žฅ
357
 
358
+ print("#"*50)
359
  print(st.session_state)
360
+ print("#"*50)
361
 
362
  #########################################################
363
  # Page5 - Total Debate Evaluation