-
Notifications
You must be signed in to change notification settings - Fork 820
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix how hashing occurs to allow reproducability
- Loading branch information
Showing
7 changed files
with
83 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import hashlib | ||
import json | ||
import os.path | ||
import typing as t | ||
from dataclasses import dataclass | ||
from pathlib import Path | ||
|
||
from unstructured.ingest.interfaces import ( | ||
ChunkingConfig, | ||
) | ||
from unstructured.ingest.logger import logger | ||
from unstructured.ingest.pipeline.interfaces import ReformatNode | ||
from unstructured.staging.base import convert_to_dict, elements_from_json | ||
|
||
|
||
@dataclass | ||
class Chunker(ReformatNode): | ||
chunking_config: ChunkingConfig | ||
reprocess: bool = False | ||
|
||
def create_hash(self) -> str: | ||
hash_dict = self.chunking_config.to_dict() | ||
return hashlib.sha256(json.dumps(hash_dict, sort_keys=True).encode()).hexdigest()[:32] | ||
|
||
def run(self, elements_json: str) -> str: | ||
elements_json_filename = os.path.basename(elements_json) | ||
filename_ext = os.path.basename(elements_json_filename) | ||
filename = os.path.splitext(filename_ext)[0] | ||
hashed_filename = hashlib.sha256(f"{self.create_hash()}{filename}".encode()).hexdigest()[ | ||
:32 | ||
] | ||
json_filename = f"{hashed_filename}.json" | ||
json_path = (Path(self.get_path()) / json_filename).resolve() | ||
self.pipeline_config.ingest_docs_map[ | ||
hashed_filename | ||
] = self.pipeline_config.ingest_docs_map[filename] | ||
if not self.reprocess and json_path.is_file() and json_path.stat().st_size: | ||
logger.debug(f"File exists: {json_path}, skipping embedding") | ||
return str(json_path) | ||
elements = elements_from_json(filename=elements_json) | ||
chunked_elements = self.chunking_config.chunk(elements=elements) | ||
elements_dict = convert_to_dict(chunked_elements) | ||
with open(json_path, "w", encoding="utf8") as output_f: | ||
logger.info(f"writing embeddings content to {json_path}") | ||
json.dump(elements_dict, output_f, ensure_ascii=False, indent=2) | ||
return str(json_path) | ||
|
||
def get_path(self) -> t.Optional[Path]: | ||
return (Path(self.pipeline_config.get_working_dir()) / "chunked").resolve() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters