File size: 4,059 Bytes
a87acfa 4f591e5 33ff5ca 4f591e5 0f305ca 4f591e5 a87acfa 0f305ca a87acfa 4f591e5 a87acfa 0f305ca a87acfa 0f305ca a87acfa 4f591e5 0f305ca 4f591e5 0f305ca a87acfa 0f305ca a87acfa 0f305ca 4f591e5 0f305ca a87acfa 0f305ca 4f591e5 a87acfa 4f591e5 a87acfa 0f305ca 4f591e5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
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')
|