HowWebWorks commited on
Commit ·
f439d97
1
Parent(s): 7133dd9
add endpoint
Browse files- handler.py +56 -0
- requirements.txt +86 -0
handler.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# handler.py
|
| 2 |
+
import os, json, time, tempfile, base64
|
| 3 |
+
import torch
|
| 4 |
+
from docling.document_converter import DocumentConverter
|
| 5 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 6 |
+
from supabase import create_client
|
| 7 |
+
from huggingface_hub import EndpointHandler
|
| 8 |
+
|
| 9 |
+
class Handler(EndpointHandler):
|
| 10 |
+
def __init__(self):
|
| 11 |
+
from dotenv import load_dotenv
|
| 12 |
+
load_dotenv()
|
| 13 |
+
# supabase
|
| 14 |
+
self.supabase = create_client(
|
| 15 |
+
os.getenv("DATABASE_URL"),
|
| 16 |
+
os.getenv("SUPABASE_SERVICE_ROLE_KEY")
|
| 17 |
+
)
|
| 18 |
+
# model
|
| 19 |
+
device = "mps" if torch.backends.mps.is_available() else "cpu"
|
| 20 |
+
dtype = torch.float16 if device=="mps" else torch.float32
|
| 21 |
+
self.model = AutoModelForCausalLM.from_pretrained(
|
| 22 |
+
"numind/NuExtract-1.5-tiny",
|
| 23 |
+
torch_dtype=dtype, trust_remote_code=True
|
| 24 |
+
).to(device).eval()
|
| 25 |
+
self.tokenizer = AutoTokenizer.from_pretrained(
|
| 26 |
+
"numind/NuExtract-1.5-tiny", trust_remote_code=True
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
def __call__(self, payload):
|
| 30 |
+
"""
|
| 31 |
+
Expects JSON: {
|
| 32 |
+
"inputs": "<raw text or base64-pdf>",
|
| 33 |
+
"is_pdf": false|true
|
| 34 |
+
}
|
| 35 |
+
"""
|
| 36 |
+
text = payload["inputs"]
|
| 37 |
+
if payload.get("is_pdf"):
|
| 38 |
+
b = base64.b64decode(text)
|
| 39 |
+
with tempfile.NamedTemporaryFile(suffix=".pdf", delete=False) as tmp:
|
| 40 |
+
tmp.write(b)
|
| 41 |
+
path = tmp.name
|
| 42 |
+
conv = DocumentConverter().convert(path)
|
| 43 |
+
text = conv.document.export_to_text()
|
| 44 |
+
os.remove(path)
|
| 45 |
+
|
| 46 |
+
# build your prompt + generate (same as predict_NuExtract)
|
| 47 |
+
prompt = (
|
| 48 |
+
"<|input|>\n### Instruction:\nRemplis la template JSON…\n"
|
| 49 |
+
"### Text:\n" + text + "\n<|output|>"
|
| 50 |
+
)
|
| 51 |
+
enc = self.tokenizer(prompt, return_tensors="pt",
|
| 52 |
+
truncation=True).to(self.model.device)
|
| 53 |
+
out_ids = self.model.generate(**enc, max_new_tokens=1024)
|
| 54 |
+
out = self.tokenizer.decode(out_ids[0], skip_special_tokens=True)
|
| 55 |
+
result = out.split("<|output|>")[1]
|
| 56 |
+
return {"result": json.loads(result)}
|
requirements.txt
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
annotated-types==0.7.0
|
| 2 |
+
attrs==25.3.0
|
| 3 |
+
beautifulsoup4==4.13.4
|
| 4 |
+
certifi==2025.4.26
|
| 5 |
+
charset-normalizer==3.4.2
|
| 6 |
+
click==8.1.8
|
| 7 |
+
dill==0.4.0
|
| 8 |
+
docling==2.32.0
|
| 9 |
+
docling-core==2.31.0
|
| 10 |
+
docling-ibm-models==3.4.3
|
| 11 |
+
docling-parse==4.0.1
|
| 12 |
+
easyocr==1.7.2
|
| 13 |
+
et_xmlfile==2.0.0
|
| 14 |
+
filelock==3.18.0
|
| 15 |
+
filetype==1.2.0
|
| 16 |
+
fsspec==2025.3.2
|
| 17 |
+
huggingface-hub==0.31.4
|
| 18 |
+
idna==3.10
|
| 19 |
+
imageio==2.37.0
|
| 20 |
+
Jinja2==3.1.6
|
| 21 |
+
jsonlines==3.1.0
|
| 22 |
+
jsonref==1.1.0
|
| 23 |
+
jsonschema==4.23.0
|
| 24 |
+
jsonschema-specifications==2025.4.1
|
| 25 |
+
latex2mathml==3.78.0
|
| 26 |
+
lazy_loader==0.4
|
| 27 |
+
lxml==5.4.0
|
| 28 |
+
markdown-it-py==3.0.0
|
| 29 |
+
marko==2.1.3
|
| 30 |
+
MarkupSafe==3.0.2
|
| 31 |
+
mdurl==0.1.2
|
| 32 |
+
mpire==2.10.2
|
| 33 |
+
mpmath==1.3.0
|
| 34 |
+
multiprocess==0.70.18
|
| 35 |
+
networkx==3.4.2
|
| 36 |
+
ninja==1.11.1.4
|
| 37 |
+
numpy==2.2.6
|
| 38 |
+
opencv-python-headless==4.11.0.86
|
| 39 |
+
openpyxl==3.1.5
|
| 40 |
+
packaging==25.0
|
| 41 |
+
pandas==2.2.3
|
| 42 |
+
pillow==11.2.1
|
| 43 |
+
pluggy==1.6.0
|
| 44 |
+
pyclipper==1.3.0.post6
|
| 45 |
+
pydantic==2.11.4
|
| 46 |
+
pydantic-settings==2.9.1
|
| 47 |
+
pydantic_core==2.33.2
|
| 48 |
+
Pygments==2.19.1
|
| 49 |
+
pylatexenc==2.10
|
| 50 |
+
pypdfium2==4.30.1
|
| 51 |
+
python-bidi==0.6.6
|
| 52 |
+
python-dateutil==2.9.0.post0
|
| 53 |
+
python-docx==1.1.2
|
| 54 |
+
python-dotenv==1.1.0
|
| 55 |
+
python-pptx==1.0.2
|
| 56 |
+
pytz==2025.2
|
| 57 |
+
PyYAML==6.0.2
|
| 58 |
+
referencing==0.36.2
|
| 59 |
+
regex==2024.11.6
|
| 60 |
+
requests==2.32.3
|
| 61 |
+
rich==14.0.0
|
| 62 |
+
rpds-py==0.25.0
|
| 63 |
+
rtree==1.4.0
|
| 64 |
+
safetensors==0.5.3
|
| 65 |
+
scikit-image==0.25.2
|
| 66 |
+
scipy==1.15.3
|
| 67 |
+
semchunk==2.2.2
|
| 68 |
+
setuptools==80.8.0
|
| 69 |
+
shapely==2.1.1
|
| 70 |
+
shellingham==1.5.4
|
| 71 |
+
six==1.17.0
|
| 72 |
+
soupsieve==2.7
|
| 73 |
+
sympy==1.14.0
|
| 74 |
+
tabulate==0.9.0
|
| 75 |
+
tifffile==2025.5.10
|
| 76 |
+
tokenizers==0.21.1
|
| 77 |
+
torch==2.7.0
|
| 78 |
+
torchvision==0.22.0
|
| 79 |
+
tqdm==4.67.1
|
| 80 |
+
transformers==4.51.3
|
| 81 |
+
typer==0.15.4
|
| 82 |
+
typing-inspection==0.4.0
|
| 83 |
+
typing_extensions==4.13.2
|
| 84 |
+
tzdata==2025.2
|
| 85 |
+
urllib3==2.4.0
|
| 86 |
+
XlsxWriter==3.2.3
|