|
from flask import Flask, render_template, request, redirect, url_for, send_from_directory, session |
|
import json |
|
import random |
|
import os |
|
import string |
|
from flask_session import Session |
|
|
|
app = Flask(__name__) |
|
app.config['SECRET_KEY'] = 'supersecretkey' |
|
app.config['SESSION_TYPE'] = 'filesystem' |
|
Session(app) |
|
|
|
|
|
VISUALIZATION_DIRS_PLAN_OF_SQLS = { |
|
"TP": "visualizations/TP", |
|
"TN": "visualizations/TN", |
|
"FP": "visualizations/FP", |
|
"FN": "visualizations/FN" |
|
} |
|
|
|
VISUALIZATION_DIRS_CHAIN_OF_TABLE = { |
|
"TP": "htmls_COT/TP", |
|
"TN": "htmls_COT/TN", |
|
"FP": "htmls_COT/FP", |
|
"FN": "htmls_COT/FN" |
|
} |
|
|
|
|
|
|
|
def load_samples(method): |
|
if method == "Chain-of-Table": |
|
visualization_dirs = VISUALIZATION_DIRS_CHAIN_OF_TABLE |
|
else: |
|
visualization_dirs = VISUALIZATION_DIRS_PLAN_OF_SQLS |
|
|
|
samples = {"TP": [], "TN": [], "FP": [], "FN": []} |
|
for category, dir_path in visualization_dirs.items(): |
|
for filename in os.listdir(dir_path): |
|
if filename.endswith(".html"): |
|
samples[category].append(filename) |
|
return samples |
|
|
|
|
|
|
|
def select_balanced_samples(samples): |
|
tp_fp_samples = random.sample(samples["TP"] + samples["FP"], 5) |
|
tn_fn_samples = random.sample(samples["TN"] + samples["FN"], 5) |
|
return tp_fp_samples + tn_fn_samples |
|
|
|
|
|
def generate_random_string(length=8): |
|
return ''.join(random.choices(string.ascii_letters + string.digits, k=length)) |
|
|
|
|
|
@app.route('/', methods=['GET', 'POST']) |
|
def index(): |
|
if request.method == 'POST': |
|
username = request.form.get('username') |
|
seed = request.form.get('seed') |
|
method = request.form.get('method') |
|
|
|
if not username or not seed or not method: |
|
return "Missing username, seed, or method", 400 |
|
|
|
seed = int(seed) |
|
random.seed(seed) |
|
all_samples = load_samples(method) |
|
selected_samples = select_balanced_samples(all_samples) |
|
random_string = generate_random_string() |
|
filename = f'{username}_{seed}_{method}_{random_string}.json' |
|
|
|
session['selected_samples'] = selected_samples |
|
session['responses'] = [] |
|
session['method'] = method |
|
|
|
return redirect(url_for('experiment', username=username, sample_index=0, seed=seed, filename=filename)) |
|
return render_template('index.html') |
|
|
|
|
|
@app.route('/experiment/<username>/<sample_index>/<seed>/<filename>', methods=['GET']) |
|
def experiment(username, sample_index, seed, filename): |
|
sample_index = int(sample_index) |
|
selected_samples = session.get('selected_samples', []) |
|
method = session.get('method') |
|
|
|
if sample_index >= len(selected_samples): |
|
return redirect(url_for('completed', filename=filename)) |
|
|
|
visualization_file = selected_samples[sample_index] |
|
visualization_path = None |
|
|
|
|
|
if method == "Chain-of-Table": |
|
visualization_dirs = VISUALIZATION_DIRS_CHAIN_OF_TABLE |
|
else: |
|
visualization_dirs = VISUALIZATION_DIRS_PLAN_OF_SQLS |
|
|
|
|
|
for category, dir_path in visualization_dirs.items(): |
|
if visualization_file in os.listdir(dir_path): |
|
visualization_path = f"{category}/{visualization_file}" |
|
break |
|
|
|
if not visualization_path: |
|
return "Visualization file not found", 404 |
|
|
|
statement = "Please make a decision to Accept/Reject the AI prediction based on the explanation." |
|
return render_template('experiment.html', |
|
sample_id=sample_index, |
|
statement=statement, |
|
visualization=visualization_path, |
|
username=username, |
|
seed=seed, |
|
sample_index=sample_index, |
|
filename=filename) |
|
|
|
@app.route('/visualizations/<path:path>') |
|
def send_visualization(path): |
|
|
|
method = session.get('method') |
|
if method == "Chain-of-Table": |
|
visualization_dir = 'htmls_COT' |
|
else: |
|
visualization_dir = 'visualizations' |
|
|
|
|
|
return send_from_directory(visualization_dir, path) |
|
|
|
|
|
@app.route('/feedback', methods=['POST']) |
|
def feedback(): |
|
sample_id = request.form['sample_id'] |
|
feedback = request.form['feedback'] |
|
username = request.form['username'] |
|
seed = request.form['seed'] |
|
sample_index = int(request.form['sample_index']) |
|
filename = request.form['filename'] |
|
|
|
selected_samples = session.get('selected_samples', []) |
|
responses = session.get('responses', []) |
|
|
|
|
|
responses.append({ |
|
'sample_id': sample_id, |
|
'feedback': feedback |
|
}) |
|
session['responses'] = responses |
|
|
|
|
|
result_dir = 'human_study' |
|
os.makedirs(result_dir, exist_ok=True) |
|
|
|
|
|
filepath = os.path.join(result_dir, filename) |
|
if os.path.exists(filepath): |
|
with open(filepath, 'r') as f: |
|
data = json.load(f) |
|
else: |
|
data = {} |
|
|
|
|
|
data[sample_index] = { |
|
'Username': username, |
|
'Seed': seed, |
|
'Sample ID': sample_id, |
|
'Task': f"Please make a decision to Accept/Reject the AI prediction based on the explanation.", |
|
'User Feedback': feedback |
|
} |
|
|
|
|
|
with open(filepath, 'w') as f: |
|
json.dump(data, f, indent=4) |
|
|
|
next_sample_index = sample_index + 1 |
|
if next_sample_index >= len(selected_samples): |
|
return redirect(url_for('completed', filename=filename)) |
|
|
|
return redirect( |
|
url_for('experiment', username=username, sample_index=next_sample_index, seed=seed, filename=filename)) |
|
|
|
@app.route('/completed/<filename>') |
|
def completed(filename): |
|
|
|
responses = session.get('responses', []) |
|
|
|
|
|
method = session.get('method') |
|
if method == "Chain-of-Table": |
|
json_file = 'Tabular_LLMs_human_study_vis_6_COT.json' |
|
else: |
|
json_file = 'Tabular_LLMs_human_study_vis_6.json' |
|
|
|
|
|
with open(json_file, 'r') as f: |
|
ground_truth = json.load(f) |
|
|
|
|
|
correct_responses = 0 |
|
accept_count = 0 |
|
reject_count = 0 |
|
|
|
for response in responses: |
|
sample_id = response['sample_id'] |
|
feedback = response['feedback'] |
|
index = sample_id.split('-')[1].split('.')[0] |
|
|
|
|
|
if feedback.upper() == "TRUE": |
|
accept_count += 1 |
|
elif feedback.upper() == "FALSE": |
|
reject_count += 1 |
|
|
|
|
|
if method == "Chain-of-Table": |
|
ground_truth_key = f"COT_test-{index}.html" |
|
else: |
|
ground_truth_key = f"POS_test-{index}.html" |
|
|
|
|
|
if ground_truth_key in ground_truth and ground_truth[ground_truth_key]['answer'].upper() == feedback.upper(): |
|
correct_responses += 1 |
|
else: |
|
print(f"Missing or mismatched key: {ground_truth_key}") |
|
|
|
|
|
accuracy = (correct_responses / len(responses)) * 100 if responses else 0 |
|
accuracy = round(accuracy, 2) |
|
|
|
|
|
total_responses = len(responses) |
|
accept_percentage = (accept_count / total_responses) * 100 if total_responses else 0 |
|
reject_percentage = (reject_count / total_responses) * 100 if total_responses else 0 |
|
|
|
|
|
accept_percentage = round(accept_percentage, 2) |
|
reject_percentage = round(reject_percentage, 2) |
|
|
|
return render_template('completed.html', |
|
accuracy=accuracy, |
|
accept_percentage=accept_percentage, |
|
reject_percentage=reject_percentage) |
|
|
|
|
|
if __name__ == '__main__': |
|
app.run(debug=True, port=8080) |
|
|