Commit
·
de7b716
1
Parent(s):
de400cc
update
Browse files- app.py +38 -36
- templates/experiment.html +2 -2
app.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
from flask import Flask, render_template, request, redirect, url_for, send_from_directory, session
|
| 2 |
import json
|
| 3 |
import random
|
|
@@ -48,12 +49,15 @@ def get_method_dir(method):
|
|
| 48 |
|
| 49 |
METHODS = ["No-XAI", "Dater", "Chain-of-Table", "Plan-of-SQLs"]
|
| 50 |
|
|
|
|
|
|
|
| 51 |
|
| 52 |
-
def save_session_data(
|
| 53 |
try:
|
|
|
|
| 54 |
seed = data.get('seed', 'unknown')
|
| 55 |
start_time = data.get('start_time', datetime.now().isoformat())
|
| 56 |
-
file_name = f'{username}_seed{seed}_{start_time}_session.json'
|
| 57 |
file_name = "".join(c for c in file_name if c.isalnum() or c in ['_', '-', '.'])
|
| 58 |
|
| 59 |
json_data = json.dumps(data, indent=4)
|
|
@@ -65,10 +69,9 @@ def save_session_data(username, data, is_update=False):
|
|
| 65 |
repo_path = "session_data_foward_simulation"
|
| 66 |
|
| 67 |
if is_update:
|
| 68 |
-
# If updating, we need to delete the old file first
|
| 69 |
existing_files = api.list_repo_files(repo_id="luulinh90s/Tabular-LLM-Study-Data", repo_type="space")
|
| 70 |
existing_file = next(
|
| 71 |
-
(f for f in existing_files if f.startswith(f'{repo_path}/{username}_seed{seed}_{start_time}')), None)
|
| 72 |
if existing_file:
|
| 73 |
api.delete_file(repo_id="luulinh90s/Tabular-LLM-Study-Data", repo_type="space",
|
| 74 |
path_in_repo=existing_file)
|
|
@@ -81,29 +84,27 @@ def save_session_data(username, data, is_update=False):
|
|
| 81 |
)
|
| 82 |
os.remove(temp_file_path)
|
| 83 |
logger.info(
|
| 84 |
-
f"Session data {'updated' if is_update else 'saved'} for
|
| 85 |
except Exception as e:
|
| 86 |
-
logger.exception(f"Error {'updating' if is_update else 'saving'} session data for
|
| 87 |
|
| 88 |
-
|
| 89 |
-
def load_session_data(username):
|
| 90 |
try:
|
| 91 |
api = HfApi()
|
| 92 |
files = api.list_repo_files(repo_id="luulinh90s/Tabular-LLM-Study-Data", repo_type="space")
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
logger.warning(f"No session data found for user {username}")
|
| 97 |
return None
|
| 98 |
-
latest_file = sorted(
|
| 99 |
file_path = hf_hub_download(repo_id="luulinh90s/Tabular-LLM-Study-Data", repo_type="space",
|
| 100 |
filename=latest_file)
|
| 101 |
with open(file_path, 'r') as f:
|
| 102 |
data = json.load(f)
|
| 103 |
-
logger.info(f"Session data loaded for
|
| 104 |
return data
|
| 105 |
except Exception as e:
|
| 106 |
-
logger.exception(f"Error loading session data for
|
| 107 |
return None
|
| 108 |
|
| 109 |
def load_samples():
|
|
@@ -151,6 +152,7 @@ def index():
|
|
| 151 |
if len(selected_samples) == 0:
|
| 152 |
return "No common samples were found", 500
|
| 153 |
start_time = datetime.now().isoformat()
|
|
|
|
| 154 |
session_data = {
|
| 155 |
'username': username,
|
| 156 |
'seed': str(seed), # Store as string
|
|
@@ -158,19 +160,20 @@ def index():
|
|
| 158 |
'selected_samples': selected_samples,
|
| 159 |
'current_index': 0,
|
| 160 |
'responses': [],
|
| 161 |
-
'start_time': start_time
|
|
|
|
| 162 |
}
|
| 163 |
-
save_session_data(
|
| 164 |
-
return redirect(url_for('experiment',
|
| 165 |
except Exception as e:
|
| 166 |
logger.exception(f"Error in index route: {e}")
|
| 167 |
return "An error occurred", 500
|
| 168 |
return render_template('index.html')
|
| 169 |
|
| 170 |
-
@app.route('/experiment/<
|
| 171 |
-
def experiment(
|
| 172 |
try:
|
| 173 |
-
session_data = load_session_data(
|
| 174 |
if not session_data:
|
| 175 |
return redirect(url_for('index'))
|
| 176 |
|
|
@@ -179,7 +182,7 @@ def experiment(username):
|
|
| 179 |
current_index = session_data['current_index']
|
| 180 |
|
| 181 |
if current_index >= len(selected_samples):
|
| 182 |
-
return redirect(url_for('completed',
|
| 183 |
|
| 184 |
sample = selected_samples[current_index]
|
| 185 |
visualization_dir = VISUALIZATION_DIRS[method]
|
|
@@ -195,22 +198,21 @@ Will it predict the statement as TRUE or FALSE?
|
|
| 195 |
sample_id=current_index,
|
| 196 |
statement=statement,
|
| 197 |
visualization=url_for('send_visualization', filename=visualization_path),
|
| 198 |
-
|
| 199 |
method=method)
|
| 200 |
except Exception as e:
|
| 201 |
logger.exception(f"An error occurred in the experiment route: {e}")
|
| 202 |
return "An error occurred", 500
|
| 203 |
|
| 204 |
-
|
| 205 |
@app.route('/feedback', methods=['POST'])
|
| 206 |
def feedback():
|
| 207 |
try:
|
| 208 |
-
|
| 209 |
prediction = request.form['prediction']
|
| 210 |
|
| 211 |
-
session_data = load_session_data(
|
| 212 |
if not session_data:
|
| 213 |
-
logger.error(f"No session data found for
|
| 214 |
return redirect(url_for('index'))
|
| 215 |
|
| 216 |
session_data['responses'].append({
|
|
@@ -219,23 +221,23 @@ def feedback():
|
|
| 219 |
})
|
| 220 |
|
| 221 |
session_data['current_index'] += 1
|
| 222 |
-
save_session_data(
|
| 223 |
-
logger.info(f"Prediction saved for
|
| 224 |
|
| 225 |
if session_data['current_index'] >= len(session_data['selected_samples']):
|
| 226 |
-
return redirect(url_for('completed',
|
| 227 |
|
| 228 |
-
return redirect(url_for('experiment',
|
| 229 |
except Exception as e:
|
| 230 |
logger.exception(f"Error in feedback route: {e}")
|
| 231 |
return "An error occurred", 500
|
| 232 |
|
| 233 |
-
@app.route('/completed/<
|
| 234 |
-
def completed(
|
| 235 |
try:
|
| 236 |
-
session_data = load_session_data(
|
| 237 |
if not session_data:
|
| 238 |
-
logger.error(f"No session data found for
|
| 239 |
return redirect(url_for('index'))
|
| 240 |
|
| 241 |
session_data['end_time'] = datetime.now().isoformat()
|
|
@@ -294,7 +296,7 @@ def completed(username):
|
|
| 294 |
session_data['true_percentage'] = true_percentage
|
| 295 |
session_data['false_percentage'] = false_percentage
|
| 296 |
|
| 297 |
-
save_session_data(
|
| 298 |
|
| 299 |
return render_template('completed.html',
|
| 300 |
accuracy=accuracy,
|
|
|
|
| 1 |
+
import uuid
|
| 2 |
from flask import Flask, render_template, request, redirect, url_for, send_from_directory, session
|
| 3 |
import json
|
| 4 |
import random
|
|
|
|
| 49 |
|
| 50 |
METHODS = ["No-XAI", "Dater", "Chain-of-Table", "Plan-of-SQLs"]
|
| 51 |
|
| 52 |
+
def generate_session_id():
|
| 53 |
+
return str(uuid.uuid4())
|
| 54 |
|
| 55 |
+
def save_session_data(session_id, data, is_update=False):
|
| 56 |
try:
|
| 57 |
+
username = data.get('username', 'unknown')
|
| 58 |
seed = data.get('seed', 'unknown')
|
| 59 |
start_time = data.get('start_time', datetime.now().isoformat())
|
| 60 |
+
file_name = f'{username}_seed{seed}_{start_time}_{session_id}_session.json'
|
| 61 |
file_name = "".join(c for c in file_name if c.isalnum() or c in ['_', '-', '.'])
|
| 62 |
|
| 63 |
json_data = json.dumps(data, indent=4)
|
|
|
|
| 69 |
repo_path = "session_data_foward_simulation"
|
| 70 |
|
| 71 |
if is_update:
|
|
|
|
| 72 |
existing_files = api.list_repo_files(repo_id="luulinh90s/Tabular-LLM-Study-Data", repo_type="space")
|
| 73 |
existing_file = next(
|
| 74 |
+
(f for f in existing_files if f.startswith(f'{repo_path}/{username}_seed{seed}_{start_time}_{session_id}')), None)
|
| 75 |
if existing_file:
|
| 76 |
api.delete_file(repo_id="luulinh90s/Tabular-LLM-Study-Data", repo_type="space",
|
| 77 |
path_in_repo=existing_file)
|
|
|
|
| 84 |
)
|
| 85 |
os.remove(temp_file_path)
|
| 86 |
logger.info(
|
| 87 |
+
f"Session data {'updated' if is_update else 'saved'} for session {session_id} in Hugging Face Data Space")
|
| 88 |
except Exception as e:
|
| 89 |
+
logger.exception(f"Error {'updating' if is_update else 'saving'} session data for session {session_id}: {e}")
|
| 90 |
|
| 91 |
+
def load_session_data(session_id):
|
|
|
|
| 92 |
try:
|
| 93 |
api = HfApi()
|
| 94 |
files = api.list_repo_files(repo_id="luulinh90s/Tabular-LLM-Study-Data", repo_type="space")
|
| 95 |
+
session_files = [f for f in files if f.endswith(f'{session_id}_session.json')]
|
| 96 |
+
if not session_files:
|
| 97 |
+
logger.warning(f"No session data found for session {session_id}")
|
|
|
|
| 98 |
return None
|
| 99 |
+
latest_file = sorted(session_files, reverse=True)[0]
|
| 100 |
file_path = hf_hub_download(repo_id="luulinh90s/Tabular-LLM-Study-Data", repo_type="space",
|
| 101 |
filename=latest_file)
|
| 102 |
with open(file_path, 'r') as f:
|
| 103 |
data = json.load(f)
|
| 104 |
+
logger.info(f"Session data loaded for session {session_id} from Hugging Face Data Space")
|
| 105 |
return data
|
| 106 |
except Exception as e:
|
| 107 |
+
logger.exception(f"Error loading session data for session {session_id}: {e}")
|
| 108 |
return None
|
| 109 |
|
| 110 |
def load_samples():
|
|
|
|
| 152 |
if len(selected_samples) == 0:
|
| 153 |
return "No common samples were found", 500
|
| 154 |
start_time = datetime.now().isoformat()
|
| 155 |
+
session_id = generate_session_id()
|
| 156 |
session_data = {
|
| 157 |
'username': username,
|
| 158 |
'seed': str(seed), # Store as string
|
|
|
|
| 160 |
'selected_samples': selected_samples,
|
| 161 |
'current_index': 0,
|
| 162 |
'responses': [],
|
| 163 |
+
'start_time': start_time,
|
| 164 |
+
'session_id': session_id
|
| 165 |
}
|
| 166 |
+
save_session_data(session_id, session_data)
|
| 167 |
+
return redirect(url_for('experiment', session_id=session_id))
|
| 168 |
except Exception as e:
|
| 169 |
logger.exception(f"Error in index route: {e}")
|
| 170 |
return "An error occurred", 500
|
| 171 |
return render_template('index.html')
|
| 172 |
|
| 173 |
+
@app.route('/experiment/<session_id>', methods=['GET', 'POST'])
|
| 174 |
+
def experiment(session_id):
|
| 175 |
try:
|
| 176 |
+
session_data = load_session_data(session_id)
|
| 177 |
if not session_data:
|
| 178 |
return redirect(url_for('index'))
|
| 179 |
|
|
|
|
| 182 |
current_index = session_data['current_index']
|
| 183 |
|
| 184 |
if current_index >= len(selected_samples):
|
| 185 |
+
return redirect(url_for('completed', session_id=session_id))
|
| 186 |
|
| 187 |
sample = selected_samples[current_index]
|
| 188 |
visualization_dir = VISUALIZATION_DIRS[method]
|
|
|
|
| 198 |
sample_id=current_index,
|
| 199 |
statement=statement,
|
| 200 |
visualization=url_for('send_visualization', filename=visualization_path),
|
| 201 |
+
session_id=session_id,
|
| 202 |
method=method)
|
| 203 |
except Exception as e:
|
| 204 |
logger.exception(f"An error occurred in the experiment route: {e}")
|
| 205 |
return "An error occurred", 500
|
| 206 |
|
|
|
|
| 207 |
@app.route('/feedback', methods=['POST'])
|
| 208 |
def feedback():
|
| 209 |
try:
|
| 210 |
+
session_id = request.form['session_id']
|
| 211 |
prediction = request.form['prediction']
|
| 212 |
|
| 213 |
+
session_data = load_session_data(session_id)
|
| 214 |
if not session_data:
|
| 215 |
+
logger.error(f"No session data found for session: {session_id}")
|
| 216 |
return redirect(url_for('index'))
|
| 217 |
|
| 218 |
session_data['responses'].append({
|
|
|
|
| 221 |
})
|
| 222 |
|
| 223 |
session_data['current_index'] += 1
|
| 224 |
+
save_session_data(session_id, session_data)
|
| 225 |
+
logger.info(f"Prediction saved for session {session_id}, sample {session_data['current_index'] - 1}")
|
| 226 |
|
| 227 |
if session_data['current_index'] >= len(session_data['selected_samples']):
|
| 228 |
+
return redirect(url_for('completed', session_id=session_id))
|
| 229 |
|
| 230 |
+
return redirect(url_for('experiment', session_id=session_id))
|
| 231 |
except Exception as e:
|
| 232 |
logger.exception(f"Error in feedback route: {e}")
|
| 233 |
return "An error occurred", 500
|
| 234 |
|
| 235 |
+
@app.route('/completed/<session_id>')
|
| 236 |
+
def completed(session_id):
|
| 237 |
try:
|
| 238 |
+
session_data = load_session_data(session_id)
|
| 239 |
if not session_data:
|
| 240 |
+
logger.error(f"No session data found for session: {session_id}")
|
| 241 |
return redirect(url_for('index'))
|
| 242 |
|
| 243 |
session_data['end_time'] = datetime.now().isoformat()
|
|
|
|
| 296 |
session_data['true_percentage'] = true_percentage
|
| 297 |
session_data['false_percentage'] = false_percentage
|
| 298 |
|
| 299 |
+
save_session_data(session_id, session_data, is_update=True) # Update the existing file
|
| 300 |
|
| 301 |
return render_template('completed.html',
|
| 302 |
accuracy=accuracy,
|
templates/experiment.html
CHANGED
|
@@ -83,11 +83,11 @@
|
|
| 83 |
</div>
|
| 84 |
<div class="buttons">
|
| 85 |
<form action="{{ url_for('feedback') }}" method="post">
|
| 86 |
-
<input type="hidden" name="
|
| 87 |
<button type="submit" name="prediction" value="TRUE">Predict TRUE</button>
|
| 88 |
</form>
|
| 89 |
<form action="{{ url_for('feedback') }}" method="post">
|
| 90 |
-
<input type="hidden" name="
|
| 91 |
<button type="submit" name="prediction" value="FALSE" class="reject">Predict FALSE</button>
|
| 92 |
</form>
|
| 93 |
</div>
|
|
|
|
| 83 |
</div>
|
| 84 |
<div class="buttons">
|
| 85 |
<form action="{{ url_for('feedback') }}" method="post">
|
| 86 |
+
<input type="hidden" name="session_id" value="{{ session_id }}">
|
| 87 |
<button type="submit" name="prediction" value="TRUE">Predict TRUE</button>
|
| 88 |
</form>
|
| 89 |
<form action="{{ url_for('feedback') }}" method="post">
|
| 90 |
+
<input type="hidden" name="session_id" value="{{ session_id }}">
|
| 91 |
<button type="submit" name="prediction" value="FALSE" class="reject">Predict FALSE</button>
|
| 92 |
</form>
|
| 93 |
</div>
|