Commit
·
ca13a3e
1
Parent(s):
4298c08
fix json formate from main.py index new new new thing new
Browse files- main.py +49 -19
- templates/index.html +13 -7
main.py
CHANGED
@@ -128,26 +128,54 @@ from similarity_check.llm_based_scoring.llm import llm_score
|
|
128 |
app = Flask(__name__)
|
129 |
app.config['JSON_SORT_KEYS'] = False
|
130 |
app.config['JSONIFY_PRETTYPRINT_REGULAR'] = False
|
|
|
131 |
|
132 |
# Create a temporary directory for file uploads
|
133 |
UPLOAD_FOLDER = tempfile.mkdtemp()
|
134 |
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
135 |
logger.info(f"Using temporary upload folder: {UPLOAD_FOLDER}")
|
136 |
|
137 |
-
# Configure CORS for
|
138 |
CORS(app, resources={
|
139 |
r"/*": {
|
140 |
-
"origins":
|
141 |
"methods": ["GET", "POST", "OPTIONS"],
|
142 |
-
"allow_headers": ["Content-Type", "Authorization", "Accept"]
|
|
|
143 |
}
|
144 |
})
|
145 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
146 |
@app.route('/')
|
147 |
def index():
|
148 |
try:
|
149 |
response = make_response(render_template('index.html'))
|
150 |
-
response.headers['Content-Type'] = 'text/html'
|
151 |
return response
|
152 |
except Exception as e:
|
153 |
return jsonify({"error": str(e)}), 500
|
@@ -258,7 +286,8 @@ def compute_marks():
|
|
258 |
correct_answers = request.form.getlist('correct_answers[]')
|
259 |
if not correct_answers:
|
260 |
return jsonify({
|
261 |
-
"error": "
|
|
|
262 |
}), 400
|
263 |
|
264 |
# Create TFIDF values for correct answers
|
@@ -266,14 +295,16 @@ def compute_marks():
|
|
266 |
max_tfidf = create_tfidf_values(correct_answers)
|
267 |
except Exception as e:
|
268 |
return jsonify({
|
269 |
-
"error":
|
|
|
270 |
}), 400
|
271 |
|
272 |
# Get all uploaded files
|
273 |
files = request.files.getlist('file')
|
274 |
if not files:
|
275 |
return jsonify({
|
276 |
-
"error": "
|
|
|
277 |
}), 400
|
278 |
|
279 |
# Create a temporary directory for processing
|
@@ -287,6 +318,10 @@ def compute_marks():
|
|
287 |
# Process each file
|
288 |
for file in files:
|
289 |
try:
|
|
|
|
|
|
|
|
|
290 |
# Get folder structure from file path
|
291 |
path_parts = file.filename.split('/')
|
292 |
if len(path_parts) < 2:
|
@@ -372,7 +407,8 @@ def compute_marks():
|
|
372 |
|
373 |
if not results:
|
374 |
return jsonify({
|
375 |
-
"error": "
|
|
|
376 |
}), 400
|
377 |
|
378 |
# Clean the results for JSON response
|
@@ -381,29 +417,23 @@ def compute_marks():
|
|
381 |
clean_student = student.encode('ascii', 'ignore').decode('ascii')
|
382 |
clean_results[clean_student] = scores
|
383 |
|
384 |
-
|
385 |
"results": clean_results,
|
386 |
"failed_files": [{
|
387 |
"file": f["file"].encode('ascii', 'ignore').decode('ascii'),
|
388 |
"error": f["error"].encode('ascii', 'ignore').decode('ascii')
|
389 |
} for f in failed_files]
|
390 |
})
|
|
|
|
|
391 |
|
392 |
except Exception as e:
|
393 |
error_msg = str(e).encode('ascii', 'ignore').decode('ascii')
|
394 |
return jsonify({
|
395 |
-
"error":
|
|
|
396 |
}), 500
|
397 |
|
398 |
-
# Add error handlers for common HTTP errors
|
399 |
-
@app.errorhandler(404)
|
400 |
-
def not_found_error(error):
|
401 |
-
return jsonify({"error": "Resource not found"}), 404
|
402 |
-
|
403 |
-
@app.errorhandler(500)
|
404 |
-
def internal_error(error):
|
405 |
-
return jsonify({"error": "Internal server error"}), 500
|
406 |
-
|
407 |
@app.route('/check_logs')
|
408 |
def check_logs():
|
409 |
try:
|
|
|
128 |
app = Flask(__name__)
|
129 |
app.config['JSON_SORT_KEYS'] = False
|
130 |
app.config['JSONIFY_PRETTYPRINT_REGULAR'] = False
|
131 |
+
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 # 16MB max file size
|
132 |
|
133 |
# Create a temporary directory for file uploads
|
134 |
UPLOAD_FOLDER = tempfile.mkdtemp()
|
135 |
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
136 |
logger.info(f"Using temporary upload folder: {UPLOAD_FOLDER}")
|
137 |
|
138 |
+
# Configure CORS for all origins
|
139 |
CORS(app, resources={
|
140 |
r"/*": {
|
141 |
+
"origins": "*",
|
142 |
"methods": ["GET", "POST", "OPTIONS"],
|
143 |
+
"allow_headers": ["Content-Type", "Authorization", "Accept"],
|
144 |
+
"expose_headers": ["Content-Type"]
|
145 |
}
|
146 |
})
|
147 |
|
148 |
+
# Global error handler for all exceptions
|
149 |
+
@app.errorhandler(Exception)
|
150 |
+
def handle_exception(e):
|
151 |
+
# Log the error for debugging
|
152 |
+
app.logger.error(f"Unhandled exception: {str(e)}")
|
153 |
+
return jsonify({
|
154 |
+
"error": "Internal server error",
|
155 |
+
"message": str(e)
|
156 |
+
}), 500
|
157 |
+
|
158 |
+
# Handle 404 errors
|
159 |
+
@app.errorhandler(404)
|
160 |
+
def not_found_error(error):
|
161 |
+
return jsonify({
|
162 |
+
"error": "Not found",
|
163 |
+
"message": "The requested resource was not found"
|
164 |
+
}), 404
|
165 |
+
|
166 |
+
# Handle 400 Bad Request
|
167 |
+
@app.errorhandler(400)
|
168 |
+
def bad_request_error(error):
|
169 |
+
return jsonify({
|
170 |
+
"error": "Bad request",
|
171 |
+
"message": str(error)
|
172 |
+
}), 400
|
173 |
+
|
174 |
@app.route('/')
|
175 |
def index():
|
176 |
try:
|
177 |
response = make_response(render_template('index.html'))
|
178 |
+
response.headers['Content-Type'] = 'text/html; charset=utf-8'
|
179 |
return response
|
180 |
except Exception as e:
|
181 |
return jsonify({"error": str(e)}), 500
|
|
|
286 |
correct_answers = request.form.getlist('correct_answers[]')
|
287 |
if not correct_answers:
|
288 |
return jsonify({
|
289 |
+
"error": "Missing data",
|
290 |
+
"message": "No correct answers provided"
|
291 |
}), 400
|
292 |
|
293 |
# Create TFIDF values for correct answers
|
|
|
295 |
max_tfidf = create_tfidf_values(correct_answers)
|
296 |
except Exception as e:
|
297 |
return jsonify({
|
298 |
+
"error": "TFIDF error",
|
299 |
+
"message": f"Error creating TFIDF values: {str(e)}"
|
300 |
}), 400
|
301 |
|
302 |
# Get all uploaded files
|
303 |
files = request.files.getlist('file')
|
304 |
if not files:
|
305 |
return jsonify({
|
306 |
+
"error": "Missing data",
|
307 |
+
"message": "No files uploaded"
|
308 |
}), 400
|
309 |
|
310 |
# Create a temporary directory for processing
|
|
|
318 |
# Process each file
|
319 |
for file in files:
|
320 |
try:
|
321 |
+
# Validate file
|
322 |
+
if not file or not file.filename:
|
323 |
+
continue
|
324 |
+
|
325 |
# Get folder structure from file path
|
326 |
path_parts = file.filename.split('/')
|
327 |
if len(path_parts) < 2:
|
|
|
407 |
|
408 |
if not results:
|
409 |
return jsonify({
|
410 |
+
"error": "Processing error",
|
411 |
+
"message": "No results computed"
|
412 |
}), 400
|
413 |
|
414 |
# Clean the results for JSON response
|
|
|
417 |
clean_student = student.encode('ascii', 'ignore').decode('ascii')
|
418 |
clean_results[clean_student] = scores
|
419 |
|
420 |
+
response = jsonify({
|
421 |
"results": clean_results,
|
422 |
"failed_files": [{
|
423 |
"file": f["file"].encode('ascii', 'ignore').decode('ascii'),
|
424 |
"error": f["error"].encode('ascii', 'ignore').decode('ascii')
|
425 |
} for f in failed_files]
|
426 |
})
|
427 |
+
response.headers['Content-Type'] = 'application/json'
|
428 |
+
return response
|
429 |
|
430 |
except Exception as e:
|
431 |
error_msg = str(e).encode('ascii', 'ignore').decode('ascii')
|
432 |
return jsonify({
|
433 |
+
"error": "Server error",
|
434 |
+
"message": f"Error computing marks: {error_msg}"
|
435 |
}), 500
|
436 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
437 |
@app.route('/check_logs')
|
438 |
def check_logs():
|
439 |
try:
|
templates/index.html
CHANGED
@@ -987,6 +987,7 @@
|
|
987 |
throw new Error("No valid image files found in the uploaded folder");
|
988 |
}
|
989 |
|
|
|
990 |
const response = await fetch('/compute_marks', {
|
991 |
method: 'POST',
|
992 |
headers: {
|
@@ -995,20 +996,25 @@
|
|
995 |
body: formData
|
996 |
});
|
997 |
|
998 |
-
|
|
|
|
|
|
|
999 |
const contentType = response.headers.get('content-type');
|
1000 |
-
if (contentType
|
1001 |
-
|
1002 |
-
} else {
|
1003 |
throw new Error('Server did not return JSON');
|
1004 |
}
|
1005 |
|
|
|
|
|
|
|
1006 |
if (!response.ok) {
|
1007 |
-
throw new Error(result.error || `Server returned error ${response.status}`);
|
1008 |
}
|
1009 |
|
1010 |
if (result.error) {
|
1011 |
-
throw new Error(result.error);
|
1012 |
}
|
1013 |
|
1014 |
if (!result.results) {
|
@@ -1026,7 +1032,7 @@
|
|
1026 |
}
|
1027 |
|
1028 |
} catch (error) {
|
1029 |
-
console.error('Error:', error);
|
1030 |
alert('Error computing marks: ' + error.message);
|
1031 |
} finally {
|
1032 |
hideLoading();
|
|
|
987 |
throw new Error("No valid image files found in the uploaded folder");
|
988 |
}
|
989 |
|
990 |
+
console.log('Sending request to compute marks...');
|
991 |
const response = await fetch('/compute_marks', {
|
992 |
method: 'POST',
|
993 |
headers: {
|
|
|
996 |
body: formData
|
997 |
});
|
998 |
|
999 |
+
console.log('Response status:', response.status);
|
1000 |
+
console.log('Response headers:', Object.fromEntries(response.headers.entries()));
|
1001 |
+
|
1002 |
+
// Check if the response is JSON
|
1003 |
const contentType = response.headers.get('content-type');
|
1004 |
+
if (!contentType || !contentType.includes('application/json')) {
|
1005 |
+
console.error('Non-JSON response:', await response.text());
|
|
|
1006 |
throw new Error('Server did not return JSON');
|
1007 |
}
|
1008 |
|
1009 |
+
const result = await response.json();
|
1010 |
+
console.log('Parsed response:', result);
|
1011 |
+
|
1012 |
if (!response.ok) {
|
1013 |
+
throw new Error(result.message || result.error || `Server returned error ${response.status}`);
|
1014 |
}
|
1015 |
|
1016 |
if (result.error) {
|
1017 |
+
throw new Error(result.message || result.error);
|
1018 |
}
|
1019 |
|
1020 |
if (!result.results) {
|
|
|
1032 |
}
|
1033 |
|
1034 |
} catch (error) {
|
1035 |
+
console.error('Error details:', error);
|
1036 |
alert('Error computing marks: ' + error.message);
|
1037 |
} finally {
|
1038 |
hideLoading();
|