botsi commited on
Commit
0652dbc
·
verified ·
1 Parent(s): 0208b4e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -40
app.py CHANGED
@@ -1,4 +1,4 @@
1
- # Changed now everyting from session_index to session_id
2
 
3
  # Original code from https://huggingface.co/spaces/huggingface-projects/llama-2-7b-chat
4
  # Modified for trust game purposes
@@ -86,7 +86,7 @@ if torch.cuda.is_available():
86
  tokenizer = AutoTokenizer.from_pretrained(model_id)
87
  tokenizer.use_default_system_prompt = False
88
 
89
- def fetch_personalized_data(session_id):
90
  try:
91
  # Connect to the database
92
  with mysql.connector.connect(
@@ -97,13 +97,13 @@ def fetch_personalized_data(session_id):
97
  ) as conn:
98
  # Create a cursor object
99
  with conn.cursor() as cursor:
100
- # Query to fetch relevant data from both tables based on session_id
101
  query = """
102
  SELECT e5390g37581_core.playerNr,
103
  e5390g37581_core.groupNrStart,
104
  e5390g37581_core.subjectNr,
105
  e5390g37581_core.onPage,
106
- e5390g37581_decisions.session_id,
107
  e5390g37581_decisions.transfer1,
108
  e5390g37581_decisions.tripledAmount1,
109
  e5390g37581_decisions.keptForSelf1,
@@ -123,13 +123,13 @@ def fetch_personalized_data(session_id):
123
  FROM e5390g37581_core
124
  JOIN e5390g37581_decisions ON
125
  e5390g37581_core.playerNr = e5390g37581_decisions.playerNr
126
- WHERE e5390g37581_decisions.session_id = %s
127
  UNION ALL
128
  SELECT e5390g37581_core.playerNr,
129
  e5390g37581_core.groupNrStart,
130
  e5390g37581_core.subjectNr,
131
  e5390g37581_core.onPage,
132
- e5390g37581_decisions.session_id,
133
  e5390g37581_decisions.transfer1,
134
  e5390g37581_decisions.tripledAmount1,
135
  e5390g37581_decisions.keptForSelf1,
@@ -154,17 +154,17 @@ def fetch_personalized_data(session_id):
154
  FROM e5390g37581_core
155
  JOIN e5390g37581_decisions
156
  ON e5390g37581_core.playerNr = e5390g37581_decisions.playerNr
157
- WHERE e5390g37581_decisions.session_id = %s
158
- ) AND e5390g37581_decisions.session_id != %s
159
  """
160
- cursor.execute(query,(session_id, session_id, session_id))
161
  # Fetch data row by row
162
  data = [{
163
  'playerNr': row[0],
164
  'groupNrStart': row[1],
165
  'subjectNr': row[2],
166
  'onPage': row[3],
167
- 'session_id': row[4],
168
  'transfer1': row[5],
169
  'tripledAmount1': row[6],
170
  'keptForSelf1': row[7],
@@ -277,18 +277,18 @@ def generate(
277
  #print(params)
278
 
279
  # Assuming params = request.query_params is the dictionary containing the query parameters
280
- # Extract the value of the 'session_id' parameter
281
- session_id = params.get('session_id')
282
 
283
- # Check if session_id_value is None or contains a value
284
- if session_id is not None:
285
- print("Session index:", session_id)
286
  else:
287
- session_id = 'no_session_id'
288
  print("Session index not found or has no value.")
289
 
290
  # Fetch personalized data
291
- personalized_data = fetch_personalized_data(session_id)
292
 
293
  # Construct the input prompt using the functions from the system_prompt_config module
294
  input_prompt = construct_input_prompt(chat_history, message, personalized_data)
@@ -355,52 +355,79 @@ def generate(
355
 
356
  # Iterate over each dictionary in the list
357
  for entry in personalized_data:
358
- # Check if the session_id matches the value in session_id variable
359
- if entry['session_id'] == session_id:
360
  # If a match is found, retrieve the onPage value
361
  onPage = entry['onPage']
362
  break # Break the loop since we found the desired entry
363
 
364
- # Check if onPage is still None (i.e., no onPage found or session_id is None)
365
  if onPage is None:
366
  onPage = "no_onPage"
 
 
 
 
 
367
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
368
  # Print the onPage value
369
- print("onPage for session_id =", session_id, ":", onPage)
370
  onPage_filename, onPage_prompt = map_onPage(onPage)
371
  print("onPage_filename:", onPage_filename)
372
  print("onPage_prompt:", onPage_prompt)
373
 
 
 
 
 
374
  # Save chat history to .csv file on HuggingFace Hub
375
  # Generate filename with bot id and session id
376
- filename = f"{session_id}_{onPage}_{DATA_FILENAME}"
377
  data_file = os.path.join(DATA_DIRECTORY, filename)
378
-
379
  # Generate timestamp
380
  timestamp = datetime.datetime.now()
381
 
 
 
 
 
 
 
 
 
 
 
 
382
  # Check if the file already exists
383
  if os.path.exists(data_file):
384
  # If file exists, load existing data
385
  existing_data = pd.read_csv(data_file)
386
-
387
- # Add timestamp column
388
- conversation_df = pd.DataFrame(conversation)
389
- conversation_df['ip_address'] = request.client.host
390
- conversation_df['readable_sentence'] = readable_sentence
391
- conversation_df['timestamp'] = timestamp
392
-
393
- # Append new conversation to existing data
394
- updated_data = pd.concat([existing_data, conversation_df], ignore_index=True)
395
- updated_data.to_csv(data_file, index=False)
396
  else:
397
- # If file doesn't exist, create new file with conversation data
398
- conversation_df = pd.DataFrame(conversation)
399
- conversation_df['ip_address'] = request.client.host
400
- conversation_df['readable_sentence'] = readable_sentence
401
- conversation_df['timestamp'] = timestamp
402
- conversation_df.to_csv(data_file, index=False)
403
-
404
  print("Updating .csv")
405
  repo.push_to_hub(blocking=False, commit_message=f"Updating data at {timestamp}")
406
 
 
1
+ # Changed now everyting from session_index to PROLIFIC_PID
2
 
3
  # Original code from https://huggingface.co/spaces/huggingface-projects/llama-2-7b-chat
4
  # Modified for trust game purposes
 
86
  tokenizer = AutoTokenizer.from_pretrained(model_id)
87
  tokenizer.use_default_system_prompt = False
88
 
89
+ def fetch_personalized_data(PROLIFIC_PID):
90
  try:
91
  # Connect to the database
92
  with mysql.connector.connect(
 
97
  ) as conn:
98
  # Create a cursor object
99
  with conn.cursor() as cursor:
100
+ # Query to fetch relevant data from both tables based on PROLIFIC_PID
101
  query = """
102
  SELECT e5390g37581_core.playerNr,
103
  e5390g37581_core.groupNrStart,
104
  e5390g37581_core.subjectNr,
105
  e5390g37581_core.onPage,
106
+ e5390g37581_decisions.PROLIFIC_PID,
107
  e5390g37581_decisions.transfer1,
108
  e5390g37581_decisions.tripledAmount1,
109
  e5390g37581_decisions.keptForSelf1,
 
123
  FROM e5390g37581_core
124
  JOIN e5390g37581_decisions ON
125
  e5390g37581_core.playerNr = e5390g37581_decisions.playerNr
126
+ WHERE e5390g37581_decisions.PROLIFIC_PID = %s
127
  UNION ALL
128
  SELECT e5390g37581_core.playerNr,
129
  e5390g37581_core.groupNrStart,
130
  e5390g37581_core.subjectNr,
131
  e5390g37581_core.onPage,
132
+ e5390g37581_decisions.PROLIFIC_PID,
133
  e5390g37581_decisions.transfer1,
134
  e5390g37581_decisions.tripledAmount1,
135
  e5390g37581_decisions.keptForSelf1,
 
154
  FROM e5390g37581_core
155
  JOIN e5390g37581_decisions
156
  ON e5390g37581_core.playerNr = e5390g37581_decisions.playerNr
157
+ WHERE e5390g37581_decisions.PROLIFIC_PID = %s
158
+ ) AND e5390g37581_decisions.PROLIFIC_PID != %s
159
  """
160
+ cursor.execute(query,(PROLIFIC_PID, PROLIFIC_PID, PROLIFIC_PID))
161
  # Fetch data row by row
162
  data = [{
163
  'playerNr': row[0],
164
  'groupNrStart': row[1],
165
  'subjectNr': row[2],
166
  'onPage': row[3],
167
+ 'PROLIFIC_PID': row[4],
168
  'transfer1': row[5],
169
  'tripledAmount1': row[6],
170
  'keptForSelf1': row[7],
 
277
  #print(params)
278
 
279
  # Assuming params = request.query_params is the dictionary containing the query parameters
280
+ # Extract the value of the 'PROLIFIC_PID' parameter
281
+ PROLIFIC_PID = params.get('PROLIFIC_PID')
282
 
283
+ # Check if PROLIFIC_PID_value is None or contains a value
284
+ if PROLIFIC_PID is not None:
285
+ print("Session index:", PROLIFIC_PID)
286
  else:
287
+ PROLIFIC_PID = 'no_PROLIFIC_PID'
288
  print("Session index not found or has no value.")
289
 
290
  # Fetch personalized data
291
+ personalized_data = fetch_personalized_data(PROLIFIC_PID)
292
 
293
  # Construct the input prompt using the functions from the system_prompt_config module
294
  input_prompt = construct_input_prompt(chat_history, message, personalized_data)
 
355
 
356
  # Iterate over each dictionary in the list
357
  for entry in personalized_data:
358
+ # Check if the PROLIFIC_PID matches the value in PROLIFIC_PID variable
359
+ if entry['PROLIFIC_PID'] == PROLIFIC_PID:
360
  # If a match is found, retrieve the onPage value
361
  onPage = entry['onPage']
362
  break # Break the loop since we found the desired entry
363
 
364
+ # Check if onPage is still None (i.e., no onPage found or PROLIFIC_PID is None)
365
  if onPage is None:
366
  onPage = "no_onPage"
367
+
368
+ # Find playerNr and groupNrStart variables in personalized_data to add it to the .csv filename to record
369
+ # Initialize onPage variable to None
370
+ playerNr = None
371
+ groupNrStart = None
372
 
373
+ # Iterate over each dictionary in the list
374
+ for entry in personalized_data:
375
+ # Check if the PROLIFIC_PID matches the value in PROLIFIC_PID variable
376
+ if entry['PROLIFIC_PID'] == PROLIFIC_PID:
377
+ # If a match is found, retrieve the onPage value
378
+ playerNr = entry['playerNr']
379
+ groupNrStart = entry['groupNrStart']
380
+ break # Break the loop since we found the desired entry
381
+
382
+ # Check if playerNr, groupNrStart is still None (i.e., no onPage found or PROLIFIC_PID is None)
383
+ if playerNr is None:
384
+ playerNr = "no_playerNr"
385
+
386
+ if groupNrStart is None:
387
+ groupNrStart = "groupNrStart"
388
+
389
  # Print the onPage value
390
+ print("onPage for PROLIFIC_PID =", PROLIFIC_PID, ":", onPage)
391
  onPage_filename, onPage_prompt = map_onPage(onPage)
392
  print("onPage_filename:", onPage_filename)
393
  print("onPage_prompt:", onPage_prompt)
394
 
395
+ # Print the playerNr, groupNrStart values
396
+ print(groupNrStart)
397
+ print(playerNr)
398
+
399
  # Save chat history to .csv file on HuggingFace Hub
400
  # Generate filename with bot id and session id
401
+ filename = f"{groupNrStart}_{playerNr}_{PROLIFIC_PID}_{onPage}_{DATA_FILENAME}"
402
  data_file = os.path.join(DATA_DIRECTORY, filename)
403
+
404
  # Generate timestamp
405
  timestamp = datetime.datetime.now()
406
 
407
+ # Create a DataFrame for the current conversation turn
408
+ turn_data = {
409
+ "turn_id": len(existing_data) + 1 if existing_data is not None else 1,
410
+ "role": "user",
411
+ "content": user_message,
412
+ "ip_address": request.client.host,
413
+ "readable_sentence": user_readable_sentence,
414
+ "timestamp": timestamp,
415
+ }
416
+ turn_df = pd.DataFrame([turn_data])
417
+
418
  # Check if the file already exists
419
  if os.path.exists(data_file):
420
  # If file exists, load existing data
421
  existing_data = pd.read_csv(data_file)
422
+ # Append the new conversation turn to the existing data
423
+ updated_data = pd.concat([existing_data, turn_df], ignore_index=True)
 
 
 
 
 
 
 
 
424
  else:
425
+ # If file doesn't exist, create new DataFrame with the current conversation turn
426
+ updated_data = turn_df
427
+
428
+ # Write the updated data to the CSV file
429
+ updated_data.to_csv(data_file, index=False)
430
+
 
431
  print("Updating .csv")
432
  repo.push_to_hub(blocking=False, commit_message=f"Updating data at {timestamp}")
433