Update app.py
Browse files
app.py
CHANGED
|
@@ -29,22 +29,16 @@ async def process_omr(
|
|
| 29 |
omr_marker: UploadFile = File(...),
|
| 30 |
pdf_file: UploadFile = File(...),
|
| 31 |
):
|
| 32 |
-
"""
|
| 33 |
-
Receives OMR-related files, processes them, and returns the analysis results.
|
| 34 |
-
"""
|
| 35 |
|
| 36 |
-
print("πΉ [START] Processing new OMR request...")
|
| 37 |
|
| 38 |
try:
|
| 39 |
-
|
| 40 |
-
print("π§Ή Cleaning old directories...")
|
| 41 |
shutil.rmtree(INPUT_DIR, ignore_errors=True)
|
| 42 |
shutil.rmtree(OUTPUT_DIR, ignore_errors=True)
|
| 43 |
os.makedirs(INPUT_DIR, exist_ok=True)
|
| 44 |
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
| 45 |
|
| 46 |
-
|
| 47 |
-
print("πΎ Saving uploaded files to INPUT_DIR...")
|
| 48 |
uploaded_files = {
|
| 49 |
"config.json": config,
|
| 50 |
"evaluation.json": evaluation,
|
|
@@ -59,8 +53,7 @@ async def process_omr(
|
|
| 59 |
shutil.copyfileobj(file.file, buffer)
|
| 60 |
print(f"β
Saved: {filename}")
|
| 61 |
|
| 62 |
-
|
| 63 |
-
print("π Converting PDF to images...")
|
| 64 |
pdf_path = os.path.join(INPUT_DIR, pdf_file.filename)
|
| 65 |
images = convert_from_path(pdf_path)
|
| 66 |
image_paths = []
|
|
@@ -72,8 +65,6 @@ async def process_omr(
|
|
| 72 |
image_paths.append(img_output_path)
|
| 73 |
print(f"πΌοΈ Converted and saved: {img_output_path}")
|
| 74 |
|
| 75 |
-
# Run main.py as subprocess
|
| 76 |
-
print("π Running main.py with subprocess...")
|
| 77 |
script_path = os.path.join(os.path.dirname(__file__), "main.py")
|
| 78 |
|
| 79 |
process = subprocess.run(
|
|
@@ -82,16 +73,12 @@ async def process_omr(
|
|
| 82 |
text=True,
|
| 83 |
)
|
| 84 |
|
| 85 |
-
print("π Subprocess stdout:")
|
| 86 |
print(process.stdout)
|
| 87 |
-
print("β οΈ Subprocess stderr:")
|
| 88 |
print(process.stderr)
|
| 89 |
|
| 90 |
-
# Expected output files
|
| 91 |
response_json = os.path.join(OUTPUT_DIR, "read_response.json")
|
| 92 |
score_txt = os.path.join(OUTPUT_DIR, "score.txt")
|
| 93 |
|
| 94 |
-
# Collect outputs
|
| 95 |
results = {"converted_images": [], "read_response": None, "score": None}
|
| 96 |
|
| 97 |
output_images = [
|
|
@@ -107,17 +94,17 @@ async def process_omr(
|
|
| 107 |
if os.path.exists(response_json):
|
| 108 |
with open(response_json, "r") as f:
|
| 109 |
results["read_response"] = json.load(f)
|
| 110 |
-
print("
|
| 111 |
|
| 112 |
if os.path.exists(score_txt):
|
| 113 |
with open(score_txt, "r") as f:
|
| 114 |
results["score"] = f.read().strip()
|
| 115 |
-
print("
|
| 116 |
|
| 117 |
-
print("
|
| 118 |
return JSONResponse(content=results)
|
| 119 |
|
| 120 |
except Exception as e:
|
| 121 |
-
print("
|
| 122 |
traceback.print_exc()
|
| 123 |
return JSONResponse(content={"error": str(e)}, status_code=500)
|
|
|
|
| 29 |
omr_marker: UploadFile = File(...),
|
| 30 |
pdf_file: UploadFile = File(...),
|
| 31 |
):
|
|
|
|
|
|
|
|
|
|
| 32 |
|
|
|
|
| 33 |
|
| 34 |
try:
|
| 35 |
+
print("Cleaning old directories...")
|
|
|
|
| 36 |
shutil.rmtree(INPUT_DIR, ignore_errors=True)
|
| 37 |
shutil.rmtree(OUTPUT_DIR, ignore_errors=True)
|
| 38 |
os.makedirs(INPUT_DIR, exist_ok=True)
|
| 39 |
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
| 40 |
|
| 41 |
+
print("Saving uploaded files to INPUT_DIR...")
|
|
|
|
| 42 |
uploaded_files = {
|
| 43 |
"config.json": config,
|
| 44 |
"evaluation.json": evaluation,
|
|
|
|
| 53 |
shutil.copyfileobj(file.file, buffer)
|
| 54 |
print(f"β
Saved: {filename}")
|
| 55 |
|
| 56 |
+
print("Converting PDF to images...")
|
|
|
|
| 57 |
pdf_path = os.path.join(INPUT_DIR, pdf_file.filename)
|
| 58 |
images = convert_from_path(pdf_path)
|
| 59 |
image_paths = []
|
|
|
|
| 65 |
image_paths.append(img_output_path)
|
| 66 |
print(f"πΌοΈ Converted and saved: {img_output_path}")
|
| 67 |
|
|
|
|
|
|
|
| 68 |
script_path = os.path.join(os.path.dirname(__file__), "main.py")
|
| 69 |
|
| 70 |
process = subprocess.run(
|
|
|
|
| 73 |
text=True,
|
| 74 |
)
|
| 75 |
|
|
|
|
| 76 |
print(process.stdout)
|
|
|
|
| 77 |
print(process.stderr)
|
| 78 |
|
|
|
|
| 79 |
response_json = os.path.join(OUTPUT_DIR, "read_response.json")
|
| 80 |
score_txt = os.path.join(OUTPUT_DIR, "score.txt")
|
| 81 |
|
|
|
|
| 82 |
results = {"converted_images": [], "read_response": None, "score": None}
|
| 83 |
|
| 84 |
output_images = [
|
|
|
|
| 94 |
if os.path.exists(response_json):
|
| 95 |
with open(response_json, "r") as f:
|
| 96 |
results["read_response"] = json.load(f)
|
| 97 |
+
print("Loaded read_response.json")
|
| 98 |
|
| 99 |
if os.path.exists(score_txt):
|
| 100 |
with open(score_txt, "r") as f:
|
| 101 |
results["score"] = f.read().strip()
|
| 102 |
+
print("Loaded score.txt")
|
| 103 |
|
| 104 |
+
print("Processing complete.")
|
| 105 |
return JSONResponse(content=results)
|
| 106 |
|
| 107 |
except Exception as e:
|
| 108 |
+
print("Exception during OMR processing:")
|
| 109 |
traceback.print_exc()
|
| 110 |
return JSONResponse(content={"error": str(e)}, status_code=500)
|