luulinh90s
commited on
Commit
•
4020dbd
1
Parent(s):
0882179
update
Browse files
app.py
CHANGED
@@ -110,26 +110,34 @@ def index():
|
|
110 |
return "An error occurred", 500
|
111 |
return render_template('index.html')
|
112 |
|
113 |
-
@app.route('/experiment/<username>/<sample_index>/<seed>/<filename>', methods=['GET'])
|
114 |
def experiment(username, sample_index, seed, filename):
|
115 |
try:
|
116 |
sample_index = int(sample_index)
|
|
|
117 |
|
118 |
-
|
119 |
-
|
120 |
-
|
|
|
|
|
121 |
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
|
|
|
|
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)}.")
|
133 |
return redirect(url_for('completed', filename=filename))
|
134 |
|
135 |
visualization_file = selected_samples[sample_index]
|
@@ -152,12 +160,20 @@ def experiment(username, sample_index, seed, filename):
|
|
152 |
|
153 |
statement = "Please make a decision to Accept/Reject the AI prediction based on the explanation."
|
154 |
|
155 |
-
#
|
156 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
157 |
except Exception as e:
|
158 |
logger.exception(f"An error occurred in the experiment route: {e}")
|
159 |
return "An error occurred", 500
|
160 |
|
|
|
161 |
@app.route('/feedback', methods=['POST'])
|
162 |
def feedback():
|
163 |
try:
|
@@ -168,21 +184,21 @@ def feedback():
|
|
168 |
sample_index = int(request.form['sample_index'])
|
169 |
filename = request.form['filename']
|
170 |
|
171 |
-
|
172 |
-
with open(f'session_data/{filename}', 'r') as f:
|
173 |
-
selected_samples = json.load(f)
|
174 |
-
|
175 |
responses = session.get('responses', [])
|
176 |
|
|
|
177 |
responses.append({
|
178 |
'sample_id': sample_id,
|
179 |
'feedback': feedback
|
180 |
})
|
181 |
session['responses'] = responses
|
182 |
|
|
|
183 |
result_dir = 'human_study'
|
184 |
os.makedirs(result_dir, exist_ok=True)
|
185 |
|
|
|
186 |
filepath = os.path.join(result_dir, filename)
|
187 |
if os.path.exists(filepath):
|
188 |
with open(filepath, 'r') as f:
|
@@ -190,6 +206,7 @@ def feedback():
|
|
190 |
else:
|
191 |
data = {}
|
192 |
|
|
|
193 |
data[sample_index] = {
|
194 |
'Username': username,
|
195 |
'Seed': seed,
|
@@ -198,16 +215,16 @@ def feedback():
|
|
198 |
'User Feedback': feedback
|
199 |
}
|
200 |
|
|
|
201 |
with open(filepath, 'w') as f:
|
202 |
json.dump(data, f, indent=4)
|
203 |
|
204 |
-
logger.info(f"Feedback saved for sample {sample_id}")
|
205 |
-
|
206 |
next_sample_index = sample_index + 1
|
207 |
if next_sample_index >= len(selected_samples):
|
208 |
return redirect(url_for('completed', filename=filename))
|
209 |
|
210 |
-
return redirect(
|
|
|
211 |
except Exception as e:
|
212 |
logger.exception(f"Error in feedback route: {e}")
|
213 |
return "An error occurred", 500
|
@@ -216,17 +233,7 @@ def feedback():
|
|
216 |
def completed(filename):
|
217 |
try:
|
218 |
responses = session.get('responses', [])
|
219 |
-
|
220 |
-
|
221 |
-
# Read the method from the file
|
222 |
-
method_file_path = f'session_data/method_{username}.txt'
|
223 |
-
if os.path.exists(method_file_path):
|
224 |
-
with open(method_file_path, 'r') as f:
|
225 |
-
method = f.read().strip()
|
226 |
-
os.remove(method_file_path) # Remove the method file after use
|
227 |
-
else:
|
228 |
-
logger.error("Method file not found.")
|
229 |
-
return "Method file not found", 500
|
230 |
|
231 |
if method == "Chain-of-Table":
|
232 |
json_file = 'Tabular_LLMs_human_study_vis_6_COT.json'
|
@@ -274,7 +281,7 @@ def completed(filename):
|
|
274 |
accept_percentage=accept_percentage,
|
275 |
reject_percentage=reject_percentage)
|
276 |
except Exception as e:
|
277 |
-
logger.exception(f"
|
278 |
return "An error occurred", 500
|
279 |
|
280 |
if __name__ == "__main__":
|
|
|
110 |
return "An error occurred", 500
|
111 |
return render_template('index.html')
|
112 |
|
113 |
+
@app.route('/experiment/<username>/<sample_index>/<seed>/<filename>', methods=['GET', 'POST'])
|
114 |
def experiment(username, sample_index, seed, filename):
|
115 |
try:
|
116 |
sample_index = int(sample_index)
|
117 |
+
selected_samples = session.get('selected_samples', [])
|
118 |
|
119 |
+
if request.method == 'POST':
|
120 |
+
# Process feedback
|
121 |
+
feedback = request.form.get('feedback')
|
122 |
+
if not feedback:
|
123 |
+
return "Feedback is required", 400
|
124 |
|
125 |
+
responses = session.get('responses', [])
|
126 |
+
responses.append({
|
127 |
+
'sample_id': sample_index,
|
128 |
+
'feedback': feedback
|
129 |
+
})
|
130 |
+
session['responses'] = responses
|
131 |
+
|
132 |
+
next_sample_index = sample_index + 1
|
133 |
+
if next_sample_index >= len(selected_samples):
|
134 |
+
return redirect(url_for('completed', filename=filename))
|
135 |
|
136 |
+
return redirect(url_for('experiment', username=username, sample_index=next_sample_index, seed=seed, filename=filename))
|
137 |
+
|
138 |
+
# Render the experiment page
|
139 |
+
method = session.get('method')
|
140 |
if sample_index >= len(selected_samples):
|
|
|
141 |
return redirect(url_for('completed', filename=filename))
|
142 |
|
143 |
visualization_file = selected_samples[sample_index]
|
|
|
160 |
|
161 |
statement = "Please make a decision to Accept/Reject the AI prediction based on the explanation."
|
162 |
|
163 |
+
# Render the template with the correct path for the iframe
|
164 |
+
return render_template('experiment.html',
|
165 |
+
sample_id=sample_index,
|
166 |
+
statement=statement,
|
167 |
+
visualization=url_for('send_visualization', path=visualization_path),
|
168 |
+
username=username,
|
169 |
+
seed=seed,
|
170 |
+
sample_index=sample_index,
|
171 |
+
filename=filename)
|
172 |
except Exception as e:
|
173 |
logger.exception(f"An error occurred in the experiment route: {e}")
|
174 |
return "An error occurred", 500
|
175 |
|
176 |
+
|
177 |
@app.route('/feedback', methods=['POST'])
|
178 |
def feedback():
|
179 |
try:
|
|
|
184 |
sample_index = int(request.form['sample_index'])
|
185 |
filename = request.form['filename']
|
186 |
|
187 |
+
selected_samples = session.get('selected_samples', [])
|
|
|
|
|
|
|
188 |
responses = session.get('responses', [])
|
189 |
|
190 |
+
# Store the feedback
|
191 |
responses.append({
|
192 |
'sample_id': sample_id,
|
193 |
'feedback': feedback
|
194 |
})
|
195 |
session['responses'] = responses
|
196 |
|
197 |
+
# Create the result directory if it doesn't exist
|
198 |
result_dir = 'human_study'
|
199 |
os.makedirs(result_dir, exist_ok=True)
|
200 |
|
201 |
+
# Load existing data if the JSON file exists
|
202 |
filepath = os.path.join(result_dir, filename)
|
203 |
if os.path.exists(filepath):
|
204 |
with open(filepath, 'r') as f:
|
|
|
206 |
else:
|
207 |
data = {}
|
208 |
|
209 |
+
# Update data with the current feedback
|
210 |
data[sample_index] = {
|
211 |
'Username': username,
|
212 |
'Seed': seed,
|
|
|
215 |
'User Feedback': feedback
|
216 |
}
|
217 |
|
218 |
+
# Save updated data to the file
|
219 |
with open(filepath, 'w') as f:
|
220 |
json.dump(data, f, indent=4)
|
221 |
|
|
|
|
|
222 |
next_sample_index = sample_index + 1
|
223 |
if next_sample_index >= len(selected_samples):
|
224 |
return redirect(url_for('completed', filename=filename))
|
225 |
|
226 |
+
return redirect(
|
227 |
+
url_for('experiment', username=username, sample_index=next_sample_index, seed=seed, filename=filename))
|
228 |
except Exception as e:
|
229 |
logger.exception(f"Error in feedback route: {e}")
|
230 |
return "An error occurred", 500
|
|
|
233 |
def completed(filename):
|
234 |
try:
|
235 |
responses = session.get('responses', [])
|
236 |
+
method = session.get('method')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
237 |
|
238 |
if method == "Chain-of-Table":
|
239 |
json_file = 'Tabular_LLMs_human_study_vis_6_COT.json'
|
|
|
281 |
accept_percentage=accept_percentage,
|
282 |
reject_percentage=reject_percentage)
|
283 |
except Exception as e:
|
284 |
+
logger.exception(f"An error occurred in the completed route: {e}")
|
285 |
return "An error occurred", 500
|
286 |
|
287 |
if __name__ == "__main__":
|