Upload backend/app/api/routes/extract_routes.py with huggingface_hub
Browse files
backend/app/api/routes/extract_routes.py
CHANGED
|
@@ -5,6 +5,7 @@ Deterministic offline parser with side-by-side officer review.
|
|
| 5 |
|
| 6 |
from __future__ import annotations
|
| 7 |
|
|
|
|
| 8 |
from typing import Any
|
| 9 |
|
| 10 |
from fastapi import APIRouter, Depends, File, Form, HTTPException, UploadFile, status
|
|
@@ -43,6 +44,44 @@ class ReviewResolutionRequest(BaseModel):
|
|
| 43 |
extracted_value: Any = None
|
| 44 |
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
@router.post("/parse")
|
| 47 |
async def parse_announcement(
|
| 48 |
text: str | None = Form(default=None),
|
|
|
|
| 5 |
|
| 6 |
from __future__ import annotations
|
| 7 |
|
| 8 |
+
import os
|
| 9 |
from typing import Any
|
| 10 |
|
| 11 |
from fastapi import APIRouter, Depends, File, Form, HTTPException, UploadFile, status
|
|
|
|
| 44 |
extracted_value: Any = None
|
| 45 |
|
| 46 |
|
| 47 |
+
@router.get("/samples")
|
| 48 |
+
def list_sample_announcements(
|
| 49 |
+
current_user: UserModel = Depends(require_officer_or_coordinator),
|
| 50 |
+
) -> list[dict[str, Any]]:
|
| 51 |
+
"""Returns the 25 real-format drive announcements with extracted content and metadata."""
|
| 52 |
+
ann_dir = None
|
| 53 |
+
for cand in [
|
| 54 |
+
os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", "..", "..", "fixtures", "announcements")),
|
| 55 |
+
os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", "..", "fixtures", "announcements")),
|
| 56 |
+
os.path.abspath("fixtures/announcements"),
|
| 57 |
+
]:
|
| 58 |
+
if os.path.exists(cand):
|
| 59 |
+
ann_dir = cand
|
| 60 |
+
break
|
| 61 |
+
if not os.path.exists(ann_dir):
|
| 62 |
+
return []
|
| 63 |
+
|
| 64 |
+
results = []
|
| 65 |
+
files = sorted([f for f in os.listdir(ann_dir) if not f.endswith(".json") and not f.startswith(".")])
|
| 66 |
+
for f in files:
|
| 67 |
+
path = os.path.join(ann_dir, f)
|
| 68 |
+
ext = f.rsplit(".", 1)[1].upper()
|
| 69 |
+
name_part = f.split("_", 1)[1].rsplit(".", 1)[0].replace("_", " ").title()
|
| 70 |
+
try:
|
| 71 |
+
with open(path, "rb") as fp:
|
| 72 |
+
text = _loader.load_from_bytes(fp.read(), f)
|
| 73 |
+
results.append({
|
| 74 |
+
"filename": f,
|
| 75 |
+
"label": f"{name_part} ({ext})",
|
| 76 |
+
"format": ext,
|
| 77 |
+
"company_name": name_part.split()[0],
|
| 78 |
+
"text": text,
|
| 79 |
+
})
|
| 80 |
+
except Exception:
|
| 81 |
+
continue
|
| 82 |
+
return results
|
| 83 |
+
|
| 84 |
+
|
| 85 |
@router.post("/parse")
|
| 86 |
async def parse_announcement(
|
| 87 |
text: str | None = Form(default=None),
|