Ridealist commited on
Commit
ca0b469
โ€ข
1 Parent(s): 50e66ae

feat: page4_debate_ongoing

Browse files
Files changed (1) hide show
  1. vocal_app.py +98 -3
vocal_app.py CHANGED
@@ -1,4 +1,8 @@
1
  import streamlit as st
 
 
 
 
2
  #import SessionState
3
 
4
  # Page Configuration
@@ -29,6 +33,17 @@ if "page2_tab" not in st.session_state:
29
  st.session_state.page2_tab = "tab1"
30
 
31
 
 
 
 
 
 
 
 
 
 
 
 
32
  # Save function (placeholder)
33
  def save_info(user_id, openAI_token, debate_theme):
34
  # You can add the code to save the submitted info (e.g., to a database)
@@ -60,6 +75,9 @@ def page_2_3_controller():
60
  def page2_tab_controller():
61
  st.session_state.page2_tab = "tab2"
62
 
 
 
 
63
  #########################################################
64
  # Page 1
65
  #########################################################
@@ -204,7 +222,7 @@ def page3():
204
 
205
  st.button(
206
  "Start Debate",
207
- on_click=page2_tab_controller
208
  )
209
 
210
 
@@ -219,6 +237,33 @@ def page3():
219
  #########################################################
220
  # Page4
221
  #########################################################
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
222
  def page4():
223
 
224
  with st.sidebar:
@@ -229,6 +274,55 @@ def page4():
229
  height=100)
230
  st.sidebar.button("Ask")
231
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
232
 
233
  #########################################################
234
  # Page5
@@ -251,8 +345,9 @@ pages = {
251
  "Page 1": page1, # user_id์™€ openai_key๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š” ํŽ˜์ด์ง€
252
  "Page 2": page2, # ์›ํ•˜๋Š” ๊ธฐ๋Šฅ์„ ์„ ํƒํ•˜๋Š” ํŽ˜์ด์ง€
253
  "Page 3": page3, # Total Debate
254
- "Page 4": page4, # Evaluation Only
255
- "Page 5": page5, # Analyzing Utterances
 
256
  }
257
 
258
  selection = st.session_state.page
 
1
  import streamlit as st
2
+ import openai
3
+
4
+ from dotenv import dotenv_values
5
+ from streamlit_chat import message
6
  #import SessionState
7
 
8
  # Page Configuration
 
33
  st.session_state.page2_tab = "tab1"
34
 
35
 
36
+ # Initialize session state variables
37
+ if 'generated' not in st.session_state:
38
+ st.session_state['generated'] = []
39
+ if 'past' not in st.session_state:
40
+ st.session_state['past'] = []
41
+ if 'messages' not in st.session_state:
42
+ st.session_state['messages'] = [
43
+ {"role": "system", "content": "You are a helpful assistant."}
44
+ ]
45
+
46
+
47
  # Save function (placeholder)
48
  def save_info(user_id, openAI_token, debate_theme):
49
  # You can add the code to save the submitted info (e.g., to a database)
 
75
  def page2_tab_controller():
76
  st.session_state.page2_tab = "tab2"
77
 
78
+ def page4_controller():
79
+ st.session_state.page = "Page 4"
80
+
81
  #########################################################
82
  # Page 1
83
  #########################################################
 
222
 
223
  st.button(
224
  "Start Debate",
225
+ on_click=page4_controller
226
  )
227
 
228
 
 
237
  #########################################################
238
  # Page4
239
  #########################################################
240
+
241
+ config = dotenv_values(".env")
242
+
243
+ openai.organization = config.get("OPENAI_ORGANIZATION")
244
+ openai.api_key = config.get("OPENAI_API_KEY")
245
+
246
+ # generate response
247
+ def generate_response(prompt):
248
+ st.session_state['messages'].append({"role": "user", "content": prompt})
249
+
250
+ completion = openai.ChatCompletion.create(
251
+ model = "gpt-3.5-turbo",
252
+ messages = st.session_state['messages']
253
+ )
254
+ response = completion.choices[0].message.content
255
+ st.session_state['messages'].append({"role": "assistant", "content": response})
256
+
257
+ print(st.session_state['messages'])
258
+ # total_tokens = completion.usage.total_tokens
259
+ # prompt_tokens = completion.usage.prompt_tokens
260
+ # completion_tokens = completion.usage.completion_tokens
261
+
262
+ return response #, total_tokens, prompt_tokens, completion_tokens
263
+
264
+ #TODO ์›…๊ธฐํ˜•์ด ์ถ”๊ฐ€ํ•ด๋†“์€ history ์„ธ์…˜ 3๊ฐ€์ง€ ์ถ”๊ฐ€ํ•ด๋†“๊ธฐ
265
+ #TODO ์ „์ฒด ์œ ์ €๊ฐ€ ๋ฐœํ™”ํ•œ ์‹œ๊ฐ„ ๊ธฐ๋กํ•˜๊ธฐ ->
266
+
267
  def page4():
268
 
269
  with st.sidebar:
 
274
  height=100)
275
  st.sidebar.button("Ask")
276
 
277
+ debate_preset = "\n".join([
278
+ "Debate Rules: ",
279
+ "1) This debate will be divided into two teams, pro and con, with two debates on each team.",
280
+ "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.",
281
+ "3) Answer logically with an introduction, body, and conclusion.\n", #add this one.
282
+ "4) If User take pro side, you take con side and vice versa.\n"
283
+ "5) You should comprehend user's chat and figure out whether the user take pro or con side.\n"
284
+ "6) Debate subject: " + st.session_state['topic']
285
+ ])
286
+ st.session_state['messages'] = [
287
+ {"role": "system", "content": debate_preset}
288
+ ]
289
+
290
+ # container for chat history
291
+ response_container = st.container()
292
+ # container for text box
293
+ container = st.container()
294
+
295
+ with container:
296
+ with st.form(key='my_form', clear_on_submit=True):
297
+ user_input = st.text_area("You:", key='input', height=100)
298
+ submit_buttom = st.form_submit_button(label='Send')
299
+
300
+ if submit_buttom and user_input:
301
+ output = generate_response(user_input)
302
+ st.session_state['past'].append(user_input)
303
+ st.session_state['generated'].append(output)
304
+ # st.session_state['model_name'].append(model_name)
305
+ # st.session_state['total_tokens'].append(total_tokens)
306
+
307
+ # from https://openai.com/pricing#language-models
308
+ # if model_name == "GPT-3.5":
309
+ # cost = total_tokens * 0.002 / 1000
310
+ # else:
311
+ # cost = (prompt_tokens * 0.03 + completion_tokens * 0.06) / 1000
312
+
313
+ # st.session_state['cost'].append(cost)
314
+ # st.session_state['total_cost'] += cost
315
+
316
+ if st.session_state['generated']:
317
+ with response_container:
318
+ for i in range(len(st.session_state['generated'])):
319
+ message(st.session_state["past"][i], is_user=True, key=str(i) + '_user')
320
+ message(st.session_state["generated"][i], key=str(i))
321
+ # st.write(
322
+ # f"Model used: {st.session_state['model_name'][i]}; Number of tokens: {st.session_state['total_tokens'][i]}; Cost: ${st.session_state['cost'][i]:.5f}"
323
+ # )
324
+
325
+ print(st.session_state)
326
 
327
  #########################################################
328
  # Page5
 
345
  "Page 1": page1, # user_id์™€ openai_key๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š” ํŽ˜์ด์ง€
346
  "Page 2": page2, # ์›ํ•˜๋Š” ๊ธฐ๋Šฅ์„ ์„ ํƒํ•˜๋Š” ํŽ˜์ด์ง€
347
  "Page 3": page3, # Total Debate
348
+ "Page 4": page4, #
349
+ "Page 5": page5, # Evaluation Only
350
+ # "Page 6": page6, # Analyzing Utterances
351
  }
352
 
353
  selection = st.session_state.page