SimpleAES / views /infer.py
SFM2001's picture
s
a87acfa
from flask import render_template, request, redirect, url_for, flash
from . import infer_bp
from database import History, db
from flask_login import current_user
from inference.infer_single import *
from utils.util_func import *
import uuid
# In-memory storage for results (works in Spaces)
results_db = {}
@infer_bp.route('/infer')
def infer():
result_id = request.args.get('result_id')
if not result_id or result_id not in results_db:
flash('No results found. Please submit an essay first.')
return redirect(url_for('infer.index'))
data = results_db[result_id]
return render_template('infer.html',
result=data['scores'],
topic=data['topic'],
essay=data['essay'].replace('\n', '<br>'),
corrected_essay=data['feedback'].get('Corrected_essay', '').replace('\n', '<br>'),
tr=data['feedback'].get('TR_feedback', ''),
cc=data['feedback'].get('CC_feedback', ''),
lr=data['feedback'].get('LR_feedback', ''),
gra=data['feedback'].get('GRA_feedback', '')
)
@infer_bp.route('/', methods=['GET', 'POST'])
def index():
set_seed(42)
if request.method == 'POST':
topic = request.form['topic'].strip()
essay = request.form['essay'].strip()
if not topic or not essay:
flash('Please provide both a topic and an essay text.')
return render_template('index.html', isempty='True')
# Generate unique ID for this submission
result_id = str(uuid.uuid4())
# Process essay
if len(essay.split()) < 20:
results_db[result_id] = {
'scores': [1, 1, 1, 1],
'topic': topic,
'essay': essay,
'feedback': {
'TR_feedback': 'Essay too short for proper evaluation',
'CC_feedback': 'Essay too short for proper evaluation',
'LR_feedback': 'Essay too short for proper evaluation',
'GRA_feedback': 'Essay too short for proper evaluation',
'Corrected_essay': 'No suggestions available for short essays'
}
}
else:
scores, feedback = generate_and_score_essay(topic, essay)
results_db[result_id] = {
'scores': scores,
'topic': topic,
'essay': essay.replace('\\n', '\n'),
'feedback': feedback or {}
}
# Save to database if authenticated
if current_user.is_authenticated:
new_history = History(
user_id=current_user.id,
topic=topic,
essay=essay,
score_tr=int(results_db[result_id]['scores'][0]),
score_cc=int(results_db[result_id]['scores'][1]),
score_lr=int(results_db[result_id]['scores'][2]),
score_gra=int(results_db[result_id]['scores'][3])
)
db.session.add(new_history)
db.session.commit()
# Redirect to infer page with result_id in URL
return redirect(url_for('infer.infer', result_id=result_id))
return render_template('index.html', isempty='False',
username=current_user.nickname if current_user.is_authenticated else '')
@infer_bp.route('/rubric_explanation')
def rubric_explanation():
return render_template('rubric_explanation.html')
@infer_bp.route('/rubric_explanation/task_response')
def task_response():
return render_template('task_response.html')
@infer_bp.route('/rubric_explanation/coherence_cohesion')
def coherence_cohesion():
return render_template('coherence_cohesion.html')
@infer_bp.route('/rubric_explanation/lexical_resource')
def lexical_resource():
return render_template('lexical_resource.html')
@infer_bp.route('/rubric_explanation/grammatical_range_accuracy')
def grammatical_range_accuracy():
return render_template('grammatical_range_accuracy.html')