luulinh90s commited on
Commit
3c6a020
·
1 Parent(s): 9eb7cee
Files changed (1) hide show
  1. app.py +17 -43
app.py CHANGED
@@ -52,7 +52,7 @@ METHODS = ["No-XAI", "Dater", "Chain-of-Table", "Plan-of-SQLs"]
52
  def generate_session_id():
53
  return str(uuid.uuid4())
54
 
55
- def save_session_data(session_id, data, is_update=False):
56
  try:
57
  username = data.get('username', 'unknown')
58
  seed = data.get('seed', 'unknown')
@@ -68,14 +68,6 @@ def save_session_data(session_id, data, is_update=False):
68
  api = HfApi()
69
  repo_path = "session_data_foward_simulation"
70
 
71
- if is_update:
72
- existing_files = api.list_repo_files(repo_id="luulinh90s/Tabular-LLM-Study-Data", repo_type="space")
73
- existing_file = next(
74
- (f for f in existing_files if f.startswith(f'{repo_path}/{username}_seed{seed}_{start_time}_{session_id}')), None)
75
- if existing_file:
76
- api.delete_file(repo_id="luulinh90s/Tabular-LLM-Study-Data", repo_type="space",
77
- path_in_repo=existing_file)
78
-
79
  api.upload_file(
80
  path_or_fileobj=temp_file_path,
81
  path_in_repo=f"{repo_path}/{file_name}",
@@ -83,29 +75,9 @@ def save_session_data(session_id, data, is_update=False):
83
  repo_type="space",
84
  )
85
  os.remove(temp_file_path)
86
- logger.info(
87
- f"Session data {'updated' if is_update else 'saved'} for session {session_id} in Hugging Face Data Space")
88
- except Exception as e:
89
- logger.exception(f"Error {'updating' if is_update else 'saving'} session data for session {session_id}: {e}")
90
-
91
- def load_session_data(session_id):
92
- try:
93
- api = HfApi()
94
- files = api.list_repo_files(repo_id="luulinh90s/Tabular-LLM-Study-Data", repo_type="space")
95
- session_files = [f for f in files if f.endswith(f'{session_id}_session.json')]
96
- if not session_files:
97
- logger.warning(f"No session data found for session {session_id}")
98
- return None
99
- latest_file = sorted(session_files, reverse=True)[0]
100
- file_path = hf_hub_download(repo_id="luulinh90s/Tabular-LLM-Study-Data", repo_type="space",
101
- filename=latest_file)
102
- with open(file_path, 'r') as f:
103
- data = json.load(f)
104
- logger.info(f"Session data loaded for session {session_id} from Hugging Face Data Space")
105
- return data
106
  except Exception as e:
107
- logger.exception(f"Error loading session data for session {session_id}: {e}")
108
- return None
109
 
110
  def load_samples():
111
  common_samples = []
@@ -183,17 +155,16 @@ def index():
183
  'start_time': start_time,
184
  'session_id': session_id
185
  }
186
- save_session_data(session_id, session_data)
187
  return redirect(url_for('explanation', session_id=session_id))
188
  except Exception as e:
189
  logger.exception(f"Error in index route: {e}")
190
  return "An error occurred", 500
191
  return render_template('index.html')
192
 
193
-
194
  @app.route('/explanation/<session_id>')
195
  def explanation(session_id):
196
- session_data = load_session_data(session_id)
197
  if not session_data:
198
  return redirect(url_for('index'))
199
 
@@ -210,7 +181,7 @@ def explanation(session_id):
210
  @app.route('/experiment/<session_id>', methods=['GET', 'POST'])
211
  def experiment(session_id):
212
  try:
213
- session_data = load_session_data(session_id)
214
  if not session_data:
215
  return redirect(url_for('index'))
216
 
@@ -241,19 +212,18 @@ Will it predict the statement as TRUE or FALSE?
241
  logger.exception(f"An error occurred in the experiment route: {e}")
242
  return "An error occurred", 500
243
 
244
-
245
  @app.route('/subjective/<session_id>', methods=['GET', 'POST'])
246
  def subjective(session_id):
247
  if request.method == 'POST':
248
  understanding = request.form.get('understanding')
249
 
250
- session_data = load_session_data(session_id)
251
  if not session_data:
252
  logger.error(f"No session data found for session: {session_id}")
253
  return redirect(url_for('index'))
254
 
255
  session_data['subjective_feedback'] = understanding
256
- save_session_data(session_id, session_data, is_update=True)
257
 
258
  return redirect(url_for('completed', session_id=session_id))
259
 
@@ -265,7 +235,7 @@ def feedback():
265
  session_id = request.form['session_id']
266
  prediction = request.form['prediction']
267
 
268
- session_data = load_session_data(session_id)
269
  if not session_data:
270
  logger.error(f"No session data found for session: {session_id}")
271
  return redirect(url_for('index'))
@@ -276,7 +246,7 @@ def feedback():
276
  })
277
 
278
  session_data['current_index'] += 1
279
- save_session_data(session_id, session_data)
280
  logger.info(f"Prediction saved for session {session_id}, sample {session_data['current_index'] - 1}")
281
 
282
  if session_data['current_index'] >= len(session_data['selected_samples']):
@@ -290,7 +260,7 @@ def feedback():
290
  @app.route('/completed/<session_id>')
291
  def completed(session_id):
292
  try:
293
- session_data = load_session_data(session_id)
294
  if not session_data:
295
  logger.error(f"No session data found for session: {session_id}")
296
  return redirect(url_for('index'))
@@ -333,7 +303,7 @@ def completed(session_id):
333
  model_prediction = ground_truth[ground_truth_key]['prediction'].upper()
334
  if user_prediction.upper() == model_prediction:
335
  correct_predictions += 1
336
-
337
  if user_prediction.upper() == "TRUE":
338
  true_predictions += 1
339
  elif user_prediction.upper() == "FALSE":
@@ -354,7 +324,11 @@ def completed(session_id):
354
  session_data['true_percentage'] = true_percentage
355
  session_data['false_percentage'] = false_percentage
356
 
357
- save_session_data(session_id, session_data, is_update=True) # Update the existing file
 
 
 
 
358
 
359
  return render_template('completed.html',
360
  accuracy=accuracy,
 
52
  def generate_session_id():
53
  return str(uuid.uuid4())
54
 
55
+ def save_session_data(session_id, data):
56
  try:
57
  username = data.get('username', 'unknown')
58
  seed = data.get('seed', 'unknown')
 
68
  api = HfApi()
69
  repo_path = "session_data_foward_simulation"
70
 
 
 
 
 
 
 
 
 
71
  api.upload_file(
72
  path_or_fileobj=temp_file_path,
73
  path_in_repo=f"{repo_path}/{file_name}",
 
75
  repo_type="space",
76
  )
77
  os.remove(temp_file_path)
78
+ logger.info(f"Session data saved for session {session_id} in Hugging Face Data Space")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
  except Exception as e:
80
+ logger.exception(f"Error saving session data for session {session_id}: {e}")
 
81
 
82
  def load_samples():
83
  common_samples = []
 
155
  'start_time': start_time,
156
  'session_id': session_id
157
  }
158
+ session['data'] = session_data # Store data in session instead of committing
159
  return redirect(url_for('explanation', session_id=session_id))
160
  except Exception as e:
161
  logger.exception(f"Error in index route: {e}")
162
  return "An error occurred", 500
163
  return render_template('index.html')
164
 
 
165
  @app.route('/explanation/<session_id>')
166
  def explanation(session_id):
167
+ session_data = session.get('data')
168
  if not session_data:
169
  return redirect(url_for('index'))
170
 
 
181
  @app.route('/experiment/<session_id>', methods=['GET', 'POST'])
182
  def experiment(session_id):
183
  try:
184
+ session_data = session.get('data')
185
  if not session_data:
186
  return redirect(url_for('index'))
187
 
 
212
  logger.exception(f"An error occurred in the experiment route: {e}")
213
  return "An error occurred", 500
214
 
 
215
  @app.route('/subjective/<session_id>', methods=['GET', 'POST'])
216
  def subjective(session_id):
217
  if request.method == 'POST':
218
  understanding = request.form.get('understanding')
219
 
220
+ session_data = session.get('data')
221
  if not session_data:
222
  logger.error(f"No session data found for session: {session_id}")
223
  return redirect(url_for('index'))
224
 
225
  session_data['subjective_feedback'] = understanding
226
+ session['data'] = session_data # Update session data
227
 
228
  return redirect(url_for('completed', session_id=session_id))
229
 
 
235
  session_id = request.form['session_id']
236
  prediction = request.form['prediction']
237
 
238
+ session_data = session.get('data')
239
  if not session_data:
240
  logger.error(f"No session data found for session: {session_id}")
241
  return redirect(url_for('index'))
 
246
  })
247
 
248
  session_data['current_index'] += 1
249
+ session['data'] = session_data # Update session data
250
  logger.info(f"Prediction saved for session {session_id}, sample {session_data['current_index'] - 1}")
251
 
252
  if session_data['current_index'] >= len(session_data['selected_samples']):
 
260
  @app.route('/completed/<session_id>')
261
  def completed(session_id):
262
  try:
263
+ session_data = session.get('data')
264
  if not session_data:
265
  logger.error(f"No session data found for session: {session_id}")
266
  return redirect(url_for('index'))
 
303
  model_prediction = ground_truth[ground_truth_key]['prediction'].upper()
304
  if user_prediction.upper() == model_prediction:
305
  correct_predictions += 1
306
+
307
  if user_prediction.upper() == "TRUE":
308
  true_predictions += 1
309
  elif user_prediction.upper() == "FALSE":
 
324
  session_data['true_percentage'] = true_percentage
325
  session_data['false_percentage'] = false_percentage
326
 
327
+ # Save all the data to Hugging Face at the end
328
+ save_session_data(session_id, session_data)
329
+
330
+ # Clear the session data
331
+ session.pop('data', None)
332
 
333
  return render_template('completed.html',
334
  accuracy=accuracy,