luulinh90s commited on
Commit
00c0d2b
1 Parent(s): 4600a8b
Files changed (1) hide show
  1. app.py +30 -15
app.py CHANGED
@@ -1,5 +1,5 @@
1
  import uuid
2
- from flask import Flask, render_template, request, redirect, url_for, send_from_directory, session
3
  import json
4
  import random
5
  import os
@@ -27,6 +27,10 @@ else:
27
  app = Flask(__name__)
28
  app.config['SECRET_KEY'] = 'supersecretkey' # Change this to a random secret key
29
 
 
 
 
 
30
  # Directories for visualizations
31
  VISUALIZATION_DIRS = {
32
  "No-XAI": "htmls_NO_XAI_mod",
@@ -53,6 +57,19 @@ def generate_session_id():
53
  return str(uuid.uuid4())
54
 
55
  def save_session_data(session_id, data):
 
 
 
 
 
 
 
 
 
 
 
 
 
56
  try:
57
  username = data.get('username', 'unknown')
58
  seed = data.get('seed', 'unknown')
@@ -128,7 +145,6 @@ def introduction():
128
  def attribution():
129
  return render_template('attribution.html')
130
 
131
-
132
  @app.route('/index', methods=['GET', 'POST'])
133
  def index():
134
  if request.method == 'POST':
@@ -156,8 +172,8 @@ def index():
156
  'start_time': start_time,
157
  'session_id': session_id
158
  }
159
- session['data'] = session_data # Store data in session
160
- logger.info(f"Session data stored for user {username}, method {method}")
161
 
162
  # Redirect based on the selected method
163
  if method == 'No-XAI':
@@ -171,7 +187,7 @@ def index():
171
 
172
  @app.route('/explanation/<session_id>')
173
  def explanation(session_id):
174
- session_data = session.get('data')
175
  if not session_data:
176
  logger.error(f"No session data found for session ID: {session_id}")
177
  return redirect(url_for('index'))
@@ -194,7 +210,7 @@ def explanation(session_id):
194
  @app.route('/experiment/<session_id>', methods=['GET', 'POST'])
195
  def experiment(session_id):
196
  try:
197
- session_data = session.get('data')
198
  if not session_data:
199
  return redirect(url_for('index'))
200
 
@@ -230,13 +246,13 @@ def subjective(session_id):
230
  if request.method == 'POST':
231
  understanding = request.form.get('understanding')
232
 
233
- session_data = session.get('data')
234
  if not session_data:
235
  logger.error(f"No session data found for session: {session_id}")
236
  return redirect(url_for('index'))
237
 
238
  session_data['subjective_feedback'] = understanding
239
- session['data'] = session_data # Update session data
240
 
241
  return redirect(url_for('completed', session_id=session_id))
242
 
@@ -248,7 +264,7 @@ def feedback():
248
  session_id = request.form['session_id']
249
  prediction = request.form['prediction']
250
 
251
- session_data = session.get('data')
252
  if not session_data:
253
  logger.error(f"No session data found for session: {session_id}")
254
  return redirect(url_for('index'))
@@ -259,7 +275,7 @@ def feedback():
259
  })
260
 
261
  session_data['current_index'] += 1
262
- session['data'] = session_data # Update session data
263
  logger.info(f"Prediction saved for session {session_id}, sample {session_data['current_index'] - 1}")
264
 
265
  if session_data['current_index'] >= len(session_data['selected_samples']):
@@ -273,7 +289,7 @@ def feedback():
273
  @app.route('/completed/<session_id>')
274
  def completed(session_id):
275
  try:
276
- session_data = session.get('data')
277
  if not session_data:
278
  logger.error(f"No session data found for session: {session_id}")
279
  return redirect(url_for('index'))
@@ -338,10 +354,10 @@ def completed(session_id):
338
  session_data['false_percentage'] = false_percentage
339
 
340
  # Save all the data to Hugging Face at the end
341
- save_session_data(session_id, session_data)
342
 
343
- # Clear the session data
344
- session.pop('data', None)
345
 
346
  return render_template('completed.html',
347
  accuracy=accuracy,
@@ -371,6 +387,5 @@ def send_visualization(filename):
371
  def send_examples(filename):
372
  return send_from_directory('', filename)
373
 
374
-
375
  if __name__ == "__main__":
376
  app.run(host="0.0.0.0", port=7860, debug=True)
 
1
  import uuid
2
+ from flask import Flask, render_template, request, redirect, url_for, send_from_directory
3
  import json
4
  import random
5
  import os
 
27
  app = Flask(__name__)
28
  app.config['SECRET_KEY'] = 'supersecretkey' # Change this to a random secret key
29
 
30
+ # File-based session storage
31
+ SESSION_DIR = '/tmp/sessions'
32
+ os.makedirs(SESSION_DIR, exist_ok=True)
33
+
34
  # Directories for visualizations
35
  VISUALIZATION_DIRS = {
36
  "No-XAI": "htmls_NO_XAI_mod",
 
57
  return str(uuid.uuid4())
58
 
59
  def save_session_data(session_id, data):
60
+ file_path = os.path.join(SESSION_DIR, f'{session_id}.json')
61
+ with open(file_path, 'w') as f:
62
+ json.dump(data, f)
63
+ logger.info(f"Session data saved for session {session_id}")
64
+
65
+ def load_session_data(session_id):
66
+ file_path = os.path.join(SESSION_DIR, f'{session_id}.json')
67
+ if os.path.exists(file_path):
68
+ with open(file_path, 'r') as f:
69
+ return json.load(f)
70
+ return None
71
+
72
+ def save_session_data_to_hf(session_id, data):
73
  try:
74
  username = data.get('username', 'unknown')
75
  seed = data.get('seed', 'unknown')
 
145
  def attribution():
146
  return render_template('attribution.html')
147
 
 
148
  @app.route('/index', methods=['GET', 'POST'])
149
  def index():
150
  if request.method == 'POST':
 
172
  'start_time': start_time,
173
  'session_id': session_id
174
  }
175
+ save_session_data(session_id, session_data)
176
+ logger.info(f"Session data stored for user {username}, method {method}, session_id {session_id}")
177
 
178
  # Redirect based on the selected method
179
  if method == 'No-XAI':
 
187
 
188
  @app.route('/explanation/<session_id>')
189
  def explanation(session_id):
190
+ session_data = load_session_data(session_id)
191
  if not session_data:
192
  logger.error(f"No session data found for session ID: {session_id}")
193
  return redirect(url_for('index'))
 
210
  @app.route('/experiment/<session_id>', methods=['GET', 'POST'])
211
  def experiment(session_id):
212
  try:
213
+ session_data = load_session_data(session_id)
214
  if not session_data:
215
  return redirect(url_for('index'))
216
 
 
246
  if request.method == 'POST':
247
  understanding = request.form.get('understanding')
248
 
249
+ session_data = load_session_data(session_id)
250
  if not session_data:
251
  logger.error(f"No session data found for session: {session_id}")
252
  return redirect(url_for('index'))
253
 
254
  session_data['subjective_feedback'] = understanding
255
+ save_session_data(session_id, session_data)
256
 
257
  return redirect(url_for('completed', session_id=session_id))
258
 
 
264
  session_id = request.form['session_id']
265
  prediction = request.form['prediction']
266
 
267
+ session_data = load_session_data(session_id)
268
  if not session_data:
269
  logger.error(f"No session data found for session: {session_id}")
270
  return redirect(url_for('index'))
 
275
  })
276
 
277
  session_data['current_index'] += 1
278
+ save_session_data(session_id, session_data)
279
  logger.info(f"Prediction saved for session {session_id}, sample {session_data['current_index'] - 1}")
280
 
281
  if session_data['current_index'] >= len(session_data['selected_samples']):
 
289
  @app.route('/completed/<session_id>')
290
  def completed(session_id):
291
  try:
292
+ session_data = load_session_data(session_id)
293
  if not session_data:
294
  logger.error(f"No session data found for session: {session_id}")
295
  return redirect(url_for('index'))
 
354
  session_data['false_percentage'] = false_percentage
355
 
356
  # Save all the data to Hugging Face at the end
357
+ save_session_data_to_hf(session_id, session_data)
358
 
359
+ # Remove the local session data file
360
+ os.remove(os.path.join(SESSION_DIR, f'{session_id}.json'))
361
 
362
  return render_template('completed.html',
363
  accuracy=accuracy,
 
387
  def send_examples(filename):
388
  return send_from_directory('', filename)
389
 
 
390
  if __name__ == "__main__":
391
  app.run(host="0.0.0.0", port=7860, debug=True)