TTsamurai commited on
Commit
bc8bdfb
·
1 Parent(s): 1047c8b

code with stock familiarity in the decision making process

Browse files
Files changed (1) hide show
  1. app.py +47 -138
app.py CHANGED
@@ -54,7 +54,6 @@ SYSTEM_INSTRUECTION_PREFERENCE_ELICITATION_FILE = os.path.join(
54
  SUMMARIZATION_PROMPT_FILE = os.path.join(STATIC_FILE, "txt/system_summarization_user_preference_elicitation.txt")
55
 
56
  uuid_this_session = str(uuid.uuid4())
57
- feedback_file_stock_familiarity = Path("user_feedback/") / f"stock_familiarity_{uuid_this_session}.json"
58
  feedback_file_interaction = Path("user_feedback/") / f"interaction_{uuid_this_session}.json"
59
  feedback_file_summarization = Path("user_feedback/") / f"summarization_{uuid_this_session}.json"
60
  feedback_file_round_evaluation = Path("user_feedback/") / f"round_evaluation_{uuid_this_session}.json"
@@ -180,29 +179,32 @@ def get_task_instruction_for_user(context):
180
  user_narrative = user_narrative.replace("\n", "<br>")
181
  html_user_narrative = markdown.markdown(user_narrative)
182
  general_instruction = GENERAL_INSTRUCTION
183
- stock_instruction = f"""
184
  <div style="background-color: #f9f9f9; padding: 20px; border-radius: 10px; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); margin-bottom: 20px; max-height: 780px; overflow-y: auto; overflow-x: hidden;">
185
  <!-- Stock Information (Bold label, Normal ticker name) -->
186
- <h4 style="color: #2c3e50; text-align: center; margin-bottom: 20px; font-size: 20px; font-weight: 600;">
187
- Stock Information
188
- </h4>
 
 
 
189
  <div style="text-align: left; font-weight: normal; font-size: 16px; margin-bottom: 20px;">
190
- <span style="font-weight: bold;">This Round's Stock:</span>
 
 
191
  {ticker_name}
192
  </div>
193
- </div>"""
194
- user_narrative_instruction = f"""
195
- <div style="background-color: #f9f9f9; padding: 20px; border-radius: 10px; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); margin-bottom: 20px; max-height: 780px; overflow-y: auto; overflow-x: hidden;">
196
  <!-- User Narrative (Bold label, Normal narrative) -->
197
- <h4 style="color: #2c3e50; text-align: center; margin-bottom: 20px; font-size: 20px; font-weight: 600;">
198
  User Narrative
199
- </h4>
200
  <div style="text-align: left; font-weight: normal; font-size: 16px; margin-bottom: 20px;">
201
  {html_user_narrative}
202
  </div>
203
  </div>"""
204
 
205
- return general_instruction, stock_instruction, user_narrative_instruction
206
 
207
 
208
  def display_system_instruction_with_html(
@@ -278,35 +280,15 @@ def create_demo():
278
  english_order = ["1", "2", "3", "4", "5"]
279
  with gr.Tab(f"{english_order[order]}-1:Discuss"):
280
  general_instruction = gr.HTML(label="General Instruction")
281
- with gr.Row():
282
- with gr.Column():
283
- stock_instruction = gr.HTML(label="Candidate Stock")
284
- with gr.Column():
285
- stock_familiarity = gr.Radio(
286
- [1, 2, 3, 4, 5, 6, 7],
287
- label="What is your level of familiarity with the candidate stock? (1 = Not Familiar, 7 = Very Familiar)",
288
- )
289
-
290
- with gr.Row():
291
- with gr.Column():
292
- with gr.Row():
293
- stock_familiarity_explain = gr.HTML(
294
- """<div style="background-color: #f8d7da; color: #721c24; padding: 15px; border: 1px solid #f5c6cb; border-radius: 5px; margin-bottom: 20px;">
295
- Please select your familiarity with the stock
296
- </div>"""
297
- )
298
- with gr.Column():
299
- with gr.Row():
300
- stock_familiarity_button = gr.Button(value="Submit Stock Familiarity")
301
  with gr.Row():
302
  with gr.Column():
303
  with gr.Row():
304
- user_narrative_instruction = gr.HTML(label="User Narrative")
305
  with gr.Column():
306
  with gr.Row():
307
  chatbot = gr.Chatbot(height=600)
308
  with gr.Row():
309
- start_conversation = gr.Button(value="Start Conversation", interactive=False)
310
  with gr.Row():
311
  msg = gr.Textbox(scale=1, label="User Input")
312
  with gr.Row():
@@ -356,11 +338,7 @@ def create_demo():
356
  "confidence": confidence,
357
  "evaluation_send_button": evaluation_send_button,
358
  "general_instruction": general_instruction,
359
- "stock_instruction": stock_instruction,
360
- "stock_familiarity_explain": stock_familiarity_explain,
361
- "stock_familiarity_button": stock_familiarity_button,
362
- "stock_familiarity": stock_familiarity,
363
- "user_narrative_instruction": user_narrative_instruction,
364
  "textbox": textbox,
365
  }
366
 
@@ -482,9 +460,6 @@ def create_demo():
482
  confidence,
483
  evaluation_send_button,
484
  textbox,
485
- stock_familiarity,
486
- stock_familiarity_button,
487
- stock_familiarity_explain,
488
  ) = (
489
  tabs["comp"],
490
  tabs["system_instruction_context"],
@@ -499,19 +474,8 @@ def create_demo():
499
  tabs["confidence"],
500
  tabs["evaluation_send_button"],
501
  tabs["textbox"],
502
- tabs["stock_familiarity"],
503
- tabs["stock_familiarity_button"],
504
- tabs["stock_familiarity_explain"],
505
  )
506
  system_instruction = ""
507
- stock_familiarity_button.click(
508
- lambda user_id, comp, stock_familiarity, stock_familiarity_button, start_conversation: respond_familiarity(
509
- user_id, comp, stock_familiarity, stock_familiarity_button, start_conversation
510
- ),
511
- [user_id, comp, stock_familiarity, stock_familiarity_button, start_conversation],
512
- [stock_familiarity_explain, stock_familiarity_button, start_conversation],
513
- )
514
-
515
  start_conversation.click(
516
  lambda user_id, tab_session, history, comp, user_preference_elicitation_session, system_description_without_context, system_instruction_context: respond_start_conversation(
517
  user_id,
@@ -864,31 +828,6 @@ def create_demo():
864
 
865
  return tab_data, "", history
866
 
867
- def respond_familiarity(user_id, tab_name, stock_familiarity, stock_familiarity_button, start_conversation):
868
- if stock_familiarity is None:
869
- return (
870
- """<div style="background-color: #f8d7da; color: #721c24; padding: 15px; border: 1px solid #f5c6cb; border-radius: 5px; margin-bottom: 20px;">
871
- <strong>Please select your familiarity with the stock.</strong>
872
- </div>""",
873
- stock_familiarity_button,
874
- start_conversation,
875
- )
876
- else:
877
- save_feedback(
878
- user_id,
879
- uuid_this_session,
880
- "familiarity",
881
- {"stock": tab_name, "familiarity": stock_familiarity},
882
- feedback_file_stock_familiarity,
883
- )
884
- return (
885
- """<div style="background-color: #d4edda; color: #155724; padding: 15px; border: 1px solid #c3e6cb; border-radius: 5px; margin-bottom: 20px;">
886
- Thank you for submitting your familiarity rating. Please proceed by reading the user narrative, and then press ‘Start Conversation’ to begin the discussion.
887
- </div>""",
888
- gr.Button(value="Familiarity Received", interactive=False),
889
- gr.Button(value="Start Conversation", interactive=True),
890
- )
891
-
892
  def respond_start_conversation(
893
  user_id,
894
  tab_data,
@@ -1214,10 +1153,8 @@ def create_demo():
1214
  def get_context(index, raw_context_list, stock_context_list):
1215
  comp = raw_context_list[index]["short_name"]
1216
  context = stock_context_list[index]
1217
- general_instruction, stock_instruction, user_narrative_instruction = get_task_instruction_for_user(
1218
- raw_context_list[index]
1219
- )
1220
- return comp, context, general_instruction, stock_instruction, user_narrative_instruction
1221
 
1222
  def set_user_id(request: gr.Request):
1223
  user_id = request.username
@@ -1244,34 +1181,18 @@ def create_demo():
1244
  # User Narrative
1245
  user_narrative = get_user_narrative_from_raw(raw_context_list[0]["user_narrative"])
1246
  # Tab Context
1247
- (
1248
- first_comp,
1249
- first_context,
1250
- first_general_instruction,
1251
- first_stock_instruction,
1252
- first_user_narrative_instruction,
1253
- ) = get_context(0, raw_context_list, stock_context_list)
1254
- (
1255
- second_comp,
1256
- second_context,
1257
- second_general_instruction,
1258
- second_stock_instruction,
1259
- second_user_narrative_instruction,
1260
- ) = get_context(1, raw_context_list, stock_context_list)
1261
- (
1262
- third_comp,
1263
- third_context,
1264
- third_general_instruction,
1265
- third_stock_instruction,
1266
- thired_user_narrative_instruction,
1267
- ) = get_context(2, raw_context_list, stock_context_list)
1268
- (
1269
- fourth_comp,
1270
- fourth_context,
1271
- fourth_general_instruction,
1272
- fourth_stock_instruction,
1273
- fourth_user_narrative_instruction,
1274
- ) = get_context(3, raw_context_list, stock_context_list)
1275
  # Final Evaluation
1276
  ranking_first_comp = gr.Dropdown(choices=[1, 2, 3, 4, 5], label=first_comp)
1277
  ranking_second_comp = gr.Dropdown(choices=[1, 2, 3, 4, 5], label=second_comp)
@@ -1288,23 +1209,19 @@ def create_demo():
1288
  first_comp,
1289
  first_context,
1290
  first_general_instruction,
1291
- first_stock_instruction,
1292
- first_user_narrative_instruction,
1293
  second_comp,
1294
  second_context,
1295
  second_general_instruction,
1296
- second_stock_instruction,
1297
- second_user_narrative_instruction,
1298
  third_comp,
1299
  third_context,
1300
  third_general_instruction,
1301
- third_stock_instruction,
1302
- third_user_narrative_instruction,
1303
  fourth_comp,
1304
  fourth_context,
1305
  fourth_general_instruction,
1306
- fourth_stock_instruction,
1307
- fourth_user_narrative_instruction,
1308
  ranking_first_comp,
1309
  ranking_second_comp,
1310
  ranking_third_comp,
@@ -1360,10 +1277,9 @@ def create_demo():
1360
  with gr.Tab("Financial Decision Stage") as financial_decision:
1361
  # Experiment Tag
1362
  first_tab = tab_creation_exploration_stage(0, first_comp, first_context)
1363
- first_general_instruction, first_stock_instruction, first_user_narrative_instruction = (
1364
  first_tab["general_instruction"],
1365
- first_tab["stock_instruction"],
1366
- first_tab["user_narrative_instruction"],
1367
  )
1368
  click_control_exploration_stage(
1369
  first_tab,
@@ -1373,10 +1289,9 @@ def create_demo():
1373
  system_description_without_context,
1374
  )
1375
  second_tab = tab_creation_exploration_stage(1, second_comp, second_context)
1376
- second_general_instruction, second_stock_instruction, second_user_narrative_instruction = (
1377
  second_tab["general_instruction"],
1378
- second_tab["stock_instruction"],
1379
- second_tab["user_narrative_instruction"],
1380
  )
1381
  click_control_exploration_stage(
1382
  second_tab,
@@ -1386,10 +1301,9 @@ def create_demo():
1386
  system_description_without_context,
1387
  )
1388
  third_tab = tab_creation_exploration_stage(2, third_comp, third_context)
1389
- third_general_instruction, third_stock_instruction, third_user_narrative_instruction = (
1390
  third_tab["general_instruction"],
1391
- third_tab["stock_instruction"],
1392
- third_tab["user_narrative_instruction"],
1393
  )
1394
  click_control_exploration_stage(
1395
  third_tab,
@@ -1399,10 +1313,9 @@ def create_demo():
1399
  system_description_without_context,
1400
  )
1401
  fourth_tab = tab_creation_exploration_stage(3, fourth_comp, fourth_context)
1402
- fourth_general_instruction, fourth_stock_instruction, fourth_user_narrative_instruction = (
1403
  fourth_tab["general_instruction"],
1404
- fourth_tab["stock_instruction"],
1405
- fourth_tab["user_narrative_instruction"],
1406
  )
1407
  click_control_exploration_stage(
1408
  fourth_tab,
@@ -1444,23 +1357,19 @@ def create_demo():
1444
  first_comp,
1445
  first_context,
1446
  first_general_instruction,
1447
- first_stock_instruction,
1448
- first_user_narrative_instruction,
1449
  second_comp,
1450
  second_context,
1451
  second_general_instruction,
1452
- second_stock_instruction,
1453
- second_user_narrative_instruction,
1454
  third_comp,
1455
  third_context,
1456
  third_general_instruction,
1457
- third_stock_instruction,
1458
- third_user_narrative_instruction,
1459
  fourth_comp,
1460
  fourth_context,
1461
  fourth_general_instruction,
1462
- fourth_stock_instruction,
1463
- fourth_user_narrative_instruction,
1464
  ranking_first_comp,
1465
  ranking_second_comp,
1466
  ranking_third_comp,
 
54
  SUMMARIZATION_PROMPT_FILE = os.path.join(STATIC_FILE, "txt/system_summarization_user_preference_elicitation.txt")
55
 
56
  uuid_this_session = str(uuid.uuid4())
 
57
  feedback_file_interaction = Path("user_feedback/") / f"interaction_{uuid_this_session}.json"
58
  feedback_file_summarization = Path("user_feedback/") / f"summarization_{uuid_this_session}.json"
59
  feedback_file_round_evaluation = Path("user_feedback/") / f"round_evaluation_{uuid_this_session}.json"
 
179
  user_narrative = user_narrative.replace("\n", "<br>")
180
  html_user_narrative = markdown.markdown(user_narrative)
181
  general_instruction = GENERAL_INSTRUCTION
182
+ round_instruction = f"""
183
  <div style="background-color: #f9f9f9; padding: 20px; border-radius: 10px; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); margin-bottom: 20px; max-height: 780px; overflow-y: auto; overflow-x: hidden;">
184
  <!-- Stock Information (Bold label, Normal ticker name) -->
185
+ <h2 style="color: #2c3e50; text-align: center; margin-bottom: 20px; font-size: 20px; font-weight: 600;">
186
+ Round Info
187
+ </h2>
188
+ <div style="text-align: left; font-size: 20px; font-weight: bold; margin-bottom: 20px;">
189
+ Stock
190
+ </div>
191
  <div style="text-align: left; font-weight: normal; font-size: 16px; margin-bottom: 20px;">
192
+ <span style="font-weight: bold;">
193
+ This Round's Stock:
194
+ </span>
195
  {ticker_name}
196
  </div>
197
+
 
 
198
  <!-- User Narrative (Bold label, Normal narrative) -->
199
+ <div style="text-align: left; font-size: 20px; font-weight: bold; margin-bottom: 20px;">
200
  User Narrative
201
+ </div>
202
  <div style="text-align: left; font-weight: normal; font-size: 16px; margin-bottom: 20px;">
203
  {html_user_narrative}
204
  </div>
205
  </div>"""
206
 
207
+ return general_instruction, round_instruction
208
 
209
 
210
  def display_system_instruction_with_html(
 
280
  english_order = ["1", "2", "3", "4", "5"]
281
  with gr.Tab(f"{english_order[order]}-1:Discuss"):
282
  general_instruction = gr.HTML(label="General Instruction")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
283
  with gr.Row():
284
  with gr.Column():
285
  with gr.Row():
286
+ round_instruction = gr.HTML(label="Round Instruction")
287
  with gr.Column():
288
  with gr.Row():
289
  chatbot = gr.Chatbot(height=600)
290
  with gr.Row():
291
+ start_conversation = gr.Button(value="Start Conversation")
292
  with gr.Row():
293
  msg = gr.Textbox(scale=1, label="User Input")
294
  with gr.Row():
 
338
  "confidence": confidence,
339
  "evaluation_send_button": evaluation_send_button,
340
  "general_instruction": general_instruction,
341
+ "round_instruction": round_instruction,
 
 
 
 
342
  "textbox": textbox,
343
  }
344
 
 
460
  confidence,
461
  evaluation_send_button,
462
  textbox,
 
 
 
463
  ) = (
464
  tabs["comp"],
465
  tabs["system_instruction_context"],
 
474
  tabs["confidence"],
475
  tabs["evaluation_send_button"],
476
  tabs["textbox"],
 
 
 
477
  )
478
  system_instruction = ""
 
 
 
 
 
 
 
 
479
  start_conversation.click(
480
  lambda user_id, tab_session, history, comp, user_preference_elicitation_session, system_description_without_context, system_instruction_context: respond_start_conversation(
481
  user_id,
 
828
 
829
  return tab_data, "", history
830
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
831
  def respond_start_conversation(
832
  user_id,
833
  tab_data,
 
1153
  def get_context(index, raw_context_list, stock_context_list):
1154
  comp = raw_context_list[index]["short_name"]
1155
  context = stock_context_list[index]
1156
+ general_instruction, round_instruction = get_task_instruction_for_user(raw_context_list[index])
1157
+ return comp, context, general_instruction, round_instruction
 
 
1158
 
1159
  def set_user_id(request: gr.Request):
1160
  user_id = request.username
 
1181
  # User Narrative
1182
  user_narrative = get_user_narrative_from_raw(raw_context_list[0]["user_narrative"])
1183
  # Tab Context
1184
+ first_comp, first_context, first_general_instruction, first_round_instruction = get_context(
1185
+ 0, raw_context_list, stock_context_list
1186
+ )
1187
+ second_comp, second_context, second_general_instruction, second_round_instruction = get_context(
1188
+ 1, raw_context_list, stock_context_list
1189
+ )
1190
+ third_comp, third_context, third_general_instruction, third_round_instruction = get_context(
1191
+ 2, raw_context_list, stock_context_list
1192
+ )
1193
+ fourth_comp, fourth_context, fourth_general_instruction, fourth_round_instruction = get_context(
1194
+ 3, raw_context_list, stock_context_list
1195
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1196
  # Final Evaluation
1197
  ranking_first_comp = gr.Dropdown(choices=[1, 2, 3, 4, 5], label=first_comp)
1198
  ranking_second_comp = gr.Dropdown(choices=[1, 2, 3, 4, 5], label=second_comp)
 
1209
  first_comp,
1210
  first_context,
1211
  first_general_instruction,
1212
+ first_round_instruction,
 
1213
  second_comp,
1214
  second_context,
1215
  second_general_instruction,
1216
+ second_round_instruction,
 
1217
  third_comp,
1218
  third_context,
1219
  third_general_instruction,
1220
+ third_round_instruction,
 
1221
  fourth_comp,
1222
  fourth_context,
1223
  fourth_general_instruction,
1224
+ fourth_round_instruction,
 
1225
  ranking_first_comp,
1226
  ranking_second_comp,
1227
  ranking_third_comp,
 
1277
  with gr.Tab("Financial Decision Stage") as financial_decision:
1278
  # Experiment Tag
1279
  first_tab = tab_creation_exploration_stage(0, first_comp, first_context)
1280
+ first_general_instruction, first_round_instruction = (
1281
  first_tab["general_instruction"],
1282
+ first_tab["round_instruction"],
 
1283
  )
1284
  click_control_exploration_stage(
1285
  first_tab,
 
1289
  system_description_without_context,
1290
  )
1291
  second_tab = tab_creation_exploration_stage(1, second_comp, second_context)
1292
+ second_general_instruction, second_round_instruction = (
1293
  second_tab["general_instruction"],
1294
+ second_tab["round_instruction"],
 
1295
  )
1296
  click_control_exploration_stage(
1297
  second_tab,
 
1301
  system_description_without_context,
1302
  )
1303
  third_tab = tab_creation_exploration_stage(2, third_comp, third_context)
1304
+ third_general_instruction, third_round_instruction = (
1305
  third_tab["general_instruction"],
1306
+ third_tab["round_instruction"],
 
1307
  )
1308
  click_control_exploration_stage(
1309
  third_tab,
 
1313
  system_description_without_context,
1314
  )
1315
  fourth_tab = tab_creation_exploration_stage(3, fourth_comp, fourth_context)
1316
+ fourth_general_instruction, fourth_round_instruction = (
1317
  fourth_tab["general_instruction"],
1318
+ fourth_tab["round_instruction"],
 
1319
  )
1320
  click_control_exploration_stage(
1321
  fourth_tab,
 
1357
  first_comp,
1358
  first_context,
1359
  first_general_instruction,
1360
+ first_round_instruction,
 
1361
  second_comp,
1362
  second_context,
1363
  second_general_instruction,
1364
+ second_round_instruction,
 
1365
  third_comp,
1366
  third_context,
1367
  third_general_instruction,
1368
+ third_round_instruction,
 
1369
  fourth_comp,
1370
  fourth_context,
1371
  fourth_general_instruction,
1372
+ fourth_round_instruction,
 
1373
  ranking_first_comp,
1374
  ranking_second_comp,
1375
  ranking_third_comp,