luulinh90s commited on
Commit
de400cc
1 Parent(s): e7da2b9
Files changed (1) hide show
  1. app.py +32 -11
app.py CHANGED
@@ -48,36 +48,56 @@ def get_method_dir(method):
48
 
49
  METHODS = ["No-XAI", "Dater", "Chain-of-Table", "Plan-of-SQLs"]
50
 
51
- def save_session_data(username, data):
 
52
  try:
53
- timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
54
- file_name = f'{username}_{timestamp}_session.json'
 
 
 
55
  json_data = json.dumps(data, indent=4)
56
  temp_file_path = f"/tmp/{file_name}"
57
  with open(temp_file_path, 'w') as f:
58
  f.write(json_data)
 
59
  api = HfApi()
 
 
 
 
 
 
 
 
 
 
 
60
  api.upload_file(
61
  path_or_fileobj=temp_file_path,
62
- path_in_repo=f"session_data_foward_simulation/{file_name}",
63
  repo_id="luulinh90s/Tabular-LLM-Study-Data",
64
  repo_type="space",
65
  )
66
  os.remove(temp_file_path)
67
- logger.info(f"Session data saved for user {username} in Hugging Face Data Space")
 
68
  except Exception as e:
69
- logger.exception(f"Error saving session data for user {username}: {e}")
 
70
 
71
  def load_session_data(username):
72
  try:
73
  api = HfApi()
74
  files = api.list_repo_files(repo_id="luulinh90s/Tabular-LLM-Study-Data", repo_type="space")
75
- user_files = [f for f in files if f.startswith(f'session_data_foward_simulation/{username}_') and f.endswith('_session.json')]
 
76
  if not user_files:
77
  logger.warning(f"No session data found for user {username}")
78
  return None
79
  latest_file = sorted(user_files, reverse=True)[0]
80
- file_path = hf_hub_download(repo_id="luulinh90s/Tabular-LLM-Study-Data", repo_type="space", filename=latest_file)
 
81
  with open(file_path, 'r') as f:
82
  data = json.load(f)
83
  logger.info(f"Session data loaded for user {username} from Hugging Face Data Space")
@@ -130,14 +150,15 @@ def index():
130
  selected_samples = select_balanced_samples(all_samples)
131
  if len(selected_samples) == 0:
132
  return "No common samples were found", 500
 
133
  session_data = {
134
  'username': username,
135
- 'seed': seed,
136
  'method': method,
137
  'selected_samples': selected_samples,
138
  'current_index': 0,
139
  'responses': [],
140
- 'start_time': datetime.now().isoformat()
141
  }
142
  save_session_data(username, session_data)
143
  return redirect(url_for('experiment', username=username))
@@ -273,7 +294,7 @@ def completed(username):
273
  session_data['true_percentage'] = true_percentage
274
  session_data['false_percentage'] = false_percentage
275
 
276
- save_session_data(username, session_data)
277
 
278
  return render_template('completed.html',
279
  accuracy=accuracy,
 
48
 
49
  METHODS = ["No-XAI", "Dater", "Chain-of-Table", "Plan-of-SQLs"]
50
 
51
+
52
+ def save_session_data(username, data, is_update=False):
53
  try:
54
+ seed = data.get('seed', 'unknown')
55
+ start_time = data.get('start_time', datetime.now().isoformat())
56
+ file_name = f'{username}_seed{seed}_{start_time}_session.json'
57
+ file_name = "".join(c for c in file_name if c.isalnum() or c in ['_', '-', '.'])
58
+
59
  json_data = json.dumps(data, indent=4)
60
  temp_file_path = f"/tmp/{file_name}"
61
  with open(temp_file_path, 'w') as f:
62
  f.write(json_data)
63
+
64
  api = HfApi()
65
+ repo_path = "session_data_foward_simulation"
66
+
67
+ if is_update:
68
+ # If updating, we need to delete the old file first
69
+ existing_files = api.list_repo_files(repo_id="luulinh90s/Tabular-LLM-Study-Data", repo_type="space")
70
+ existing_file = next(
71
+ (f for f in existing_files if f.startswith(f'{repo_path}/{username}_seed{seed}_{start_time}')), None)
72
+ if existing_file:
73
+ api.delete_file(repo_id="luulinh90s/Tabular-LLM-Study-Data", repo_type="space",
74
+ path_in_repo=existing_file)
75
+
76
  api.upload_file(
77
  path_or_fileobj=temp_file_path,
78
+ path_in_repo=f"{repo_path}/{file_name}",
79
  repo_id="luulinh90s/Tabular-LLM-Study-Data",
80
  repo_type="space",
81
  )
82
  os.remove(temp_file_path)
83
+ logger.info(
84
+ f"Session data {'updated' if is_update else 'saved'} for user {username} in Hugging Face Data Space")
85
  except Exception as e:
86
+ logger.exception(f"Error {'updating' if is_update else 'saving'} session data for user {username}: {e}")
87
+
88
 
89
  def load_session_data(username):
90
  try:
91
  api = HfApi()
92
  files = api.list_repo_files(repo_id="luulinh90s/Tabular-LLM-Study-Data", repo_type="space")
93
+ user_files = [f for f in files if
94
+ f.startswith(f'session_data_foward_simulation/{username}_') and f.endswith('_session.json')]
95
  if not user_files:
96
  logger.warning(f"No session data found for user {username}")
97
  return None
98
  latest_file = sorted(user_files, reverse=True)[0]
99
+ file_path = hf_hub_download(repo_id="luulinh90s/Tabular-LLM-Study-Data", repo_type="space",
100
+ filename=latest_file)
101
  with open(file_path, 'r') as f:
102
  data = json.load(f)
103
  logger.info(f"Session data loaded for user {username} from Hugging Face Data Space")
 
150
  selected_samples = select_balanced_samples(all_samples)
151
  if len(selected_samples) == 0:
152
  return "No common samples were found", 500
153
+ start_time = datetime.now().isoformat()
154
  session_data = {
155
  'username': username,
156
+ 'seed': str(seed), # Store as string
157
  'method': method,
158
  'selected_samples': selected_samples,
159
  'current_index': 0,
160
  'responses': [],
161
+ 'start_time': start_time
162
  }
163
  save_session_data(username, session_data)
164
  return redirect(url_for('experiment', username=username))
 
294
  session_data['true_percentage'] = true_percentage
295
  session_data['false_percentage'] = false_percentage
296
 
297
+ save_session_data(username, session_data, is_update=True) # Update the existing file
298
 
299
  return render_template('completed.html',
300
  accuracy=accuracy,