Spaces:
Build error
Build error
Commit ·
4020dbd
1
Parent(s): 0882179
update
Browse files109 unmodified lines11011111211311311411511611711811811912011912012112212312412212312412512612712812912512612712812913013113213313413513613713813914013214114214316 unmodified lines1601611621551561631641651661671681691701711721731741751761771781794 unmodified lines1841851861711721731741871881891901911921931941951961971981992002012022032041 unmodified line2062072082092102112122 unmodified lines2152162172182192202212042052222232242252262102272282292302 unmodified lines23323423521922022122222322422522622722822923623723823941 unmodified lines281282283277284285286287109 unmodified lines return "An error occurred", 500 return render_template('index.html')
@app.route('/experiment/<username>/<sample_index>/<seed>/<filename>', methods=['GET'])@app.route('/experiment/<username>/<sample_index>/<seed>/<filename>', methods=['GET', 'POST'])def experiment(username, sample_index, seed, filename): try: sample_index = int(sample_index) selected_samples = session.get('selected_samples', [])
# Load selected samples from the JSON file with open(f'session_data/{filename}', 'r') as f: selected_samples = json.load(f) if request.method == 'POST': # Process feedback feedback = request.form.get('feedback') if not feedback: return "Feedback is required", 400
# Read the method from the file method_file_path = f'session_data/method_{username}.txt' if os.path.exists(method_file_path): with open(method_file_path, 'r') as f: method = f.read().strip() else: logger.error(f"Method file not found for user {username}.") return "Method file not found", 500 responses = session.get('responses', []) responses.append({ 'sample_id': sample_index, 'feedback': feedback }) session['responses'] = responses
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))
# Render the experiment page method = session.get('method') if sample_index >= len(selected_samples): logger.error(f"Sample index {sample_index} exceeds the number of selected samples {len(selected_samples)}.") return redirect(url_for('completed', filename=filename))
visualization_file = selected_samples[sample_index]16 unmodified lines
statement = "Please make a decision to Accept/Reject the AI prediction based on the explanation."
# Serve the file directly from the directory return send_from_directory(directory=os.path.join(os.getcwd(), vis_dir, category), path=visualization_file) # Render the template with the correct path for the iframe return render_template('experiment.html', sample_id=sample_index, statement=statement, visualization=url_for('send_visualization', path=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('/feedback', methods=['POST'])def feedback(): try:4 unmodified lines sample_index = int(request.form['sample_index']) filename = request.form['filename']
# Load selected samples from the JSON file with open(f'session_data/{filename}', 'r') as f: selected_samples = json.load(f)
selected_samples = session.get('selected_samples', []) responses = session.get('responses', [])
# Store the feedback responses.append({ 'sample_id': sample_id, 'feedback': feedback }) session['responses'] = responses
# Create the result directory if it doesn't exist result_dir = 'human_study' os.makedirs(result_dir, exist_ok=True)
# Load existing data if the JSON file exists filepath = os.path.join(result_dir, filename) if os.path.exists(filepath): with open(filepath, 'r') as f:1 unmodified line else: data = {}
# Update data with the current feedback data[sample_index] = { 'Username': username, 'Seed': seed,2 unmodified lines 'User Feedback': feedback }
# Save updated data to the file 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 + 1 if next_sample_index >= len(selected_samples): return redirect(url_for('completed', filename=filename))
return redirect( return redirect(url_for('experiment', username=username, sample_index=next_sample_index, seed=seed, filename=filename)) 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", 5002 unmodified linesdef completed(filename): try: responses = session.get('responses', []) username = session.get('username')
# Read the method from the file method_file_path = f'session_data/method_{username}.txt' if os.path.exists(method_file_path): with open(method_file_path, 'r') as f: method = f.read().strip() os.remove(method_file_path) # Remove the method file after use else: logger.error("Method file not found.") return "Method file not found", 500 method = session.get('method')
if method == "Chain-of-Table": json_file = 'Tabular_LLMs_human_study_vis_6_COT.json'41 unmodified lines accept_percentage=accept_percentage, reject_percentage=reject_percentage) except Exception as e: logger.exception(f"Error in completed route: {e}") logger.exception(f"An error occurred in the completed route: {e}") return "An error occurred", 500
if __name__ == "__main__":app.py+41-34