Spaces:
Build error
Build error
Commit ·
4020dbd
1
Parent(s): 0882179
update
Browse files109 unmodified lines11011111211311411511611711811912012112212312412512612712812913013113213313413516 unmodified lines1521531541551561571581591601611621634 unmodified lines1681691701711721731741751761771781791801811821831841851861871881 unmodified line1901911921931941952 unmodified lines1981992002012022032042052062072082092102112122132 unmodified lines21621721821922022122222322422522622722822923023123241 unmodified lines274275276277278279280109 unmodified linesreturn "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)# Load selected samples from the JSON filewith open(f'session_data/{filename}', 'r') as f:selected_samples = json.load(f)# Read the method from the filemethod_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", 500if 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 linesstatement = "Please make a decision to Accept/Reject the AI prediction based on the explanation."# Serve the file directly from the directoryreturn send_from_directory(directory=os.path.join(os.getcwd(), vis_dir, category), path=visualization_file)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 linessample_index = int(request.form['sample_index'])filename = request.form['filename']# Load selected samples from the JSON filewith open(f'session_data/{filename}', 'r') as f:selected_samples = json.load(f)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:1 unmodified lineelse:data = {}data[sample_index] = {'Username': username,'Seed': seed,2 unmodified lines'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", 5002 unmodified linesdef completed(filename):try:responses = session.get('responses', [])username = session.get('username')# Read the method from the filemethod_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 useelse:logger.error("Method file not found.")return "Method file not found", 500if method == "Chain-of-Table":json_file = 'Tabular_LLMs_human_study_vis_6_COT.json'41 unmodified linesaccept_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__":109 unmodified lines11011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214316 unmodified lines1601611621631641651661671681691701711721731741751761771781794 unmodified lines1841851861871881891901911921931941951961971981992002012022032041 unmodified line2062072082092102112122 unmodified lines2152162172182192202212222232242252262272282292302 unmodified lines23323423523623723823941 unmodified lines281282283284285286287109 unmodified linesreturn "An error occurred", 500return render_template('index.html')@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', [])if request.method == 'POST':# Process feedbackfeedback = request.form.get('feedback')if not feedback:return "Feedback is required", 400responses = session.get('responses', [])responses.append({'sample_id': sample_index,'feedback': feedback})session['responses'] = responsesnext_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))# Render the experiment pagemethod = session.get('method')if sample_index >= len(selected_samples):return redirect(url_for('completed', filename=filename))visualization_file = selected_samples[sample_index]16 unmodified linesstatement = "Please make a decision to Accept/Reject the AI prediction based on the explanation."# Render the template with the correct path for the iframereturn 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 linessample_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:1 unmodified lineelse:data = {}# Update data with the current feedbackdata[sample_index] = {'Username': username,'Seed': seed,2 unmodified lines'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))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', [])method = session.get('method')if method == "Chain-of-Table":json_file = 'Tabular_LLMs_human_study_vis_6_COT.json'41 unmodified linesaccept_percentage=accept_percentage,reject_percentage=reject_percentage)except Exception as e:logger.exception(f"An error occurred in the completed route: {e}")return "An error occurred", 500if __name__ == "__main__":
app.py+41-34