Spaces:
Build error
Build error
Commit ·
3909e19
1
Parent(s): 479b115
update
Browse files3 unmodified lines45678918 unmodified lines2829303132331 unmodified line35363738394041424344454647484950512 unmodified lines545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472483 unmodified linesimport osimport stringfrom flask_session import Sessionapp = Flask(__name__)app.config['SECRET_KEY'] = 'supersecretkey' # Change this to a random secret key18 unmodified lines# Load all sample files from the directories based on the selected methoddef load_samples(method):if method == "Chain-of-Table":visualization_dirs = VISUALIZATION_DIRS_CHAIN_OF_TABLEelse:1 unmodified linesamples = {"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# Randomly select balanced samplesdef 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_samplesdef generate_random_string(length=8):2 unmodified lines@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", 400seed = 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' # Append method to filenamesession['selected_samples'] = selected_samplessession['responses'] = [] # Initialize responses listsession['method'] = method # Store the selected methodreturn 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') # Retrieve the selected methodif sample_index >= len(selected_samples):return redirect(url_for('completed', filename=filename))visualization_file = selected_samples[sample_index]visualization_path = None# Determine the correct visualization directory based on the methodif method == "Chain-of-Table":visualization_dirs = VISUALIZATION_DIRS_CHAIN_OF_TABLEelse:visualization_dirs = VISUALIZATION_DIRS_PLAN_OF_SQLS# Find the correct visualization pathfor category, dir_path in visualization_dirs.items():if visualization_file in os.listdir(dir_path):visualization_path = f"{category}/{visualization_file}"breakif not visualization_path:return "Visualization file not found", 404statement = "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):# Determine which visualization folder to use based on the selected methodmethod = session.get('method')if method == "Chain-of-Table":visualization_dir = 'htmls_COT'else: # Default to Plan-of-SQLsvisualization_dir = 'visualizations'# Serve the file from the appropriate directoryreturn 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', [])# Store the feedbackresponses.append({'sample_id': sample_id,'feedback': feedback})session['responses'] = responses# Create the result directory if it doesn't existresult_dir = 'human_study'os.makedirs(result_dir, exist_ok=True)# Load existing data if the JSON file existsfilepath = os.path.join(result_dir, filename)if os.path.exists(filepath):with open(filepath, 'r') as f:data = json.load(f)else:data = {}# Update data with the current feedbackdata[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}# Save updated data to the filewith open(filepath, 'w') as f:json.dump(data, f, indent=4)next_sample_index = sample_index + 1if 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):# Load responses from the sessionresponses = session.get('responses', [])# Determine which JSON file to load based on the methodmethod = session.get('method')if method == "Chain-of-Table":json_file = 'Tabular_LLMs_human_study_vis_6_COT.json'else: # Default to Plan-of-SQLsjson_file = 'Tabular_LLMs_human_study_vis_6.json'# Load the ground truth data from the appropriate JSON filewith open(json_file, 'r') as f:ground_truth = json.load(f)# Initialize counterscorrect_responses = 0accept_count = 0reject_count = 0for response in responses:sample_id = response['sample_id']feedback = response['feedback']index = sample_id.split('-')[1].split('.')[0] # Extract index from filename# Count the feedbackif feedback.upper() == "TRUE":accept_count += 1elif feedback.upper() == "FALSE":reject_count += 1# Construct the ground truth keyif method == "Chain-of-Table":ground_truth_key = f"COT_test-{index}.html" # Adjust this based on your actual key format in the CoTable JSONelse:ground_truth_key = f"POS_test-{index}.html"# Check if the key exists in the ground truth dataif ground_truth_key in ground_truth and ground_truth[ground_truth_key]['answer'].upper() == feedback.upper():correct_responses += 1else:print(f"Missing or mismatched key: {ground_truth_key}")# Calculate accuracyaccuracy = (correct_responses / len(responses)) * 100 if responses else 0accuracy = round(accuracy, 2)# Calculate percentagestotal_responses = len(responses)accept_percentage = (accept_count / total_responses) * 100 if total_responses else 0reject_percentage = (reject_count / total_responses) * 100 if total_responses else 0# Round percentagesaccept_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)# change for running on HuggingFaceapp.run(debug=False, port=7860)3 unmodified lines4567891011121314151617181918 unmodified lines383940414243441 unmodified line46474849505152535455565758596061626364656667686970712 unmodified lines74757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752763 unmodified linesimport osimport stringfrom flask_session import Sessionimport logging# Set up logginglogging.basicConfig(level=logging.INFO,format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',handlers=[logging.FileHandler("app.log"),logging.StreamHandler()])logger = logging.getLogger(__name__)app = Flask(__name__)app.config['SECRET_KEY'] = 'supersecretkey' # Change this to a random secret key18 unmodified lines# Load all sample files from the directories based on the selected methoddef load_samples(method):logger.info(f"Loading samples for method: {method}")if method == "Chain-of-Table":visualization_dirs = VISUALIZATION_DIRS_CHAIN_OF_TABLEelse:1 unmodified linesamples = {"TP": [], "TN": [], "FP": [], "FN": []}for category, dir_path in visualization_dirs.items():try:for filename in os.listdir(dir_path):if filename.endswith(".html"):samples[category].append(filename)logger.info(f"Loaded {len(samples[category])} samples for category {category}")except Exception as e:logger.exception(f"Error loading samples from {dir_path}: {e}")return samples# Randomly select balanced samplesdef select_balanced_samples(samples):try:tp_fp_samples = random.sample(samples["TP"] + samples["FP"], 5)tn_fn_samples = random.sample(samples["TN"] + samples["FN"], 5)logger.info(f"Selected balanced samples: {len(tp_fp_samples + tn_fn_samples)}")return tp_fp_samples + tn_fn_samplesexcept Exception as e:logger.exception("Error selecting balanced samples")return []def generate_random_string(length=8):2 unmodified lines@app.route('/', methods=['GET', 'POST'])def index():logger.info("Rendering index page.")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:logger.error("Missing username, seed, or method.")return "Missing username, seed, or method", 400try: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'logger.info(f"Generated filename: {filename}")session['selected_samples'] = selected_samplessession['responses'] = [] # Initialize responses listsession['method'] = method # Store the selected methodreturn redirect(url_for('experiment', username=username, sample_index=0, seed=seed, filename=filename))except Exception as e:logger.exception(f"Error in index route: {e}")return "An error occurred", 500return render_template('index.html')@app.route('/experiment/<username>/<sample_index>/<seed>/<filename>', methods=['GET'])def experiment(username, sample_index, seed, filename):try:sample_index = int(sample_index)selected_samples = session.get('selected_samples', [])method = session.get('method') # Retrieve the selected methodif sample_index >= len(selected_samples):return redirect(url_for('completed', filename=filename))visualization_file = selected_samples[sample_index]visualization_path = None# Determine the correct visualization directory based on the methodif method == "Chain-of-Table":visualization_dirs = VISUALIZATION_DIRS_CHAIN_OF_TABLEelse:visualization_dirs = VISUALIZATION_DIRS_PLAN_OF_SQLS# Find the correct visualization pathfor category, dir_path in visualization_dirs.items():if visualization_file in os.listdir(dir_path):visualization_path = f"{category}/{visualization_file}"breakif not visualization_path:logger.error("Visualization file not found.")return "Visualization file not found", 404statement = "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)except Exception as e:logger.exception(f"An error occurred in the experiment route: {e}")return "An error occurred", 500@app.route('/visualizations/<path:path>')def send_visualization(path):try:method = session.get('method')if method == "Chain-of-Table":visualization_dir = 'htmls_COT'else: # Default to Plan-of-SQLsvisualization_dir = 'visualizations'return send_from_directory(visualization_dir, path)except Exception as e:logger.exception(f"Error sending visualization: {e}")return "An error occurred", 500@app.route('/feedback', methods=['POST'])def feedback():try: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'] = responsesresult_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)logger.info(f"Feedback saved for sample {sample_id}")next_sample_index = sample_index + 1if 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))except Exception as e:logger.exception(f"Error in feedback route: {e}")return "An error occurred", 500@app.route('/completed/<filename>')def completed(filename):try:responses = session.get('responses', [])method = session.get('method')if method == "Chain-of-Table":json_file = 'Tabular_LLMs_human_study_vis_6_COT.json'else: # Default to Plan-of-SQLsjson_file = 'Tabular_LLMs_human_study_vis_6.json'with open(json_file, 'r') as f:ground_truth = json.load(f)correct_responses = 0accept_count = 0reject_count = 0for response in responses:sample_id = response['sample_id']feedback = response['feedback']index = sample_id.split('-')[1].split('.')[0] # Extract index from filenameif feedback.upper() == "TRUE":accept_count += 1elif feedback.upper() == "FALSE":reject_count += 1if 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 += 1else:logger.warning(f"Missing or mismatched key: {ground_truth_key}")accuracy = (correct_responses / len(responses)) * 100 if responses else 0accuracy = round(accuracy, 2)accept_percentage = (accept_count / len(responses)) * 100 if len(responses) else 0reject_percentage = (reject_count / len(responses)) * 100 if len(responses) else 0accept_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)except Exception as e:logger.exception(f"Error in completed route: {e}")return "An error occurred", 500if __name__ == '__main__':app.run(debug=False, port=7860)
app.py+182-154