yamanavijayavardhan commited on
Commit
e57d6e8
·
1 Parent(s): 7a9da00
Files changed (1) hide show
  1. main.py +68 -36
main.py CHANGED
@@ -5,6 +5,27 @@ import logging
5
  import sys
6
  from datetime import datetime
7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  # Create a logs directory in the temp folder
9
  log_dir = os.path.join(tempfile.gettempdir(), 'app_logs')
10
  os.makedirs(log_dir, exist_ok=True)
@@ -151,42 +172,44 @@ def compute_answers():
151
  @app.route('/compute_marks', methods=['POST'])
152
  def compute_marks():
153
  try:
 
 
154
  # Get and process answers
155
  a = request.form.get('answers')
156
  if not a:
157
- log_print("No answers provided", "ERROR")
158
  return jsonify({"error": "No answers provided"}), 400
159
 
160
- log_print("=== Processing Answers ===")
161
- log_print(f"Received answers: {a}")
162
  a = json.loads(a)
163
  answers = []
164
  for i in a:
165
  ans = i.split('\n\n')
166
  answers.append(ans)
167
- log_print(f"Processed answers structure: {answers}")
168
 
169
  # Process files and create data structure
170
  data = {}
171
  parent_folder = os.path.join(cache_dir, 'student_answers')
172
  os.makedirs(parent_folder, exist_ok=True)
173
 
174
- log_print("=== Processing Uploaded Files ===")
175
  files = request.files.getlist('files[]')
176
  if not files:
177
- log_print("No files uploaded", "ERROR")
178
  return jsonify({"error": "No files uploaded"}), 400
179
 
180
- log_print(f"Number of files received: {len(files)}")
181
 
182
- # File processing with logging
183
  for file in files:
184
  if file.filename.endswith(('.jpg', '.jpeg', '.png')):
185
  relative_path = file.filename.replace('\\', '/')
186
  path_parts = relative_path.split('/')
187
 
188
- log_print(f"Processing file: {file.filename}")
189
- log_print(f"Path parts: {path_parts}")
190
 
191
  if len(path_parts) >= 2:
192
  student_folder = path_parts[1]
@@ -197,7 +220,7 @@ def compute_marks():
197
 
198
  save_path = os.path.join(student_dir, file_name)
199
  file.save(save_path)
200
- log_print(f"Saved file: {save_path}")
201
 
202
  if student_folder not in data:
203
  data[student_folder] = []
@@ -206,14 +229,14 @@ def compute_marks():
206
  'name': os.path.splitext(file_name)[0]
207
  })
208
  else:
209
- log_print(f"File {file.filename} doesn't have expected structure", "WARNING")
210
 
211
  # Log data structure
212
- log_print("=== Final Data Structure ===")
213
  for student, images in data.items():
214
- log_print(f"Student: {student}")
215
  for img in sorted(images, key=lambda x: x['name']):
216
- log_print(f" - {img['name']} ({img['path']})")
217
 
218
  # Calculate marks with logging
219
  results = []
@@ -227,11 +250,11 @@ def compute_marks():
227
  try:
228
  image_path = image_info['path']
229
  s_answer = extract_text_from_image(image_path)
230
- log_print(f"\nProcessing {student_folder}/{image_info['name']}:")
231
- log_print(f"Extracted answer: {s_answer}")
232
 
233
  if s_answer and idx < len(answers):
234
- log_print(f"Reference answer: {answers[idx]}")
235
  tf_idf_word_values, max_tfidf = create_tfidf_values(answers[idx])
236
  m = marks(s_answer, sen_vec_answers[idx], word_vec_answers[idx],
237
  tf_idf_word_values, max_tfidf, answers[idx])
@@ -241,10 +264,10 @@ def compute_marks():
241
  mark_value = round(float(m), 2)
242
  student_total += mark_value
243
  student_count += 1
244
- log_print(f"Marks awarded: {mark_value}")
245
  else:
246
  mark_value = 0
247
- log_print(f"No text extracted or no reference answer for index {idx}", "WARNING")
248
 
249
  results.append({
250
  'student': student_folder,
@@ -253,7 +276,7 @@ def compute_marks():
253
  })
254
 
255
  except Exception as e:
256
- log_print(f"Error processing {image_path}: {str(e)}", "ERROR")
257
  results.append({
258
  'student': student_folder,
259
  'image_name': image_info['name'],
@@ -263,17 +286,17 @@ def compute_marks():
263
  # Sort results
264
  results.sort(key=lambda x: (x['student'], x['image_name']))
265
 
266
- log_print("\nFinal Results:")
267
  for r in results:
268
- log_print(f"{r['student']}\t{r['image_name']}\t{r['marks']}")
269
 
270
  # Clean up temporary directory
271
  try:
272
  import shutil
273
  shutil.rmtree(parent_folder)
274
- log_print(f"Cleaned up temporary directory: {parent_folder}")
275
  except Exception as e:
276
- log_print(f"Warning: Could not clean up temporary directory: {e}", "WARNING")
277
 
278
  return jsonify({
279
  "message": results,
@@ -281,7 +304,7 @@ def compute_marks():
281
  }), 200
282
 
283
  except Exception as e:
284
- log_print(f"Error in compute_marks: {str(e)}", "ERROR")
285
  try:
286
  import shutil
287
  shutil.rmtree(parent_folder)
@@ -293,19 +316,19 @@ def compute_marks():
293
 
294
  def marks(answer, sen_vec_answers, word_vec_answers, tf_idf_word_values, max_tfidf, correct_answers):
295
  marks = 0
296
- log_print("=== Marks Calculation ===")
297
- log_print(f"Processing answer: {answer[:100]}...") # Log first 100 chars
298
 
299
  marks1 = tfidf_answer_score(answer, tf_idf_word_values, max_tfidf, marks=10)
300
- log_print(f"TFIDF Score: {marks1}")
301
 
302
  if marks1 > 3:
303
  marks += new_value(marks1, old_min=3, old_max=10, new_min=0, new_max=5)
304
- log_print(f"After TFIDF adjustment: {marks}")
305
 
306
  if marks1 > 2:
307
  marks2 = similarity_model_score(sen_vec_answers, answer)
308
- log_print(f"Sentence Similarity Score: {marks2}")
309
 
310
  if marks2 > 0.95:
311
  marks += 3
@@ -313,7 +336,7 @@ def marks(answer, sen_vec_answers, word_vec_answers, tf_idf_word_values, max_tfi
313
  marks += new_value(marks2, old_min=0.5, old_max=0.95, new_min=0, new_max=3)
314
 
315
  marks3 = fasttext_similarity(word_vec_answers, answer)
316
- log_print(f"Word Similarity Score: {marks3}")
317
 
318
  if marks3 > 0.9:
319
  marks += 2
@@ -321,18 +344,18 @@ def marks(answer, sen_vec_answers, word_vec_answers, tf_idf_word_values, max_tfi
321
  marks += new_value(marks3, old_min=0.4, old_max=0.9, new_min=0, new_max=2)
322
 
323
  marks4 = llm_score(correct_answers, answer)
324
- log_print(f"LLM Scores: {marks4}")
325
 
326
  for i in range(len(marks4)):
327
  marks4[i] = float(marks4[i])
328
 
329
  m = max(marks4)
330
- log_print(f"Max LLM Score: {m}")
331
 
332
  marks = marks/2 + m/2
333
- log_print(f"Final marks: {marks}")
334
  else:
335
- log_print("TFIDF score too low, returning 0", "WARNING")
336
 
337
  return marks
338
 
@@ -340,5 +363,14 @@ def marks(answer, sen_vec_answers, word_vec_answers, tf_idf_word_values, max_tfi
340
 
341
 
342
 
 
 
 
 
 
 
 
 
 
343
  if __name__ == '__main__':
344
  app.run(host='0.0.0.0', port=7860)
 
5
  import sys
6
  from datetime import datetime
7
 
8
+ # Set up logging to write to stdout
9
+ logging.basicConfig(
10
+ level=logging.INFO,
11
+ format='%(message)s', # Simplified format for better readability
12
+ handlers=[
13
+ logging.StreamHandler(sys.stdout)
14
+ ]
15
+ )
16
+
17
+ # Create a custom logger
18
+ logger = logging.getLogger(__name__)
19
+
20
+ # Override the print function to ensure visibility
21
+ def custom_print(*args, **kwargs):
22
+ print(*args, **kwargs, flush=True) # Force flush to ensure immediate output
23
+ logger.info(' '.join(str(arg) for arg in args))
24
+
25
+ # Replace the standard print with our custom print
26
+ import builtins
27
+ builtins.print = custom_print
28
+
29
  # Create a logs directory in the temp folder
30
  log_dir = os.path.join(tempfile.gettempdir(), 'app_logs')
31
  os.makedirs(log_dir, exist_ok=True)
 
172
  @app.route('/compute_marks', methods=['POST'])
173
  def compute_marks():
174
  try:
175
+ print("\n=== Starting Marks Computation ===")
176
+
177
  # Get and process answers
178
  a = request.form.get('answers')
179
  if not a:
180
+ print("ERROR: No answers provided")
181
  return jsonify({"error": "No answers provided"}), 400
182
 
183
+ print("=== Processing Answers ===")
184
+ print(f"Received answers: {a}")
185
  a = json.loads(a)
186
  answers = []
187
  for i in a:
188
  ans = i.split('\n\n')
189
  answers.append(ans)
190
+ print(f"Processed answers structure: {answers}")
191
 
192
  # Process files and create data structure
193
  data = {}
194
  parent_folder = os.path.join(cache_dir, 'student_answers')
195
  os.makedirs(parent_folder, exist_ok=True)
196
 
197
+ print("\n=== Processing Uploaded Files ===")
198
  files = request.files.getlist('files[]')
199
  if not files:
200
+ print("ERROR: No files uploaded")
201
  return jsonify({"error": "No files uploaded"}), 400
202
 
203
+ print(f"Number of files received: {len(files)}")
204
 
205
+ # File processing
206
  for file in files:
207
  if file.filename.endswith(('.jpg', '.jpeg', '.png')):
208
  relative_path = file.filename.replace('\\', '/')
209
  path_parts = relative_path.split('/')
210
 
211
+ print(f"\nProcessing file: {file.filename}")
212
+ print(f"Path parts: {path_parts}")
213
 
214
  if len(path_parts) >= 2:
215
  student_folder = path_parts[1]
 
220
 
221
  save_path = os.path.join(student_dir, file_name)
222
  file.save(save_path)
223
+ print(f"Saved file: {save_path}")
224
 
225
  if student_folder not in data:
226
  data[student_folder] = []
 
229
  'name': os.path.splitext(file_name)[0]
230
  })
231
  else:
232
+ print(f"WARNING: File {file.filename} doesn't have expected structure")
233
 
234
  # Log data structure
235
+ print("\n=== Final Data Structure ===")
236
  for student, images in data.items():
237
+ print(f"Student: {student}")
238
  for img in sorted(images, key=lambda x: x['name']):
239
+ print(f" - {img['name']} ({img['path']})")
240
 
241
  # Calculate marks with logging
242
  results = []
 
250
  try:
251
  image_path = image_info['path']
252
  s_answer = extract_text_from_image(image_path)
253
+ print(f"\nProcessing {student_folder}/{image_info['name']}:")
254
+ print(f"Extracted answer: {s_answer}")
255
 
256
  if s_answer and idx < len(answers):
257
+ print(f"Reference answer: {answers[idx]}")
258
  tf_idf_word_values, max_tfidf = create_tfidf_values(answers[idx])
259
  m = marks(s_answer, sen_vec_answers[idx], word_vec_answers[idx],
260
  tf_idf_word_values, max_tfidf, answers[idx])
 
264
  mark_value = round(float(m), 2)
265
  student_total += mark_value
266
  student_count += 1
267
+ print(f"Marks awarded: {mark_value}")
268
  else:
269
  mark_value = 0
270
+ print(f"No text extracted or no reference answer for index {idx}", "WARNING")
271
 
272
  results.append({
273
  'student': student_folder,
 
276
  })
277
 
278
  except Exception as e:
279
+ print(f"Error processing {image_path}: {str(e)}", "ERROR")
280
  results.append({
281
  'student': student_folder,
282
  'image_name': image_info['name'],
 
286
  # Sort results
287
  results.sort(key=lambda x: (x['student'], x['image_name']))
288
 
289
+ print("\nFinal Results:")
290
  for r in results:
291
+ print(f"{r['student']}\t{r['image_name']}\t{r['marks']}")
292
 
293
  # Clean up temporary directory
294
  try:
295
  import shutil
296
  shutil.rmtree(parent_folder)
297
+ print(f"Cleaned up temporary directory: {parent_folder}")
298
  except Exception as e:
299
+ print(f"Warning: Could not clean up temporary directory: {e}", "WARNING")
300
 
301
  return jsonify({
302
  "message": results,
 
304
  }), 200
305
 
306
  except Exception as e:
307
+ print(f"Error in compute_marks: {str(e)}", "ERROR")
308
  try:
309
  import shutil
310
  shutil.rmtree(parent_folder)
 
316
 
317
  def marks(answer, sen_vec_answers, word_vec_answers, tf_idf_word_values, max_tfidf, correct_answers):
318
  marks = 0
319
+ print("\n=== Marks Calculation ===")
320
+ print(f"Processing answer: {answer[:100]}...")
321
 
322
  marks1 = tfidf_answer_score(answer, tf_idf_word_values, max_tfidf, marks=10)
323
+ print(f"TFIDF Score: {marks1}")
324
 
325
  if marks1 > 3:
326
  marks += new_value(marks1, old_min=3, old_max=10, new_min=0, new_max=5)
327
+ print(f"After TFIDF adjustment: {marks}")
328
 
329
  if marks1 > 2:
330
  marks2 = similarity_model_score(sen_vec_answers, answer)
331
+ print(f"Sentence Similarity Score: {marks2}")
332
 
333
  if marks2 > 0.95:
334
  marks += 3
 
336
  marks += new_value(marks2, old_min=0.5, old_max=0.95, new_min=0, new_max=3)
337
 
338
  marks3 = fasttext_similarity(word_vec_answers, answer)
339
+ print(f"Word Similarity Score: {marks3}")
340
 
341
  if marks3 > 0.9:
342
  marks += 2
 
344
  marks += new_value(marks3, old_min=0.4, old_max=0.9, new_min=0, new_max=2)
345
 
346
  marks4 = llm_score(correct_answers, answer)
347
+ print(f"LLM Scores: {marks4}")
348
 
349
  for i in range(len(marks4)):
350
  marks4[i] = float(marks4[i])
351
 
352
  m = max(marks4)
353
+ print(f"Max LLM Score: {m}")
354
 
355
  marks = marks/2 + m/2
356
+ print(f"Final marks: {marks}")
357
  else:
358
+ print("WARNING: TFIDF score too low, returning 0")
359
 
360
  return marks
361
 
 
363
 
364
 
365
 
366
+ @app.route('/check_logs')
367
+ def check_logs():
368
+ try:
369
+ with open(log_file, 'r') as f:
370
+ logs = f.read()
371
+ return jsonify({"logs": logs})
372
+ except Exception as e:
373
+ return jsonify({"error": str(e)})
374
+
375
  if __name__ == '__main__':
376
  app.run(host='0.0.0.0', port=7860)