SFM2001 commited on
Commit
a87acfa
·
1 Parent(s): 0f305ca
templates/index.html CHANGED
@@ -64,4 +64,11 @@
64
 
65
  {% block extra_scripts %}
66
  <script src="{{ url_for('static', filename='js/home_page.js') }}"></script>
 
 
 
 
 
 
 
67
  {% endblock %}
 
64
 
65
  {% block extra_scripts %}
66
  <script src="{{ url_for('static', filename='js/home_page.js') }}"></script>
67
+ <script>
68
+ document.getElementById('essayForm').addEventListener('submit', function(e) {
69
+ // This ensures the form submits properly in all environments
70
+ e.preventDefault();
71
+ this.submit();
72
+ });
73
+ </script>
74
  {% endblock %}
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/infer.py CHANGED
@@ -1,127 +1,34 @@
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.')
110
- gra_feedback = feedback.get('GRA_feedback', 'No feedback available. Your essay does not make sense, try to refine it.')
111
- corrected_essay = feedback.get('Corrected_essay', "No suggestions available. Your essay does not make sense, try to refine it.")
112
- essay = essay.replace('\n', '<br>')
113
- corrected_essay = corrected_essay.replace('\\n\\n', '\\n').replace('\\n', '<br>')
114
- print("Essay:", essay, flush=True)
115
- print("Corrected essay:", corrected_essay, flush=True)
116
- print("Feedback:", feedback, flush=True)
117
- print("Scores:", scores, flush=True)
118
- print("Topic:", topic, flush=True)
119
- print("Everything loaded", flush=True)
120
- try:
121
- 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)
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():
@@ -134,10 +41,12 @@ def index():
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,
@@ -151,38 +60,32 @@ def index():
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,
172
  essay=essay,
173
- score_tr=scores[0],
174
- score_cc=scores[1],
175
- score_lr=scores[2],
176
- score_gra=scores[3]
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():
188
  return render_template('rubric_explanation.html')
 
1
+ from flask import render_template, request, redirect, url_for, flash
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  from . import infer_bp
3
  from database import History, db
4
  from flask_login import current_user
5
  from inference.infer_single import *
6
  from utils.util_func import *
7
+ import uuid
8
+
9
+ # In-memory storage for results (works in Spaces)
10
+ results_db = {}
11
 
12
+ @infer_bp.route('/infer')
13
  def infer():
14
+ result_id = request.args.get('result_id')
15
+
16
+ if not result_id or result_id not in results_db:
17
+ flash('No results found. Please submit an essay first.')
 
 
 
 
18
  return redirect(url_for('infer.index'))
19
 
20
+ data = results_db[result_id]
 
 
 
 
21
 
22
+ return render_template('infer.html',
23
+ result=data['scores'],
24
+ topic=data['topic'],
25
+ essay=data['essay'].replace('\n', '<br>'),
26
+ corrected_essay=data['feedback'].get('Corrected_essay', '').replace('\n', '<br>'),
27
+ tr=data['feedback'].get('TR_feedback', ''),
28
+ cc=data['feedback'].get('CC_feedback', ''),
29
+ lr=data['feedback'].get('LR_feedback', ''),
30
+ gra=data['feedback'].get('GRA_feedback', '')
31
+ )
 
 
 
 
 
 
 
 
32
 
33
  @infer_bp.route('/', methods=['GET', 'POST'])
34
  def index():
 
41
  flash('Please provide both a topic and an essay text.')
42
  return render_template('index.html', isempty='True')
43
 
44
+ # Generate unique ID for this submission
45
+ result_id = str(uuid.uuid4())
46
 
47
+ # Process essay
48
+ if len(essay.split()) < 20:
49
+ results_db[result_id] = {
50
  'scores': [1, 1, 1, 1],
51
  'topic': topic,
52
  'essay': essay,
 
60
  }
61
  else:
62
  scores, feedback = generate_and_score_essay(topic, essay)
63
+ results_db[result_id] = {
64
  'scores': scores,
65
  'topic': topic,
66
  'essay': essay.replace('\\n', '\n'),
67
  'feedback': feedback or {}
68
  }
69
 
 
 
 
 
 
70
  # Save to database if authenticated
71
  if current_user.is_authenticated:
 
72
  new_history = History(
73
  user_id=current_user.id,
74
  topic=topic,
75
  essay=essay,
76
+ score_tr=int(results_db[result_id]['scores'][0]),
77
+ score_cc=int(results_db[result_id]['scores'][1]),
78
+ score_lr=int(results_db[result_id]['scores'][2]),
79
+ score_gra=int(results_db[result_id]['scores'][3])
80
  )
81
  db.session.add(new_history)
82
  db.session.commit()
83
+
84
+ # Redirect to infer page with result_id in URL
85
+ return redirect(url_for('infer.infer', result_id=result_id))
86
 
87
  return render_template('index.html', isempty='False',
88
  username=current_user.nickname if current_user.is_authenticated else '')
 
89
  @infer_bp.route('/rubric_explanation')
90
  def rubric_explanation():
91
  return render_template('rubric_explanation.html')