Skip to content

Commit 7bbbcd1

Browse files
committed
Replace hf_hub_download with wrapper
1 parent ae8ec91 commit 7bbbcd1

File tree

4 files changed

+26
-18
lines changed

4 files changed

+26
-18
lines changed

unstructured_inference/models/chipper.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import torch
1010
import transformers
1111
from cv2.typing import MatLike
12-
from huggingface_hub import hf_hub_download
1312
from PIL.Image import Image
1413
from transformers import DonutProcessor, VisionEncoderDecoderModel
1514
from transformers.generation.logits_process import LogitsProcessor
@@ -22,7 +21,7 @@
2221
from unstructured_inference.models.unstructuredmodel import (
2322
UnstructuredElementExtractionModel,
2423
)
25-
from unstructured_inference.utils import LazyDict, strip_tags
24+
from unstructured_inference.utils import LazyDict, strip_tags, download_if_needed_and_get_local_path
2625

2726
MODEL_TYPES: Dict[str, Union[LazyDict, dict]] = {
2827
"chipperv1": {
@@ -115,7 +114,7 @@ def initialize(
115114
token=auth_token,
116115
)
117116
if swap_head:
118-
lm_head_file = hf_hub_download(
117+
lm_head_file = download_if_needed_and_get_local_path(
119118
repo_id=pre_trained_model_repo,
120119
filename="lm_head.pth",
121120
token=auth_token,

unstructured_inference/models/detectron2.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from pathlib import Path
44
from typing import Any, Dict, Final, List, Optional, Union
55

6-
from huggingface_hub import hf_hub_download
76
from layoutparser.models.detectron2.layoutmodel import (
87
Detectron2LayoutModel,
98
is_detectron2_available,
@@ -17,7 +16,11 @@
1716
from unstructured_inference.models.unstructuredmodel import (
1817
UnstructuredObjectDetectionModel,
1918
)
20-
from unstructured_inference.utils import LazyDict, LazyEvaluateInfo
19+
from unstructured_inference.utils import (
20+
LazyDict,
21+
LazyEvaluateInfo,
22+
download_if_needed_and_get_local_path,
23+
)
2124

2225
DETECTRON_CONFIG: Final = "lp://PubLayNet/faster_rcnn_R_50_FPN_3x/config"
2326
DEFAULT_LABEL_MAP: Final[Dict[int, str]] = {
@@ -35,12 +38,12 @@
3538
MODEL_TYPES = {
3639
"detectron2_lp": LazyDict(
3740
model_path=LazyEvaluateInfo(
38-
hf_hub_download,
41+
download_if_needed_and_get_local_path,
3942
"layoutparser/detectron2",
4043
"PubLayNet/faster_rcnn_R_50_FPN_3x/model_final.pth",
4144
),
4245
config_path=LazyEvaluateInfo(
43-
hf_hub_download,
46+
download_if_needed_and_get_local_path,
4447
"layoutparser/detectron2",
4548
"PubLayNet/faster_rcnn_R_50_FPN_3x/config.yml",
4649
),
@@ -49,12 +52,12 @@
4952
),
5053
"checkbox": LazyDict(
5154
model_path=LazyEvaluateInfo(
52-
hf_hub_download,
55+
download_if_needed_and_get_local_path,
5356
"unstructuredio/oer-checkbox",
5457
"detectron2_finetuned_oer_checkbox.pth",
5558
),
5659
config_path=LazyEvaluateInfo(
57-
hf_hub_download,
60+
download_if_needed_and_get_local_path,
5861
"unstructuredio/oer-checkbox",
5962
"detectron2_oer_checkbox.json",
6063
),

unstructured_inference/models/detectron2onnx.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import cv2
55
import numpy as np
66
import onnxruntime
7-
from huggingface_hub import hf_hub_download
87
from huggingface_hub.constants import HUGGINGFACE_HUB_CACHE
98
from onnxruntime.capi import _pybind_state as C
109
from onnxruntime.quantization import QuantType, quantize_dynamic
@@ -16,7 +15,11 @@
1615
from unstructured_inference.models.unstructuredmodel import (
1716
UnstructuredObjectDetectionModel,
1817
)
19-
from unstructured_inference.utils import LazyDict, LazyEvaluateInfo
18+
from unstructured_inference.utils import (
19+
LazyDict,
20+
LazyEvaluateInfo,
21+
download_if_needed_and_get_local_path,
22+
)
2023

2124
onnxruntime.set_default_logger_severity(logger_onnx.getEffectiveLevel())
2225

@@ -34,7 +37,7 @@
3437
MODEL_TYPES: Dict[str, Union[LazyDict, dict]] = {
3538
"detectron2_onnx": LazyDict(
3639
model_path=LazyEvaluateInfo(
37-
hf_hub_download,
40+
download_if_needed_and_get_local_path,
3841
"unstructuredio/detectron2_faster_rcnn_R_50_FPN_3x",
3942
"model.onnx",
4043
),
@@ -52,7 +55,7 @@
5255
},
5356
"detectron2_mask_rcnn": LazyDict(
5457
model_path=LazyEvaluateInfo(
55-
hf_hub_download,
58+
download_if_needed_and_get_local_path,
5659
"unstructuredio/detectron2_mask_rcnn_X_101_32x8d_FPN_3x",
5760
"model.onnx",
5861
),

unstructured_inference/models/yolox.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,17 @@
88
import cv2
99
import numpy as np
1010
import onnxruntime
11-
from huggingface_hub import hf_hub_download
1211
from onnxruntime.capi import _pybind_state as C
1312
from PIL import Image as PILImage
1413

1514
from unstructured_inference.constants import ElementType, Source
1615
from unstructured_inference.inference.layoutelement import LayoutElement
1716
from unstructured_inference.models.unstructuredmodel import UnstructuredObjectDetectionModel
18-
from unstructured_inference.utils import LazyDict, LazyEvaluateInfo
17+
from unstructured_inference.utils import (
18+
LazyDict,
19+
LazyEvaluateInfo,
20+
download_if_needed_and_get_local_path,
21+
)
1922

2023
YOLOX_LABEL_MAP = {
2124
0: ElementType.CAPTION,
@@ -34,23 +37,23 @@
3437
MODEL_TYPES = {
3538
"yolox": LazyDict(
3639
model_path=LazyEvaluateInfo(
37-
hf_hub_download,
40+
download_if_needed_and_get_local_path,
3841
"unstructuredio/yolo_x_layout",
3942
"yolox_l0.05.onnx",
4043
),
4144
label_map=YOLOX_LABEL_MAP,
4245
),
4346
"yolox_tiny": LazyDict(
4447
model_path=LazyEvaluateInfo(
45-
hf_hub_download,
48+
download_if_needed_and_get_local_path,
4649
"unstructuredio/yolo_x_layout",
4750
"yolox_tiny.onnx",
4851
),
4952
label_map=YOLOX_LABEL_MAP,
5053
),
5154
"yolox_quantized": LazyDict(
5255
model_path=LazyEvaluateInfo(
53-
hf_hub_download,
56+
download_if_needed_and_get_local_path,
5457
"unstructuredio/yolo_x_layout",
5558
"yolox_l0.05_quantized.onnx",
5659
),

0 commit comments

Comments
 (0)