Lilli98 commited on
Commit
231ebd7
·
verified ·
1 Parent(s): 56e71a6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -30
app.py CHANGED
@@ -1,5 +1,5 @@
1
  # app.py
2
- # @title Beer Game Final Version (v4.2 - Input State Bug Fixed)
3
 
4
  # -----------------------------------------------------------------------------
5
  # 1. Import Libraries
@@ -143,6 +143,10 @@ def step_game(human_final_order: int, human_initial_order: int, ai_suggestion: i
143
  llm_personality, info_sharing = state['llm_personality'], state['info_sharing']
144
  echelon_order = ["Retailer", "Wholesaler", "Distributor", "Factory"]
145
  llm_raw_responses = {}
 
 
 
 
146
 
147
  factory_state = echelons["Factory"]
148
  if state['factory_production_pipeline']: factory_state['inventory'] += state['factory_production_pipeline'].popleft()
@@ -183,6 +187,8 @@ def step_game(human_final_order: int, human_initial_order: int, ai_suggestion: i
183
  log_entry[f'{name}.{key}'] = e[key]
184
  log_entry[f'{name}.llm_raw_response'] = llm_raw_responses.get(name, "")
185
 
 
 
186
  log_entry[f'{human_role}.initial_order'] = human_initial_order
187
  log_entry[f'{human_role}.ai_suggestion'] = ai_suggestion
188
 
@@ -340,45 +346,56 @@ else:
340
  prompt_sugg = get_llm_prompt(human_echelon_state, week, state['llm_personality'], state['info_sharing'], echelons)
341
  ai_suggestion, _ = get_llm_order_decision(prompt_sugg, f"{human_role} (Suggestion)")
342
 
343
- # =============== BUG FIX STARTS HERE ===============
344
- # Initialize the session_state key for the input box only once per step.
345
  if 'final_order_input' not in st.session_state:
346
  st.session_state.final_order_input = ai_suggestion
347
- # =============== BUG FIX ENDS HERE =================
348
 
349
  with st.form(key="final_order_form"):
350
  st.markdown(f"#### **Step 2:** The AI suggests ordering **{ai_suggestion}** units.")
351
  st.markdown("Considering the AI's advice, submit your **final** order to end the week.")
352
-
353
- # =============== BUG FIX STARTS HERE ===============
354
- # Use the 'key' parameter to bind the input to session state.
355
- # Remove the problematic 'value' parameter.
356
- st.number_input(
357
- "Your Final Order Quantity:",
358
- min_value=0,
359
- step=1,
360
- key='final_order_input'
361
- )
362
- # =============== BUG FIX ENDS HERE =================
363
-
364
  if st.form_submit_button("Submit Final Order & Advance to Next Week"):
365
- # =============== BUG FIX STARTS HERE ===============
366
- # Read the final order value directly from session state.
367
  final_order_value = st.session_state.final_order_input
368
-
369
- # Call the game logic
370
  step_game(final_order_value, state['human_initial_order'], ai_suggestion)
371
-
372
- # Clean up the session state key to prepare for the next week.
373
  del st.session_state.final_order_input
374
- # =============== BUG FIX ENDS HERE =================
375
-
376
  st.rerun()
377
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
378
  st.sidebar.header("Game Info")
379
  st.sidebar.markdown(f"**Game ID**: `{state['participant_id']}`\n\n**Current Week**: {week}")
380
  if st.sidebar.button("🔄 Reset Game"):
381
- # Clean up state if the user resets
382
  if 'final_order_input' in st.session_state:
383
  del st.session_state.final_order_input
384
  del st.session_state.game_state
@@ -394,8 +411,4 @@ else:
394
  save_logs_and_upload(state)
395
  if st.button("✨ Start a New Game"):
396
  del st.session_state.game_state
397
- st.rerun()
398
-
399
-
400
-
401
-
 
1
  # app.py
2
+ # @title Beer Game Final Version (v4.3 - Added Weekly Decision Log)
3
 
4
  # -----------------------------------------------------------------------------
5
  # 1. Import Libraries
 
143
  llm_personality, info_sharing = state['llm_personality'], state['info_sharing']
144
  echelon_order = ["Retailer", "Wholesaler", "Distributor", "Factory"]
145
  llm_raw_responses = {}
146
+
147
+ # Store pre-step state for logging
148
+ pre_step_inventory = echelons[human_role]['inventory']
149
+ pre_step_backlog = echelons[human_role]['backlog']
150
 
151
  factory_state = echelons["Factory"]
152
  if state['factory_production_pipeline']: factory_state['inventory'] += state['factory_production_pipeline'].popleft()
 
187
  log_entry[f'{name}.{key}'] = e[key]
188
  log_entry[f'{name}.llm_raw_response'] = llm_raw_responses.get(name, "")
189
 
190
+ log_entry[f'{human_role}.opening_inventory'] = pre_step_inventory
191
+ log_entry[f'{human_role}.opening_backlog'] = pre_step_backlog
192
  log_entry[f'{human_role}.initial_order'] = human_initial_order
193
  log_entry[f'{human_role}.ai_suggestion'] = ai_suggestion
194
 
 
346
  prompt_sugg = get_llm_prompt(human_echelon_state, week, state['llm_personality'], state['info_sharing'], echelons)
347
  ai_suggestion, _ = get_llm_order_decision(prompt_sugg, f"{human_role} (Suggestion)")
348
 
 
 
349
  if 'final_order_input' not in st.session_state:
350
  st.session_state.final_order_input = ai_suggestion
 
351
 
352
  with st.form(key="final_order_form"):
353
  st.markdown(f"#### **Step 2:** The AI suggests ordering **{ai_suggestion}** units.")
354
  st.markdown("Considering the AI's advice, submit your **final** order to end the week.")
355
+ st.number_input("Your Final Order Quantity:", min_value=0, step=1, key='final_order_input')
 
 
 
 
 
 
 
 
 
 
 
356
  if st.form_submit_button("Submit Final Order & Advance to Next Week"):
 
 
357
  final_order_value = st.session_state.final_order_input
 
 
358
  step_game(final_order_value, state['human_initial_order'], ai_suggestion)
 
 
359
  del st.session_state.final_order_input
 
 
360
  st.rerun()
361
 
362
+ # =============== NEW SECTION STARTS HERE ===============
363
+ st.markdown("---")
364
+ with st.expander("📖 Your Weekly Decision Log", expanded=False):
365
+ if not state['logs']:
366
+ st.write("Your weekly history will be displayed here after you complete the first week.")
367
+ else:
368
+ history_df = pd.json_normalize(state['logs'])
369
+
370
+ # Define columns to display for the human player
371
+ human_cols = {
372
+ 'week': 'Week',
373
+ f'{human_role}.opening_inventory': 'Opening Inv.',
374
+ f'{human_role}.opening_backlog': 'Opening Backlog',
375
+ f'{human_role}.incoming_order': 'Incoming Order',
376
+ f'{human_role}.initial_order': 'Your Initial Order',
377
+ f'{human_role}.ai_suggestion': 'AI Suggestion',
378
+ f'{human_role}.order_placed': 'Your Final Order',
379
+ f'{human_role}.weekly_cost': 'Weekly Cost',
380
+ }
381
+
382
+ # Filter and rename columns
383
+ display_df = history_df[list(human_cols.keys())].rename(columns=human_cols)
384
+
385
+ # Format cost column
386
+ display_df['Weekly Cost'] = display_df['Weekly Cost'].apply(lambda x: f"${x:,.2f}")
387
+
388
+ # Display dataframe, sorted with the latest week on top
389
+ st.dataframe(
390
+ display_df.sort_values(by="Week", ascending=False),
391
+ hide_index=True,
392
+ use_container_width=True
393
+ )
394
+ # =============== NEW SECTION ENDS HERE =================
395
+
396
  st.sidebar.header("Game Info")
397
  st.sidebar.markdown(f"**Game ID**: `{state['participant_id']}`\n\n**Current Week**: {week}")
398
  if st.sidebar.button("🔄 Reset Game"):
 
399
  if 'final_order_input' in st.session_state:
400
  del st.session_state.final_order_input
401
  del st.session_state.game_state
 
411
  save_logs_and_upload(state)
412
  if st.button("✨ Start a New Game"):
413
  del st.session_state.game_state
414
+ st.rerun()