Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- run_all_samples.py +14 -29
run_all_samples.py
CHANGED
|
@@ -1,57 +1,42 @@
|
|
| 1 |
# run_all_samples.py
|
| 2 |
import os
|
| 3 |
-
import io
|
| 4 |
-
import zipfile
|
| 5 |
import json
|
| 6 |
from pathlib import Path
|
| 7 |
|
| 8 |
-
import requests
|
| 9 |
-
|
| 10 |
from fastapi.testclient import TestClient
|
| 11 |
-
from app import app # your
|
| 12 |
|
| 13 |
# -----------------------------
|
| 14 |
# 1. Paths
|
| 15 |
# -----------------------------
|
| 16 |
-
TRAIN_ZIP_URL = "https://hackrx.blob.core.windows.net/files/TRAINING_SAMPLES.zip?sv=2025-07-05&spr=https&st=2025-11-28T06%3A47%3A35Z&se=2025-11-29T06%3A47%3A35Z&sr=b&sp=r&sig=yB8R2zjoRL2%2FWRuv7E1lvmWSHAkm%2FoIGsepj2Io9pak%3D"
|
| 17 |
-
|
| 18 |
BASE_DIR = Path(__file__).parent
|
| 19 |
-
SAMPLES_DIR = BASE_DIR / "
|
| 20 |
RESULTS_DIR = BASE_DIR / "results"
|
| 21 |
|
| 22 |
-
SAMPLES_DIR.mkdir(exist_ok=True)
|
| 23 |
RESULTS_DIR.mkdir(exist_ok=True)
|
| 24 |
|
| 25 |
# -----------------------------
|
| 26 |
-
# 2.
|
| 27 |
-
# -----------------------------
|
| 28 |
-
print("π₯ Downloading training samples ZIP ...")
|
| 29 |
-
resp = requests.get(TRAIN_ZIP_URL, stream=True, timeout=60)
|
| 30 |
-
|
| 31 |
-
if resp.status_code != 200:
|
| 32 |
-
raise RuntimeError("Failed to download training samples.")
|
| 33 |
-
|
| 34 |
-
print("π¦ Extracting ZIP ...")
|
| 35 |
-
with zipfile.ZipFile(io.BytesIO(resp.content)) as z:
|
| 36 |
-
z.extractall(SAMPLES_DIR)
|
| 37 |
-
|
| 38 |
-
print(f"β
Extracted into: {SAMPLES_DIR}")
|
| 39 |
-
|
| 40 |
-
# -----------------------------
|
| 41 |
-
# 3. Initialize FastAPI client
|
| 42 |
# -----------------------------
|
| 43 |
client = TestClient(app)
|
| 44 |
|
| 45 |
# -----------------------------
|
| 46 |
-
#
|
| 47 |
# -----------------------------
|
| 48 |
def is_bill_file(path: Path):
|
| 49 |
return path.suffix.lower() in [".pdf", ".png", ".jpg", ".jpeg"]
|
| 50 |
|
| 51 |
-
print("\nπ Running extractor on
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
summary = {}
|
| 54 |
|
|
|
|
|
|
|
|
|
|
| 55 |
for fp in sorted(SAMPLES_DIR.rglob("*")):
|
| 56 |
if not fp.is_file() or not is_bill_file(fp):
|
| 57 |
continue
|
|
@@ -66,7 +51,7 @@ for fp in sorted(SAMPLES_DIR.rglob("*")):
|
|
| 66 |
res = client.post(
|
| 67 |
"/extract-bill-data",
|
| 68 |
json={"document": file_url},
|
| 69 |
-
timeout=
|
| 70 |
)
|
| 71 |
except Exception as e:
|
| 72 |
print(f"β Error calling extractor: {e}")
|
|
@@ -93,7 +78,7 @@ for fp in sorted(SAMPLES_DIR.rglob("*")):
|
|
| 93 |
|
| 94 |
summary[fp.name] = total_items
|
| 95 |
print(f" β Saved: {out_path}")
|
| 96 |
-
print(f" β Items Extracted: {total_items}")
|
| 97 |
|
| 98 |
print("\nπ DONE! All sample bills processed.\n")
|
| 99 |
|
|
|
|
| 1 |
# run_all_samples.py
|
| 2 |
import os
|
|
|
|
|
|
|
| 3 |
import json
|
| 4 |
from pathlib import Path
|
| 5 |
|
|
|
|
|
|
|
| 6 |
from fastapi.testclient import TestClient
|
| 7 |
+
from app import app # import your FastAPI app
|
| 8 |
|
| 9 |
# -----------------------------
|
| 10 |
# 1. Paths
|
| 11 |
# -----------------------------
|
|
|
|
|
|
|
| 12 |
BASE_DIR = Path(__file__).parent
|
| 13 |
+
SAMPLES_DIR = BASE_DIR / "samples" # <-- UPDATED
|
| 14 |
RESULTS_DIR = BASE_DIR / "results"
|
| 15 |
|
|
|
|
| 16 |
RESULTS_DIR.mkdir(exist_ok=True)
|
| 17 |
|
| 18 |
# -----------------------------
|
| 19 |
+
# 2. Init FastAPI client
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
# -----------------------------
|
| 21 |
client = TestClient(app)
|
| 22 |
|
| 23 |
# -----------------------------
|
| 24 |
+
# 3. File filter
|
| 25 |
# -----------------------------
|
| 26 |
def is_bill_file(path: Path):
|
| 27 |
return path.suffix.lower() in [".pdf", ".png", ".jpg", ".jpeg"]
|
| 28 |
|
| 29 |
+
print("\nπ Running extractor on local sample files...\n")
|
| 30 |
+
print(f"π Using samples from: {SAMPLES_DIR}\n")
|
| 31 |
+
|
| 32 |
+
if not SAMPLES_DIR.exists():
|
| 33 |
+
raise RuntimeError("β ERROR: 'samples/' folder does not exist in your Space.")
|
| 34 |
|
| 35 |
summary = {}
|
| 36 |
|
| 37 |
+
# -----------------------------
|
| 38 |
+
# 4. Process all files
|
| 39 |
+
# -----------------------------
|
| 40 |
for fp in sorted(SAMPLES_DIR.rglob("*")):
|
| 41 |
if not fp.is_file() or not is_bill_file(fp):
|
| 42 |
continue
|
|
|
|
| 51 |
res = client.post(
|
| 52 |
"/extract-bill-data",
|
| 53 |
json={"document": file_url},
|
| 54 |
+
timeout=120
|
| 55 |
)
|
| 56 |
except Exception as e:
|
| 57 |
print(f"β Error calling extractor: {e}")
|
|
|
|
| 78 |
|
| 79 |
summary[fp.name] = total_items
|
| 80 |
print(f" β Saved: {out_path}")
|
| 81 |
+
print(f" β Items Extracted: {total_items}\n")
|
| 82 |
|
| 83 |
print("\nπ DONE! All sample bills processed.\n")
|
| 84 |
|