remove_weights_from_python_wheel
#6
by jdye64 - opened
- MANIFEST.in +2 -1
- README.md +6 -2
- nemotron_table_structure_v1/model.py +18 -8
- pyproject.toml +2 -1
MANIFEST.in
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
include README.md
|
| 2 |
include THIRD_PARTY_NOTICES.md
|
| 3 |
-
recursive-include nemotron_table_structure_v1
|
|
|
|
| 4 |
|
| 5 |
|
| 6 |
|
|
|
|
| 1 |
include README.md
|
| 2 |
include THIRD_PARTY_NOTICES.md
|
| 3 |
+
recursive-include nemotron_table_structure_v1 *.py *.json
|
| 4 |
+
recursive-exclude nemotron_table_structure_v1 *.pth
|
| 5 |
|
| 6 |
|
| 7 |
|
README.md
CHANGED
|
@@ -148,8 +148,12 @@ import numpy as np
|
|
| 148 |
import matplotlib.pyplot as plt
|
| 149 |
from PIL import Image
|
| 150 |
|
| 151 |
-
from
|
| 152 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
|
| 154 |
# Load image
|
| 155 |
path = "./example.png"
|
|
|
|
| 148 |
import matplotlib.pyplot as plt
|
| 149 |
from PIL import Image
|
| 150 |
|
| 151 |
+
from nemotron_table_structure_v1 import (
|
| 152 |
+
define_model,
|
| 153 |
+
plot_sample,
|
| 154 |
+
postprocess_preds_table_structure,
|
| 155 |
+
reformat_for_plotting,
|
| 156 |
+
)
|
| 157 |
|
| 158 |
# Load image
|
| 159 |
path = "./example.png"
|
nemotron_table_structure_v1/model.py
CHANGED
|
@@ -10,35 +10,45 @@ import numpy.typing as npt
|
|
| 10 |
import torch.nn as nn
|
| 11 |
import torch.nn.functional as F
|
| 12 |
from typing import Dict, List, Tuple, Union
|
|
|
|
| 13 |
from .yolox.boxes import postprocess
|
| 14 |
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
|
|
|
|
| 17 |
"""
|
| 18 |
Defines and initializes the model based on the configuration.
|
| 19 |
|
| 20 |
Args:
|
| 21 |
-
config_name (str): Configuration name. Defaults to "
|
| 22 |
verbose (bool): Whether to print verbose output. Defaults to True.
|
| 23 |
|
| 24 |
Returns:
|
| 25 |
torch.nn.Module: The initialized YOLOX model.
|
| 26 |
"""
|
| 27 |
# Load model from exp_file
|
| 28 |
-
#
|
| 29 |
sys.path.append(os.path.dirname(__file__))
|
| 30 |
exp_module = importlib.import_module("table_structure_v1")
|
| 31 |
|
| 32 |
config = exp_module.Exp()
|
| 33 |
model = config.get_model()
|
| 34 |
|
| 35 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
if verbose:
|
| 37 |
-
print(" ->
|
| 38 |
|
| 39 |
-
# Find package directory and load weights (nemotron_table_structure_v1)
|
| 40 |
-
package_dir = os.path.dirname(os.path.abspath(__file__))
|
| 41 |
-
weights_path = os.path.join(package_dir, "weights.pth")
|
| 42 |
state_dict = torch.load(weights_path, map_location="cpu", weights_only=False)
|
| 43 |
model.load_state_dict(state_dict["model"], strict=True)
|
| 44 |
|
|
|
|
| 10 |
import torch.nn as nn
|
| 11 |
import torch.nn.functional as F
|
| 12 |
from typing import Dict, List, Tuple, Union
|
| 13 |
+
from huggingface_hub import hf_hub_download
|
| 14 |
from .yolox.boxes import postprocess
|
| 15 |
|
| 16 |
+
# HuggingFace repository for downloading model weights
|
| 17 |
+
HF_REPO_ID = "nvidia/nemotron-table-structure-v1"
|
| 18 |
+
WEIGHTS_FILENAME = "nemotron_table_structure_v1/weights.pth"
|
| 19 |
|
| 20 |
+
|
| 21 |
+
def define_model(config_name: str = "table_structure_v1", verbose: bool = True) -> nn.Module:
|
| 22 |
"""
|
| 23 |
Defines and initializes the model based on the configuration.
|
| 24 |
|
| 25 |
Args:
|
| 26 |
+
config_name (str): Configuration name. Defaults to "table_structure_v1".
|
| 27 |
verbose (bool): Whether to print verbose output. Defaults to True.
|
| 28 |
|
| 29 |
Returns:
|
| 30 |
torch.nn.Module: The initialized YOLOX model.
|
| 31 |
"""
|
| 32 |
# Load model from exp_file
|
| 33 |
+
# table_structure_v1.py is in the same directory as model.py
|
| 34 |
sys.path.append(os.path.dirname(__file__))
|
| 35 |
exp_module = importlib.import_module("table_structure_v1")
|
| 36 |
|
| 37 |
config = exp_module.Exp()
|
| 38 |
model = config.get_model()
|
| 39 |
|
| 40 |
+
# Download weights from HuggingFace Hub (cached locally after first download)
|
| 41 |
+
if verbose:
|
| 42 |
+
print(f" -> Downloading/loading weights from HuggingFace: {HF_REPO_ID}")
|
| 43 |
+
|
| 44 |
+
weights_path = hf_hub_download(
|
| 45 |
+
repo_id=HF_REPO_ID,
|
| 46 |
+
filename=WEIGHTS_FILENAME,
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
if verbose:
|
| 50 |
+
print(f" -> Weights cached at: {weights_path}")
|
| 51 |
|
|
|
|
|
|
|
|
|
|
| 52 |
state_dict = torch.load(weights_path, map_location="cpu", weights_only=False)
|
| 53 |
model.load_state_dict(state_dict["model"], strict=True)
|
| 54 |
|
pyproject.toml
CHANGED
|
@@ -33,6 +33,7 @@ dependencies = [
|
|
| 33 |
"matplotlib>=3.3.0",
|
| 34 |
"pandas>=1.3.0",
|
| 35 |
"Pillow>=8.0.0",
|
|
|
|
| 36 |
]
|
| 37 |
|
| 38 |
[project.urls]
|
|
@@ -45,7 +46,7 @@ Documentation = "https://huggingface.co/nvidia/nemotron-table-structure-v1"
|
|
| 45 |
packages = ["nemotron_table_structure_v1", "nemotron_table_structure_v1.yolox", "nemotron_table_structure_v1.post_processing"]
|
| 46 |
|
| 47 |
[tool.setuptools.package-data]
|
| 48 |
-
"*" = ["
|
| 49 |
|
| 50 |
|
| 51 |
|
|
|
|
| 33 |
"matplotlib>=3.3.0",
|
| 34 |
"pandas>=1.3.0",
|
| 35 |
"Pillow>=8.0.0",
|
| 36 |
+
"huggingface_hub>=0.20.0",
|
| 37 |
]
|
| 38 |
|
| 39 |
[project.urls]
|
|
|
|
| 46 |
packages = ["nemotron_table_structure_v1", "nemotron_table_structure_v1.yolox", "nemotron_table_structure_v1.post_processing"]
|
| 47 |
|
| 48 |
[tool.setuptools.package-data]
|
| 49 |
+
"*" = ["config.json"]
|
| 50 |
|
| 51 |
|
| 52 |
|