luulinh90s
commited on
Commit
·
83152ea
1
Parent(s):
48b2398
update
Browse files
app.py
CHANGED
@@ -4,7 +4,6 @@ import random
|
|
4 |
import os
|
5 |
import string
|
6 |
import logging
|
7 |
-
from flask_session import Session # Add this import
|
8 |
|
9 |
# Set up logging
|
10 |
logging.basicConfig(level=logging.INFO,
|
@@ -17,9 +16,6 @@ logger = logging.getLogger(__name__)
|
|
17 |
|
18 |
app = Flask(__name__)
|
19 |
app.config['SECRET_KEY'] = 'supersecretkey' # Change this to a random secret key
|
20 |
-
app.config['SESSION_TYPE'] = 'filesystem' # Use filesystem-based session
|
21 |
-
Session(app) # Initialize the session extension
|
22 |
-
|
23 |
|
24 |
# Directories for visualizations
|
25 |
VISUALIZATION_DIRS_PLAN_OF_SQLS = {
|
@@ -36,6 +32,20 @@ VISUALIZATION_DIRS_CHAIN_OF_TABLE = {
|
|
36 |
"FN": "htmls_COT/FN"
|
37 |
}
|
38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
# Load all sample files from the directories based on the selected method
|
40 |
def load_samples(method):
|
41 |
logger.info(f"Loading samples for method: {method}")
|
@@ -82,60 +92,53 @@ def index():
|
|
82 |
return "Missing username, seed, or method", 400
|
83 |
|
84 |
try:
|
85 |
-
# Save the method to a file
|
86 |
-
with open(f'session_data/method_{username}.txt', 'w') as f:
|
87 |
-
f.write(method)
|
88 |
-
|
89 |
seed = int(seed)
|
90 |
random.seed(seed)
|
91 |
all_samples = load_samples(method)
|
92 |
selected_samples = select_balanced_samples(all_samples)
|
93 |
-
logger.info(f"Number of selected samples: {len(selected_samples)}")
|
|
|
94 |
if len(selected_samples) == 0:
|
95 |
logger.error("No samples were selected.")
|
96 |
return "No samples were selected", 500
|
97 |
|
98 |
-
|
99 |
-
filename = f'{username}_{seed}_{method}_{random_string}.json'
|
100 |
-
|
101 |
logger.info(f"Generated filename: {filename}")
|
102 |
|
103 |
-
# Save
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
return redirect(url_for('experiment', username=username, sample_index=0, seed=seed, filename=filename))
|
117 |
except Exception as e:
|
118 |
logger.exception(f"Error in index route: {e}")
|
119 |
return "An error occurred", 500
|
120 |
return render_template('index.html')
|
121 |
|
122 |
-
@app.route('/experiment/<username
|
123 |
-
def experiment(username
|
124 |
try:
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
logger.info(f"Experiment route - Session data: {session}") # Log session data
|
129 |
-
logger.info(f"Selected samples: {selected_samples}") # Log selected samples
|
130 |
-
|
131 |
-
if not selected_samples:
|
132 |
-
logger.error("No samples in session. Redirecting to index.")
|
133 |
return redirect(url_for('index'))
|
134 |
|
135 |
-
|
136 |
-
|
|
|
|
|
|
|
|
|
137 |
|
138 |
-
visualization_file = selected_samples[
|
139 |
|
140 |
if method == "Chain-of-Table":
|
141 |
vis_dir = 'htmls_COT'
|
@@ -155,80 +158,56 @@ def experiment(username, sample_index, seed, filename):
|
|
155 |
|
156 |
statement = "Please make a decision to Accept/Reject the AI prediction based on the explanation."
|
157 |
|
158 |
-
# Render the template with the correct path for the iframe
|
159 |
return render_template('experiment.html',
|
160 |
-
sample_id=
|
161 |
statement=statement,
|
162 |
visualization=url_for('send_visualization', path=visualization_path),
|
163 |
-
username=username
|
164 |
-
seed=seed,
|
165 |
-
sample_index=sample_index,
|
166 |
-
filename=filename)
|
167 |
except Exception as e:
|
168 |
logger.exception(f"An error occurred in the experiment route: {e}")
|
169 |
return "An error occurred", 500
|
170 |
|
171 |
-
|
172 |
@app.route('/feedback', methods=['POST'])
|
173 |
def feedback():
|
174 |
try:
|
175 |
-
sample_id = request.form['sample_id']
|
176 |
-
feedback = request.form['feedback']
|
177 |
username = request.form['username']
|
178 |
-
|
179 |
-
sample_index = int(request.form['sample_index'])
|
180 |
-
filename = request.form['filename']
|
181 |
|
182 |
-
|
183 |
-
|
|
|
|
|
184 |
|
185 |
# Store the feedback
|
186 |
-
responses.append({
|
187 |
-
'sample_id':
|
188 |
'feedback': feedback
|
189 |
})
|
190 |
-
session['responses'] = responses
|
191 |
|
192 |
-
#
|
193 |
-
|
194 |
-
os.makedirs(result_dir, exist_ok=True)
|
195 |
|
196 |
-
#
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
# Update data with the current feedback
|
205 |
-
data[sample_index] = {
|
206 |
-
'Username': username,
|
207 |
-
'Seed': seed,
|
208 |
-
'Sample ID': sample_id,
|
209 |
-
'Task': "Please make a decision to Accept/Reject the AI prediction based on the explanation.",
|
210 |
-
'User Feedback': feedback
|
211 |
-
}
|
212 |
-
|
213 |
-
# Save updated data to the file
|
214 |
-
with open(filepath, 'w') as f:
|
215 |
-
json.dump(data, f, indent=4)
|
216 |
-
|
217 |
-
next_sample_index = sample_index + 1
|
218 |
-
if next_sample_index >= len(selected_samples):
|
219 |
-
return redirect(url_for('completed', filename=filename))
|
220 |
-
|
221 |
-
return redirect(
|
222 |
-
url_for('experiment', username=username, sample_index=next_sample_index, seed=seed, filename=filename))
|
223 |
except Exception as e:
|
224 |
logger.exception(f"Error in feedback route: {e}")
|
225 |
return "An error occurred", 500
|
226 |
|
227 |
-
@app.route('/completed/<
|
228 |
-
def completed(
|
229 |
try:
|
230 |
-
|
231 |
-
|
|
|
|
|
|
|
|
|
|
|
232 |
|
233 |
if method == "Chain-of-Table":
|
234 |
json_file = 'Tabular_LLMs_human_study_vis_6_COT.json'
|
@@ -245,7 +224,8 @@ def completed(filename):
|
|
245 |
for response in responses:
|
246 |
sample_id = response['sample_id']
|
247 |
feedback = response['feedback']
|
248 |
-
|
|
|
249 |
|
250 |
if feedback.upper() == "TRUE":
|
251 |
accept_count += 1
|
|
|
4 |
import os
|
5 |
import string
|
6 |
import logging
|
|
|
7 |
|
8 |
# Set up logging
|
9 |
logging.basicConfig(level=logging.INFO,
|
|
|
16 |
|
17 |
app = Flask(__name__)
|
18 |
app.config['SECRET_KEY'] = 'supersecretkey' # Change this to a random secret key
|
|
|
|
|
|
|
19 |
|
20 |
# Directories for visualizations
|
21 |
VISUALIZATION_DIRS_PLAN_OF_SQLS = {
|
|
|
32 |
"FN": "htmls_COT/FN"
|
33 |
}
|
34 |
|
35 |
+
# Function to save session data to a file
|
36 |
+
def save_session_data(username, data):
|
37 |
+
os.makedirs('session_data', exist_ok=True)
|
38 |
+
with open(f'session_data/{username}_session.json', 'w') as f:
|
39 |
+
json.dump(data, f)
|
40 |
+
|
41 |
+
# Function to load session data from a file
|
42 |
+
def load_session_data(username):
|
43 |
+
try:
|
44 |
+
with open(f'session_data/{username}_session.json', 'r') as f:
|
45 |
+
return json.load(f)
|
46 |
+
except FileNotFoundError:
|
47 |
+
return None
|
48 |
+
|
49 |
# Load all sample files from the directories based on the selected method
|
50 |
def load_samples(method):
|
51 |
logger.info(f"Loading samples for method: {method}")
|
|
|
92 |
return "Missing username, seed, or method", 400
|
93 |
|
94 |
try:
|
|
|
|
|
|
|
|
|
95 |
seed = int(seed)
|
96 |
random.seed(seed)
|
97 |
all_samples = load_samples(method)
|
98 |
selected_samples = select_balanced_samples(all_samples)
|
99 |
+
logger.info(f"Number of selected samples: {len(selected_samples)}")
|
100 |
+
|
101 |
if len(selected_samples) == 0:
|
102 |
logger.error("No samples were selected.")
|
103 |
return "No samples were selected", 500
|
104 |
|
105 |
+
filename = f'{username}_{seed}_{method}_{generate_random_string()}.json'
|
|
|
|
|
106 |
logger.info(f"Generated filename: {filename}")
|
107 |
|
108 |
+
# Save session data
|
109 |
+
session_data = {
|
110 |
+
'responses': [],
|
111 |
+
'username': username,
|
112 |
+
'selected_samples': selected_samples,
|
113 |
+
'method': method,
|
114 |
+
'filename': filename,
|
115 |
+
'current_index': 0
|
116 |
+
}
|
117 |
+
save_session_data(username, session_data)
|
118 |
+
logger.info(f"Session data saved for user: {username}")
|
119 |
+
|
120 |
+
return redirect(url_for('experiment', username=username))
|
|
|
121 |
except Exception as e:
|
122 |
logger.exception(f"Error in index route: {e}")
|
123 |
return "An error occurred", 500
|
124 |
return render_template('index.html')
|
125 |
|
126 |
+
@app.route('/experiment/<username>', methods=['GET', 'POST'])
|
127 |
+
def experiment(username):
|
128 |
try:
|
129 |
+
session_data = load_session_data(username)
|
130 |
+
if not session_data:
|
131 |
+
logger.error(f"No session data found for user: {username}")
|
|
|
|
|
|
|
|
|
|
|
132 |
return redirect(url_for('index'))
|
133 |
|
134 |
+
selected_samples = session_data['selected_samples']
|
135 |
+
method = session_data['method']
|
136 |
+
current_index = session_data['current_index']
|
137 |
+
|
138 |
+
if current_index >= len(selected_samples):
|
139 |
+
return redirect(url_for('completed', username=username))
|
140 |
|
141 |
+
visualization_file = selected_samples[current_index]
|
142 |
|
143 |
if method == "Chain-of-Table":
|
144 |
vis_dir = 'htmls_COT'
|
|
|
158 |
|
159 |
statement = "Please make a decision to Accept/Reject the AI prediction based on the explanation."
|
160 |
|
|
|
161 |
return render_template('experiment.html',
|
162 |
+
sample_id=current_index,
|
163 |
statement=statement,
|
164 |
visualization=url_for('send_visualization', path=visualization_path),
|
165 |
+
username=username)
|
|
|
|
|
|
|
166 |
except Exception as e:
|
167 |
logger.exception(f"An error occurred in the experiment route: {e}")
|
168 |
return "An error occurred", 500
|
169 |
|
|
|
170 |
@app.route('/feedback', methods=['POST'])
|
171 |
def feedback():
|
172 |
try:
|
|
|
|
|
173 |
username = request.form['username']
|
174 |
+
feedback = request.form['feedback']
|
|
|
|
|
175 |
|
176 |
+
session_data = load_session_data(username)
|
177 |
+
if not session_data:
|
178 |
+
logger.error(f"No session data found for user: {username}")
|
179 |
+
return redirect(url_for('index'))
|
180 |
|
181 |
# Store the feedback
|
182 |
+
session_data['responses'].append({
|
183 |
+
'sample_id': session_data['current_index'],
|
184 |
'feedback': feedback
|
185 |
})
|
|
|
186 |
|
187 |
+
# Move to the next sample
|
188 |
+
session_data['current_index'] += 1
|
|
|
189 |
|
190 |
+
# Save updated session data
|
191 |
+
save_session_data(username, session_data)
|
192 |
+
|
193 |
+
if session_data['current_index'] >= len(session_data['selected_samples']):
|
194 |
+
return redirect(url_for('completed', username=username))
|
195 |
+
|
196 |
+
return redirect(url_for('experiment', username=username))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
197 |
except Exception as e:
|
198 |
logger.exception(f"Error in feedback route: {e}")
|
199 |
return "An error occurred", 500
|
200 |
|
201 |
+
@app.route('/completed/<username>')
|
202 |
+
def completed(username):
|
203 |
try:
|
204 |
+
session_data = load_session_data(username)
|
205 |
+
if not session_data:
|
206 |
+
logger.error(f"No session data found for user: {username}")
|
207 |
+
return redirect(url_for('index'))
|
208 |
+
|
209 |
+
responses = session_data['responses']
|
210 |
+
method = session_data['method']
|
211 |
|
212 |
if method == "Chain-of-Table":
|
213 |
json_file = 'Tabular_LLMs_human_study_vis_6_COT.json'
|
|
|
224 |
for response in responses:
|
225 |
sample_id = response['sample_id']
|
226 |
feedback = response['feedback']
|
227 |
+
visualization_file = session_data['selected_samples'][sample_id]
|
228 |
+
index = visualization_file.split('-')[1].split('.')[0] # Extract index from filename
|
229 |
|
230 |
if feedback.upper() == "TRUE":
|
231 |
accept_count += 1
|