luulinh90s commited on
Commit
e56d73c
·
1 Parent(s): 09a6458
Files changed (1) hide show
  1. app.py +26 -11
app.py CHANGED
@@ -32,7 +32,6 @@ VISUALIZATION_DIRS_CHAIN_OF_TABLE = {
32
  "FN": "htmls_COT/FN"
33
  }
34
 
35
-
36
  # Load all sample files from the directories based on the selected method
37
  def load_samples(method):
38
  logger.info(f"Loading samples for method: {method}")
@@ -52,7 +51,6 @@ def load_samples(method):
52
  logger.exception(f"Error loading samples from {dir_path}: {e}")
53
  return samples
54
 
55
-
56
  # Randomly select balanced samples
57
  def select_balanced_samples(samples):
58
  try:
@@ -64,11 +62,9 @@ def select_balanced_samples(samples):
64
  logger.exception("Error selecting balanced samples")
65
  return []
66
 
67
-
68
  def generate_random_string(length=8):
69
  return ''.join(random.choices(string.ascii_letters + string.digits, k=length))
70
 
71
-
72
  @app.route('/', methods=['GET', 'POST'])
73
  def index():
74
  logger.info("Rendering index page.")
@@ -82,6 +78,10 @@ def index():
82
  return "Missing username, seed, or method", 400
83
 
84
  try:
 
 
 
 
85
  seed = int(seed)
86
  random.seed(seed)
87
  all_samples = load_samples(method)
@@ -97,11 +97,12 @@ def index():
97
  logger.info(f"Generated filename: {filename}")
98
 
99
  # Save selected samples to a JSON file
 
100
  with open(f'session_data/{filename}', 'w') as f:
101
  json.dump(selected_samples, f)
102
 
103
  session['responses'] = [] # Initialize responses list
104
- session['method'] = method # Store the selected method
105
 
106
  return redirect(url_for('experiment', username=username, sample_index=0, seed=seed, filename=filename))
107
  except Exception as e:
@@ -118,7 +119,14 @@ def experiment(username, sample_index, seed, filename):
118
  with open(f'session_data/{filename}', 'r') as f:
119
  selected_samples = json.load(f)
120
 
121
- method = session.get('method') # Retrieve the selected method
 
 
 
 
 
 
 
122
 
123
  if sample_index >= len(selected_samples):
124
  logger.error(f"Sample index {sample_index} exceeds the number of selected samples {len(selected_samples)}.")
@@ -163,7 +171,6 @@ def experiment(username, sample_index, seed, filename):
163
  logger.exception(f"An error occurred in the experiment route: {e}")
164
  return "An error occurred", 500
165
 
166
-
167
  @app.route('/feedback', methods=['POST'])
168
  def feedback():
169
  try:
@@ -200,7 +207,7 @@ def feedback():
200
  'Username': username,
201
  'Seed': seed,
202
  'Sample ID': sample_id,
203
- 'Task': f"Please make a decision to Accept/Reject the AI prediction based on the explanation.",
204
  'User Feedback': feedback
205
  }
206
 
@@ -219,12 +226,21 @@ def feedback():
219
  logger.exception(f"Error in feedback route: {e}")
220
  return "An error occurred", 500
221
 
222
-
223
  @app.route('/completed/<filename>')
224
  def completed(filename):
225
  try:
226
  responses = session.get('responses', [])
227
- method = session.get('method')
 
 
 
 
 
 
 
 
 
 
228
 
229
  if method == "Chain-of-Table":
230
  json_file = 'Tabular_LLMs_human_study_vis_6_COT.json'
@@ -275,7 +291,6 @@ def completed(filename):
275
  logger.exception(f"Error in completed route: {e}")
276
  return "An error occurred", 500
277
 
278
-
279
  if __name__ == "__main__":
280
  os.makedirs('session_data', exist_ok=True) # Ensure the directory for session files exists
281
  app.run(host="0.0.0.0", port=7860)
 
32
  "FN": "htmls_COT/FN"
33
  }
34
 
 
35
  # Load all sample files from the directories based on the selected method
36
  def load_samples(method):
37
  logger.info(f"Loading samples for method: {method}")
 
51
  logger.exception(f"Error loading samples from {dir_path}: {e}")
52
  return samples
53
 
 
54
  # Randomly select balanced samples
55
  def select_balanced_samples(samples):
56
  try:
 
62
  logger.exception("Error selecting balanced samples")
63
  return []
64
 
 
65
  def generate_random_string(length=8):
66
  return ''.join(random.choices(string.ascii_letters + string.digits, k=length))
67
 
 
68
  @app.route('/', methods=['GET', 'POST'])
69
  def index():
70
  logger.info("Rendering index page.")
 
78
  return "Missing username, seed, or method", 400
79
 
80
  try:
81
+ # Save the method to a file
82
+ with open(f'session_data/method_{username}.txt', 'w') as f:
83
+ f.write(method)
84
+
85
  seed = int(seed)
86
  random.seed(seed)
87
  all_samples = load_samples(method)
 
97
  logger.info(f"Generated filename: {filename}")
98
 
99
  # Save selected samples to a JSON file
100
+ os.makedirs('session_data', exist_ok=True)
101
  with open(f'session_data/{filename}', 'w') as f:
102
  json.dump(selected_samples, f)
103
 
104
  session['responses'] = [] # Initialize responses list
105
+ session['username'] = username # Store the username for later use
106
 
107
  return redirect(url_for('experiment', username=username, sample_index=0, seed=seed, filename=filename))
108
  except Exception as e:
 
119
  with open(f'session_data/{filename}', 'r') as f:
120
  selected_samples = json.load(f)
121
 
122
+ # Read the method from the file
123
+ method_file_path = f'session_data/method_{username}.txt'
124
+ if os.path.exists(method_file_path):
125
+ with open(method_file_path, 'r') as f:
126
+ method = f.read().strip()
127
+ else:
128
+ logger.error(f"Method file not found for user {username}.")
129
+ return "Method file not found", 500
130
 
131
  if sample_index >= len(selected_samples):
132
  logger.error(f"Sample index {sample_index} exceeds the number of selected samples {len(selected_samples)}.")
 
171
  logger.exception(f"An error occurred in the experiment route: {e}")
172
  return "An error occurred", 500
173
 
 
174
  @app.route('/feedback', methods=['POST'])
175
  def feedback():
176
  try:
 
207
  'Username': username,
208
  'Seed': seed,
209
  'Sample ID': sample_id,
210
+ 'Task': "Please make a decision to Accept/Reject the AI prediction based on the explanation.",
211
  'User Feedback': feedback
212
  }
213
 
 
226
  logger.exception(f"Error in feedback route: {e}")
227
  return "An error occurred", 500
228
 
 
229
  @app.route('/completed/<filename>')
230
  def completed(filename):
231
  try:
232
  responses = session.get('responses', [])
233
+ username = session.get('username')
234
+
235
+ # Read the method from the file
236
+ method_file_path = f'session_data/method_{username}.txt'
237
+ if os.path.exists(method_file_path):
238
+ with open(method_file_path, 'r') as f:
239
+ method = f.read().strip()
240
+ os.remove(method_file_path) # Remove the method file after use
241
+ else:
242
+ logger.error("Method file not found.")
243
+ return "Method file not found", 500
244
 
245
  if method == "Chain-of-Table":
246
  json_file = 'Tabular_LLMs_human_study_vis_6_COT.json'
 
291
  logger.exception(f"Error in completed route: {e}")
292
  return "An error occurred", 500
293
 
 
294
  if __name__ == "__main__":
295
  os.makedirs('session_data', exist_ok=True) # Ensure the directory for session files exists
296
  app.run(host="0.0.0.0", port=7860)