refactor: readds partition_file_to_data

This commit is contained in:
Gabriel Luiz Freitas Almeida 2024-06-22 13:37:34 -03:00
commit 4b495fc0a1

View file

@ -2,7 +2,7 @@ import unicodedata
import xml.etree.ElementTree as ET import xml.etree.ElementTree as ET
from concurrent import futures from concurrent import futures
from pathlib import Path from pathlib import Path
from typing import Callable, List, Optional, Text from typing import Callable, List, Optional
import chardet import chardet
import orjson import orjson
@ -70,30 +70,28 @@ def retrieve_file_paths(
glob = "**/*" if recursive else "*" glob = "**/*" if recursive else "*"
paths = walk_level(path_obj, depth) if depth else path_obj.glob(glob) paths = walk_level(path_obj, depth) if depth else path_obj.glob(glob)
file_paths = [Text(p) for p in paths if p.is_file() and match_types(p) and is_not_hidden(p)] file_paths = [str(p) for p in paths if p.is_file() and match_types(p) and is_not_hidden(p)]
return file_paths return file_paths
# ! Removing unstructured dependency until def partition_file_to_data(file_path: str, silent_errors: bool) -> Optional[Data]:
# ! 3.12 is supported # Use the partition function to load the file
# def partition_file_to_data(file_path: str, silent_errors: bool) -> Optional[Data]: from unstructured.partition.auto import partition # type: ignore
# # Use the partition function to load the file
# from unstructured.partition.auto import partition # type: ignore
# try: try:
# elements = partition(file_path) elements = partition(file_path)
# except Exception as e: except Exception as e:
# if not silent_errors: if not silent_errors:
# raise ValueError(f"Error loading file {file_path}: {e}") from e raise ValueError(f"Error loading file {file_path}: {e}") from e
# return None return None
# # Create a Data # Create a Data
# text = "\n\n".join([Text(el) for el in elements]) text = "\n\n".join([str(el) for el in elements])
# metadata = elements.metadata if hasattr(elements, "metadata") else {} metadata = elements.metadata if hasattr(elements, "metadata") else {}
# metadata["file_path"] = file_path metadata["file_path"] = file_path
# record = Data(text=text, data=metadata) record = Data(text=text, data=metadata)
# return record return record
def read_text_file(file_path: str) -> str: def read_text_file(file_path: str) -> str: