SFM2001 commited on
Commit
0f305ca
·
1 Parent(s): 61626a8

infer update no session

Browse files
__pycache__/create_app.cpython-312.pyc CHANGED
Binary files a/__pycache__/create_app.cpython-312.pyc and b/__pycache__/create_app.cpython-312.pyc differ
 
views/__pycache__/cache.cpython-312.pyc ADDED
Binary file (1 kB). View file
 
views/__pycache__/infer.cpython-312.pyc CHANGED
Binary files a/views/__pycache__/infer.cpython-312.pyc and b/views/__pycache__/infer.cpython-312.pyc differ
 
views/cache.py ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from datetime import datetime, timedelta
2
+ import uuid
3
+
4
+ # Simple in-memory cache (replace with Redis in production)
5
+ result_cache = {}
6
+ CACHE_EXPIRY_HOURS = 1
7
+
8
+ def store_result(data):
9
+ cache_id = str(uuid.uuid4())
10
+ expiry = datetime.now() + timedelta(hours=CACHE_EXPIRY_HOURS)
11
+ result_cache[cache_id] = {
12
+ 'data': data,
13
+ 'expiry': expiry
14
+ }
15
+ return cache_id
16
+
17
+ def get_result(cache_id):
18
+ if cache_id not in result_cache:
19
+ return None
20
+
21
+ if datetime.now() > result_cache[cache_id]['expiry']:
22
+ del result_cache[cache_id]
23
+ return None
24
+
25
+ return result_cache[cache_id]['data']
views/infer.py CHANGED
@@ -1,19 +1,109 @@
1
- from flask import render_template, session, request, redirect, url_for, flash
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  from . import infer_bp
3
- import secrets
4
  from database import History, db
5
  from flask_login import current_user
6
- from inference.infer_single import *
7
  from utils.util_func import *
 
 
8
  @infer_bp.route('/infer', methods=['GET'])
9
  def infer():
10
- print("Infer function entered", flush=True)
11
- scores = session.get('scores', [])
12
- topic = session.get('topic', "")
13
- essay = session.get('essay', "")
14
- feedback = session.get('feedback', {}) or {}
15
- feedback = dict(feedback)
16
- print("Feedback:", feedback, flush=True)
 
 
 
 
 
 
 
 
 
17
  tr_feedback = feedback.get('TR_feedback', 'No feedback available. Your essay does not make sense, try to refine it.')
18
  cc_feedback = feedback.get('CC_feedback', 'No feedback available. Your essay does not make sense, try to refine it.')
19
  lr_feedback = feedback.get('LR_feedback', 'No feedback available. Your essay does not make sense, try to refine it.')
@@ -32,7 +122,6 @@ def infer():
32
  except Exception as e:
33
  print(e, flush=True)
34
  return render_template('error.html', error=str(e)), 500
35
-
36
 
37
  @infer_bp.route('/', methods=['GET', 'POST'])
38
  def index():
@@ -40,28 +129,43 @@ def index():
40
  if request.method == 'POST':
41
  topic = request.form['topic'].strip()
42
  essay = request.form['essay'].strip()
43
- print("ESSAY TOPIC", essay, topic, flush=True)
44
  if not topic or not essay:
45
  flash('Please provide both a topic and an essay text.')
46
  return render_template('index.html', isempty='True')
 
47
  num_words = len(essay.split())
 
48
  if num_words < 20:
49
- session['scores'] = [1, 1, 1, 1]
50
- session['topic'] = topic
51
- session['essay'] = essay
52
- session['feedback'] = {}
53
- print("LESS THAN 20 WORDS", flush=True)
 
 
 
 
 
 
 
54
  else:
55
- scores, feedback = generate_and_score_essay(
56
- topic, essay
57
- )
58
- session['scores'] = scores
59
- session['topic'] = topic
60
- session['essay'] = essay.replace('\\n', '\n')
61
- session['feedback'] = feedback or {} # Ensure feedback is at least an empty dict
62
- session.modified = True
 
 
 
 
 
 
63
  if current_user.is_authenticated:
64
- scores = [int(score) for score in session['scores']]
65
  new_history = History(
66
  user_id=current_user.id,
67
  topic=topic,
@@ -73,9 +177,11 @@ def index():
73
  )
74
  db.session.add(new_history)
75
  db.session.commit()
76
- print("REDIRECT TO INFER", flush=True)
77
- return redirect(url_for('infer.infer'))
78
- return render_template('index.html', isempty='False', username=current_user.nickname if current_user.is_authenticated else '')
 
 
79
 
80
  @infer_bp.route('/rubric_explanation')
81
  def rubric_explanation():
 
1
+ # from flask import render_template, session, request, redirect, url_for, flash
2
+ # from . import infer_bp
3
+ # import secrets
4
+ # from database import History, db
5
+ # from flask_login import current_user
6
+ # from inference.infer_single import *
7
+ # from utils.util_func import *
8
+ # @infer_bp.route('/infer', methods=['GET'])
9
+ # def infer():
10
+ # print("Infer function entered", flush=True)
11
+ # scores = session.get('scores', [])
12
+ # topic = session.get('topic', "")
13
+ # essay = session.get('essay', "")
14
+ # feedback = session.get('feedback', {}) or {}
15
+ # feedback = dict(feedback)
16
+ # print("Feedback:", feedback, flush=True)
17
+ # tr_feedback = feedback.get('TR_feedback', 'No feedback available. Your essay does not make sense, try to refine it.')
18
+ # cc_feedback = feedback.get('CC_feedback', 'No feedback available. Your essay does not make sense, try to refine it.')
19
+ # lr_feedback = feedback.get('LR_feedback', 'No feedback available. Your essay does not make sense, try to refine it.')
20
+ # gra_feedback = feedback.get('GRA_feedback', 'No feedback available. Your essay does not make sense, try to refine it.')
21
+ # corrected_essay = feedback.get('Corrected_essay', "No suggestions available. Your essay does not make sense, try to refine it.")
22
+ # essay = essay.replace('\n', '<br>')
23
+ # corrected_essay = corrected_essay.replace('\\n\\n', '\\n').replace('\\n', '<br>')
24
+ # print("Essay:", essay, flush=True)
25
+ # print("Corrected essay:", corrected_essay, flush=True)
26
+ # print("Feedback:", feedback, flush=True)
27
+ # print("Scores:", scores, flush=True)
28
+ # print("Topic:", topic, flush=True)
29
+ # print("Everything loaded", flush=True)
30
+ # try:
31
+ # return render_template('infer.html', result=scores, topic=topic, essay=essay, tr=tr_feedback, cc=cc_feedback, lr=lr_feedback, gra=gra_feedback, corrected_essay=corrected_essay)
32
+ # except Exception as e:
33
+ # print(e, flush=True)
34
+ # return render_template('error.html', error=str(e)), 500
35
+
36
+
37
+ # @infer_bp.route('/', methods=['GET', 'POST'])
38
+ # def index():
39
+ # set_seed(42)
40
+ # if request.method == 'POST':
41
+ # topic = request.form['topic'].strip()
42
+ # essay = request.form['essay'].strip()
43
+ # print("ESSAY TOPIC", essay, topic, flush=True)
44
+ # if not topic or not essay:
45
+ # flash('Please provide both a topic and an essay text.')
46
+ # return render_template('index.html', isempty='True')
47
+ # num_words = len(essay.split())
48
+ # if num_words < 20:
49
+ # session['scores'] = [1, 1, 1, 1]
50
+ # session['topic'] = topic
51
+ # session['essay'] = essay
52
+ # session['feedback'] = {}
53
+ # print("LESS THAN 20 WORDS", flush=True)
54
+ # else:
55
+ # scores, feedback = generate_and_score_essay(
56
+ # topic, essay
57
+ # )
58
+ # session['scores'] = scores
59
+ # session['topic'] = topic
60
+ # session['essay'] = essay.replace('\\n', '\n')
61
+ # session['feedback'] = feedback or {} # Ensure feedback is at least an empty dict
62
+ # session.modified = True
63
+ # if current_user.is_authenticated:
64
+ # scores = [int(score) for score in session['scores']]
65
+ # new_history = History(
66
+ # user_id=current_user.id,
67
+ # topic=topic,
68
+ # essay=essay,
69
+ # score_tr=scores[0],
70
+ # score_cc=scores[1],
71
+ # score_lr=scores[2],
72
+ # score_gra=scores[3]
73
+ # )
74
+ # db.session.add(new_history)
75
+ # db.session.commit()
76
+ # print("REDIRECT TO INFER", flush=True)
77
+ # return redirect(url_for('infer.infer'))
78
+ # return render_template('index.html', isempty='False', username=current_user.nickname if current_user.is_authenticated else '')
79
+
80
+
81
+ from flask import render_template, request, redirect, url_for, flash, make_response
82
  from . import infer_bp
 
83
  from database import History, db
84
  from flask_login import current_user
85
+ from inference.infer_single import *
86
  from utils.util_func import *
87
+ from .cache import store_result, get_result
88
+
89
  @infer_bp.route('/infer', methods=['GET'])
90
  def infer():
91
+ cache_id = request.cookies.get('result_id')
92
+ if not cache_id:
93
+ flash('No essay results found. Please submit an essay first.')
94
+ return redirect(url_for('infer.index'))
95
+
96
+ cached_data = get_result(cache_id)
97
+ if not cached_data:
98
+ flash('Results expired. Please submit your essay again.')
99
+ return redirect(url_for('infer.index'))
100
+
101
+ # Process cached data
102
+ scores = cached_data['scores']
103
+ topic = cached_data['topic']
104
+ essay = cached_data['essay']
105
+ feedback = cached_data.get('feedback', {})
106
+
107
  tr_feedback = feedback.get('TR_feedback', 'No feedback available. Your essay does not make sense, try to refine it.')
108
  cc_feedback = feedback.get('CC_feedback', 'No feedback available. Your essay does not make sense, try to refine it.')
109
  lr_feedback = feedback.get('LR_feedback', 'No feedback available. Your essay does not make sense, try to refine it.')
 
122
  except Exception as e:
123
  print(e, flush=True)
124
  return render_template('error.html', error=str(e)), 500
 
125
 
126
  @infer_bp.route('/', methods=['GET', 'POST'])
127
  def index():
 
129
  if request.method == 'POST':
130
  topic = request.form['topic'].strip()
131
  essay = request.form['essay'].strip()
132
+
133
  if not topic or not essay:
134
  flash('Please provide both a topic and an essay text.')
135
  return render_template('index.html', isempty='True')
136
+
137
  num_words = len(essay.split())
138
+
139
  if num_words < 20:
140
+ result_data = {
141
+ 'scores': [1, 1, 1, 1],
142
+ 'topic': topic,
143
+ 'essay': essay,
144
+ 'feedback': {
145
+ 'TR_feedback': 'Essay too short for proper evaluation',
146
+ 'CC_feedback': 'Essay too short for proper evaluation',
147
+ 'LR_feedback': 'Essay too short for proper evaluation',
148
+ 'GRA_feedback': 'Essay too short for proper evaluation',
149
+ 'Corrected_essay': 'No suggestions available for short essays'
150
+ }
151
+ }
152
  else:
153
+ scores, feedback = generate_and_score_essay(topic, essay)
154
+ result_data = {
155
+ 'scores': scores,
156
+ 'topic': topic,
157
+ 'essay': essay.replace('\\n', '\n'),
158
+ 'feedback': feedback or {}
159
+ }
160
+
161
+ # Store in cache and set cookie
162
+ cache_id = store_result(result_data)
163
+ response = make_response(redirect(url_for('infer.infer')))
164
+ response.set_cookie('result_id', cache_id, max_age=3600) # 1 hour expiry
165
+
166
+ # Save to database if authenticated
167
  if current_user.is_authenticated:
168
+ scores = [int(score) for score in result_data['scores']]
169
  new_history = History(
170
  user_id=current_user.id,
171
  topic=topic,
 
177
  )
178
  db.session.add(new_history)
179
  db.session.commit()
180
+
181
+ return response
182
+
183
+ return render_template('index.html', isempty='False',
184
+ username=current_user.nickname if current_user.is_authenticated else '')
185
 
186
  @infer_bp.route('/rubric_explanation')
187
  def rubric_explanation():