triflix commited on
Commit
6894202
·
verified ·
1 Parent(s): 5b41007

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -7
app.py CHANGED
@@ -79,9 +79,9 @@ async def ocr_endpoint(files: List[UploadFile] = File(...), max_pages: int | Non
79
  if len(files) > 15:
80
  raise HTTPException(status_code=400, detail="Maximum 15 files allowed.")
81
 
82
- final_output = []
83
 
84
- for file in files:
85
  filename = file.filename.lower()
86
  ext = filename.split(".")[-1]
87
 
@@ -91,17 +91,40 @@ async def ocr_endpoint(files: List[UploadFile] = File(...), max_pages: int | Non
91
  with open(temp_path, "wb") as f:
92
  f.write(await file.read())
93
 
 
 
 
 
 
 
 
94
  # PDF
 
95
  if filename.endswith(".pdf"):
96
  img_paths = pdf_to_images(temp_path, max_pages=max_pages)
97
- for img_path in img_paths:
98
- final_output.extend(extract_text(img_path))
99
 
100
- # Images
 
 
 
 
 
 
 
 
 
 
101
  elif filename.endswith((".jpg", ".jpeg", ".png")):
102
- final_output.extend(extract_text(temp_path))
 
 
 
 
 
103
 
104
  else:
105
  raise HTTPException(status_code=400, detail=f"Unsupported type: {filename}")
106
 
107
- return JSONResponse({"results": final_output})
 
 
 
79
  if len(files) > 15:
80
  raise HTTPException(status_code=400, detail="Maximum 15 files allowed.")
81
 
82
+ structured_output = {"files": []}
83
 
84
+ for index, file in enumerate(files, start=1):
85
  filename = file.filename.lower()
86
  ext = filename.split(".")[-1]
87
 
 
91
  with open(temp_path, "wb") as f:
92
  f.write(await file.read())
93
 
94
+ file_record = {
95
+ "file_id": f"file_{index}",
96
+ "filename": filename,
97
+ "pages": []
98
+ }
99
+
100
+ # -------------------------------
101
  # PDF
102
+ # -------------------------------
103
  if filename.endswith(".pdf"):
104
  img_paths = pdf_to_images(temp_path, max_pages=max_pages)
 
 
105
 
106
+ for page_idx, img_path in enumerate(img_paths):
107
+ ocr_results = extract_text(img_path)
108
+
109
+ file_record["pages"].append({
110
+ "page_index": page_idx,
111
+ "results": ocr_results
112
+ })
113
+
114
+ # -------------------------------
115
+ # IMAGE
116
+ # -------------------------------
117
  elif filename.endswith((".jpg", ".jpeg", ".png")):
118
+ ocr_results = extract_text(temp_path)
119
+
120
+ file_record["pages"].append({
121
+ "page_index": 0,
122
+ "results": ocr_results
123
+ })
124
 
125
  else:
126
  raise HTTPException(status_code=400, detail=f"Unsupported type: {filename}")
127
 
128
+ structured_output["files"].append(file_record)
129
+
130
+ return JSONResponse(structured_output)