Update app.py
Browse files
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 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 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 |
-
|
| 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 |
+
|