botsi commited on
Commit
4195198
·
verified ·
1 Parent(s): 49af576

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -91
app.py CHANGED
@@ -239,94 +239,36 @@ def fetch_personalized_data(externalID):
239
  print(f"Error: {err}")
240
  return None
241
 
242
- def extract_variables(all_personalized_data):
243
- extracted_data = []
244
- for row in all_personalized_data:
245
- extracted_row = {}
246
- if row['part'] == 1:
247
- if row['transfer1']:
248
- extracted_row['transfer1'] = row['transfer1']
249
- if row['tripledAmount1']:
250
- extracted_row['tripledAmount1'] = row['tripledAmount1']
251
- if row['keptForSelf1']:
252
- extracted_row['keptForSelf1'] = row['keptForSelf1']
253
- if row['returned1']:
254
- extracted_row['returned1'] = row['returned1']
255
- if row['totalRound2']:
256
- extracted_row['totalRound2'] = row['totalRound2']
257
- if row['transfer2']:
258
- extracted_row['transfer2'] = row['transfer2']
259
- if row['tripledAmount2']:
260
- extracted_row['tripledAmount2'] = row['tripledAmount2']
261
- if row['keptForSelf2']:
262
- extracted_row['keptForSelf2'] = row['keptForSelf2']
263
- if row['returned2']:
264
- extracted_row['returned2'] = row['returned2']
265
- if row['totalRound2']:
266
- extracted_row['totalRound2'] = row['totalRound2']
267
- if row['transfer3']:
268
- extracted_row['transfer3'] = row['transfer3']
269
- if row['tripledAmount3']:
270
- extracted_row['tripledAmount3'] = row['tripledAmount3']
271
- if row['keptForSelf3']:
272
- extracted_row['keptForSelf3'] = row['keptForSelf3']
273
- if row['returned3']:
274
- extracted_row['returned3'] = row['returned3']
275
- if row['totalRound3']:
276
- extracted_row['totalRound3'] = row['totalRound3']
277
- elif row['part'] == 2:
278
- if row['transfer4']:
279
- extracted_row['transfer4'] = row['transfer4']
280
- if row['tripledAmount4']:
281
- extracted_row['tripledAmount4'] = row['tripledAmount4']
282
- if row['keptForSelf4']:
283
- extracted_row['keptForSelf4'] = row['keptForSelf4']
284
- if row['returned4']:
285
- extracted_row['returned4'] = row['returned4']
286
- if row['totalRound4']:
287
- extracted_row['totalRound4'] = row['totalRound4']
288
- if row['transfer5']:
289
- extracted_row['transfer5'] = row['transfer5']
290
- if row['tripledAmount5']:
291
- extracted_row['tripledAmount5'] = row['tripledAmount5']
292
- if row['keptForSelf5']:
293
- extracted_row['keptForSelf5'] = row['keptForSelf5']
294
- if row['returned5']:
295
- extracted_row['returned5'] = row['returned5']
296
- if row['totalRound5']:
297
- extracted_row['totalRound5'] = row['totalRound5']
298
- if row['transfer6']:
299
- extracted_row['transfer6'] = row['transfer6']
300
- if row['tripledAmount6']:
301
- extracted_row['tripledAmount6'] = row['tripledAmount6']
302
- if row['keptForSelf6']:
303
- extracted_row['keptForSelf6'] = row['keptForSelf6']
304
- if extracted_row:
305
- extracted_data.append(extracted_row)
306
- print(extracted_data)
307
  return extracted_data
308
-
309
- ## trust-game-llama-2-7b-chat
310
- # app.py
311
- def get_default_system_prompt(extracted_data, onPage_prompt, role_prompt):
312
- #BOS, EOS = "<s>", "</s>"
313
- #BINST, EINST = "[INST]", "[/INST]"
314
- BSYS, ESYS = "<<SYS>>\n", "\n<</SYS>>\n\n"
315
 
316
- DEFAULT_SYSTEM_PROMPT = f""" You are a smart game assistant for a Trust Game outside of this chat.
317
- Trust Game rules: Two players, The Investor and The Dealer, each play to maximize their own earnings.
318
- There are 3 rounds. Every round follows the same pattern.
319
- - First, each player gets a virtual starting credit of 10 coins.
320
- - Investor's turn: The Investor decides how much they want to investo into a shared pot. The shared pot is tripled automatically before the Dealer's turn.
321
- - Dealer's turn: The Dealer can keep and return as much of the tripled amount as they like. Their virtual starting credit remains untouched.
322
- Earnings from each round are not transferred to the next round. Each or your answers should be maximum 2 sentences long.
323
- Answer in a consistent style. If you are unsure about an answer, do not guess.
324
- Currently it is {role_prompt}’s turn so you are assisting {role_prompt} to make the right decision.
325
- The game is currently in round {onPage_prompt} of 3.
326
- This is what happened in the last rounds: {extracted_data}.
327
- """
328
- print(DEFAULT_SYSTEM_PROMPT)
329
- return DEFAULT_SYSTEM_PROMPT
330
 
331
  def map_onPage(onPage):
332
  # Define the mapping of onPage values to onPage_filename and onPage_prompt
@@ -365,6 +307,29 @@ def map_role(role):
365
  # If the role is not in the mapping, set role_prompt to "unknown"
366
  role_prompt = "unknown"
367
  return role_prompt
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
368
 
369
  ## trust-game-llama-2-7b-chat
370
  # app.py
@@ -416,9 +381,8 @@ def generate(
416
  # Fetch personalized data
417
  #personalized_data = fetch_personalized_data(externalID)
418
  all_personalized_data = fetch_personalized_data(externalID)
419
- print(all_personalized_data)
420
 
421
- # Initialize onPage, playerNr, and groupNrStart variables
422
  onPage = playerNr = groupNrStart = role = part = None
423
 
424
  # Iterate over each dictionary in the list
@@ -449,9 +413,9 @@ def generate(
449
  role_prompt = map_role(role)
450
  print("role_prompt:", role_prompt)
451
 
452
- extracted_data = extract_variables(all_personalized_data)
453
  print(extracted_data)
454
-
455
  # Construct the input prompt using the functions from the system_prompt_config module
456
  input_prompt = construct_input_prompt(chat_history, message, extracted_data, onPage_prompt, role_prompt)
457
 
@@ -578,4 +542,5 @@ with gr.Blocks(css="style.css") as demo:
578
  if __name__ == "__main__":
579
  demo.queue(max_size=20).launch()
580
  #demo.queue(max_size=20)
581
- demo.launch(share=True, debug=True)
 
 
239
  print(f"Error: {err}")
240
  return None
241
 
242
+ def extract_variables(all_personalized_data, part):
243
+ extracted_data = {}
244
+
245
+ if part == "1":
246
+ rounds = range(1, 4) # Rounds 1-3
247
+ elif part == "2":
248
+ rounds = range(1, 4) # Rounds 1-3 for part 2 as well
249
+ else:
250
+ print("No data for the particular part found")
251
+ return None
252
+
253
+ for data in all_personalized_data:
254
+ role = map_role(str(data.get('role', 'unknown'))) # Get the role description
255
+ player_data = {} # Store data for the current player
256
+ for round_num in rounds:
257
+ round_key = f'round{round_num}'
258
+ player_data[round_key] = {}
259
+ for var in ['transfer', 'tripledAmount', 'keptForSelf', 'returned', 'totalRound']:
260
+ var_name = f'{var}{round_num}'
261
+ if data.get(var_name) is not None:
262
+ player_data[round_key][var] = data[var_name]
263
+
264
+ # Update extracted_data with role prompt as key
265
+ if role in extracted_data:
266
+ extracted_data[role].update(player_data)
267
+ else:
268
+ extracted_data[role] = player_data
269
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
270
  return extracted_data
 
 
 
 
 
 
 
271
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
272
 
273
  def map_onPage(onPage):
274
  # Define the mapping of onPage values to onPage_filename and onPage_prompt
 
307
  # If the role is not in the mapping, set role_prompt to "unknown"
308
  role_prompt = "unknown"
309
  return role_prompt
310
+
311
+ ## trust-game-llama-2-7b-chat
312
+ # app.py
313
+ def get_default_system_prompt(extracted_data, onPage_prompt, role_prompt):
314
+ #BOS, EOS = "<s>", "</s>"
315
+ #BINST, EINST = "[INST]", "[/INST]"
316
+ BSYS, ESYS = "<<SYS>>\n", "\n<</SYS>>\n\n"
317
+
318
+ DEFAULT_SYSTEM_PROMPT = f""" You are a smart game assistant for a Trust Game outside of this chat.
319
+ Trust Game rules: Two players, The Investor and The Dealer, each play to maximize their own earnings.
320
+ There are 3 rounds. Every round follows the same pattern.
321
+ - First, each player gets a virtual starting credit of 10 coins.
322
+ - Investor's turn: The Investor decides how much they want to investo into a shared pot. The shared pot is tripled automatically before the Dealer's turn.
323
+ - Dealer's turn: The Dealer can keep and return as much of the tripled amount as they like. Their virtual starting credit remains untouched.
324
+ Earnings from each round are not transferred to the next round. Each or your answers should be maximum 2 sentences long.
325
+ Answer in a consistent style. If you are unsure about an answer, do not guess.
326
+ Currently it is {role_prompt}’s turn so you are assisting {role_prompt} to make the right decision.
327
+ The game is currently in round {onPage_prompt} of 3.
328
+ This is what happened in the last rounds: {extracted_data}.
329
+ """
330
+ print(DEFAULT_SYSTEM_PROMPT)
331
+ return DEFAULT_SYSTEM_PROMPT
332
+
333
 
334
  ## trust-game-llama-2-7b-chat
335
  # app.py
 
381
  # Fetch personalized data
382
  #personalized_data = fetch_personalized_data(externalID)
383
  all_personalized_data = fetch_personalized_data(externalID)
 
384
 
385
+ # Initialize onPage, playerNr, and groupNrStart variables
386
  onPage = playerNr = groupNrStart = role = part = None
387
 
388
  # Iterate over each dictionary in the list
 
413
  role_prompt = map_role(role)
414
  print("role_prompt:", role_prompt)
415
 
416
+ extracted_data = extract_variables(all_personalized_data, part)
417
  print(extracted_data)
418
+
419
  # Construct the input prompt using the functions from the system_prompt_config module
420
  input_prompt = construct_input_prompt(chat_history, message, extracted_data, onPage_prompt, role_prompt)
421
 
 
542
  if __name__ == "__main__":
543
  demo.queue(max_size=20).launch()
544
  #demo.queue(max_size=20)
545
+ demo.launch(share=True, debug=True)
546
+