Spaces:
Build error
Build error
Commit ·
de7b716
1
Parent(s): de400cc
update
Browse filesFiles changed (2)
- app.py+38-36
- templates/experiment.html+2-2
123444 unmodified lines49505152535452555657585956606162635 unmodified lines69707168727371747576776 unmodified lines8485868487888689908889919293949394959695969798989910010110210310310410510610610710810911041 unmodified lines1521531541551561571581 unmodified line1601611621611631641651631641661671681691701711721701711731741751731761771781792 unmodified lines1821831841821851861871889 unmodified lines1981992001982012022032042052062042072082092082102112122112132142132152162172182 unmodified lines22122222322222322422522622722622822922823023123223323423323423523623723623823923824024124224352 unmodified lines296297298297299300301302import uuidfrom flask import Flask, render_template, request, redirect, url_for, send_from_directory, sessionimport jsonimport random44 unmodified lines
METHODS = ["No-XAI", "Dater", "Chain-of-Table", "Plan-of-SQLs"]
def generate_session_id(): return str(uuid.uuid4())
def save_session_data(username, data, is_update=False):def save_session_data(session_id, data, is_update=False): try: username = data.get('username', 'unknown') seed = data.get('seed', 'unknown') start_time = data.get('start_time', datetime.now().isoformat()) file_name = f'{username}_seed{seed}_{start_time}_session.json' file_name = f'{username}_seed{seed}_{start_time}_{session_id}_session.json' file_name = "".join(c for c in file_name if c.isalnum() or c in ['_', '-', '.'])
json_data = json.dumps(data, indent=4)5 unmodified lines repo_path = "session_data_foward_simulation"
if is_update: # If updating, we need to delete the old file first existing_files = api.list_repo_files(repo_id="luulinh90s/Tabular-LLM-Study-Data", repo_type="space") existing_file = next( (f for f in existing_files if f.startswith(f'{repo_path}/{username}_seed{seed}_{start_time}')), None) (f for f in existing_files if f.startswith(f'{repo_path}/{username}_seed{seed}_{start_time}_{session_id}')), None) if existing_file: api.delete_file(repo_id="luulinh90s/Tabular-LLM-Study-Data", repo_type="space", path_in_repo=existing_file)6 unmodified lines ) os.remove(temp_file_path) logger.info( f"Session data {'updated' if is_update else 'saved'} for user {username} in Hugging Face Data Space") f"Session data {'updated' if is_update else 'saved'} for session {session_id} in Hugging Face Data Space") except Exception as e: logger.exception(f"Error {'updating' if is_update else 'saving'} session data for user {username}: {e}") logger.exception(f"Error {'updating' if is_update else 'saving'} session data for session {session_id}: {e}")
def load_session_data(username):def load_session_data(session_id): try: api = HfApi() files = api.list_repo_files(repo_id="luulinh90s/Tabular-LLM-Study-Data", repo_type="space") user_files = [f for f in files if f.startswith(f'session_data_foward_simulation/{username}_') and f.endswith('_session.json')] if not user_files: logger.warning(f"No session data found for user {username}") session_files = [f for f in files if f.endswith(f'{session_id}_session.json')] if not session_files: logger.warning(f"No session data found for session {session_id}") return None latest_file = sorted(user_files, reverse=True)[0] latest_file = sorted(session_files, reverse=True)[0] file_path = hf_hub_download(repo_id="luulinh90s/Tabular-LLM-Study-Data", repo_type="space", filename=latest_file) with open(file_path, 'r') as f: data = json.load(f) logger.info(f"Session data loaded for user {username} from Hugging Face Data Space") logger.info(f"Session data loaded for session {session_id} from Hugging Face Data Space") return data except Exception as e: logger.exception(f"Error loading session data for user {username}: {e}") logger.exception(f"Error loading session data for session {session_id}: {e}") return None
def load_samples():41 unmodified lines if len(selected_samples) == 0: return "No common samples were found", 500 start_time = datetime.now().isoformat() session_id = generate_session_id() session_data = { 'username': username, 'seed': str(seed), # Store as string1 unmodified line 'selected_samples': selected_samples, 'current_index': 0, 'responses': [], 'start_time': start_time 'start_time': start_time, 'session_id': session_id } save_session_data(username, session_data) return redirect(url_for('experiment', username=username)) save_session_data(session_id, session_data) return redirect(url_for('experiment', session_id=session_id)) except Exception as e: logger.exception(f"Error in index route: {e}") return "An error occurred", 500 return render_template('index.html')
@app.route('/experiment/<username>', methods=['GET', 'POST'])def experiment(username):@app.route('/experiment/<session_id>', methods=['GET', 'POST'])def experiment(session_id): try: session_data = load_session_data(username) session_data = load_session_data(session_id) if not session_data: return redirect(url_for('index'))
2 unmodified lines current_index = session_data['current_index']
if current_index >= len(selected_samples): return redirect(url_for('completed', username=username)) return redirect(url_for('completed', session_id=session_id))
sample = selected_samples[current_index] visualization_dir = VISUALIZATION_DIRS[method]9 unmodified lines sample_id=current_index, statement=statement, visualization=url_for('send_visualization', filename=visualization_path), username=username, session_id=session_id, method=method) 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: username = request.form['username'] session_id = request.form['session_id'] prediction = request.form['prediction']
session_data = load_session_data(username) session_data = load_session_data(session_id) if not session_data: logger.error(f"No session data found for user: {username}") logger.error(f"No session data found for session: {session_id}") return redirect(url_for('index'))
session_data['responses'].append({2 unmodified lines })
session_data['current_index'] += 1 save_session_data(username, session_data) logger.info(f"Prediction saved for user {username}, sample {session_data['current_index'] - 1}") save_session_data(session_id, session_data) logger.info(f"Prediction saved for session {session_id}, sample {session_data['current_index'] - 1}")
if session_data['current_index'] >= len(session_data['selected_samples']): return redirect(url_for('completed', username=username)) return redirect(url_for('completed', session_id=session_id))
return redirect(url_for('experiment', username=username)) return redirect(url_for('experiment', session_id=session_id)) except Exception as e: logger.exception(f"Error in feedback route: {e}") return "An error occurred", 500
@app.route('/completed/<username>')def completed(username):@app.route('/completed/<session_id>')def completed(session_id): try: session_data = load_session_data(username) session_data = load_session_data(session_id) if not session_data: logger.error(f"No session data found for user: {username}") logger.error(f"No session data found for session: {session_id}") return redirect(url_for('index'))
session_data['end_time'] = datetime.now().isoformat()52 unmodified lines session_data['true_percentage'] = true_percentage session_data['false_percentage'] = false_percentage
save_session_data(username, session_data, is_update=True) # Update the existing file save_session_data(session_id, session_data, is_update=True) # Update the existing file
return render_template('completed.html', accuracy=accuracy,82 unmodified lines8384858686878889909091929382 unmodified lines </div> <div class="buttons"> <form action="{{ url_for('feedback') }}" method="post"> <input type="hidden" name="username" value="{{ username }}"> <input type="hidden" name="session_id" value="{{ session_id }}"> <button type="submit" name="prediction" value="TRUE">Predict TRUE</button> </form> <form action="{{ url_for('feedback') }}" method="post"> <input type="hidden" name="username" value="{{ username }}"> <input type="hidden" name="session_id" value="{{ session_id }}"> <button type="submit" name="prediction" value="FALSE" class="reject">Predict FALSE</button> </form> </div>app.py+38-36
templates/experiment.html+2-2