Files
wehub-resource-sync 461bf6fd40
CI / lint (3.11) (push) Has been cancelled
CI / lint (3.12) (push) Has been cancelled
CI / lint (3.13) (push) Has been cancelled
CI / shellcheck (push) Has been cancelled
CI / shfmt (push) Has been cancelled
CI / setup (3.11) (push) Has been cancelled
CI / setup (3.12) (push) Has been cancelled
CI / setup (3.13) (push) Has been cancelled
CI / check-licenses (3.12) (push) Has been cancelled
CI / test_unit (3.11) (push) Has been cancelled
CI / test_unit (3.12) (push) Has been cancelled
CI / test_unit (3.13) (push) Has been cancelled
CI / test_unit_no_extras (3.11) (push) Has been cancelled
CI / test_unit_no_extras (3.12) (push) Has been cancelled
CI / test_json_to_html (3.12) (push) Has been cancelled
CI / test_unit_no_extras (3.13) (push) Has been cancelled
CI / test_unit_dependency_extras (csv, 3.12, --extra csv) (push) Has been cancelled
CI / test_unit_dependency_extras (xlsx, 3.11, --extra xlsx) (push) Has been cancelled
CI / test_unit_dependency_extras (xlsx, 3.12, --extra xlsx) (push) Has been cancelled
CI / test_unit_dependency_extras (csv, 3.11, --extra csv) (push) Has been cancelled
CI / test_unit_dependency_extras (csv, 3.13, --extra csv) (push) Has been cancelled
CI / test_unit_dependency_extras (docx, 3.11, --extra docx) (push) Has been cancelled
CI / test_unit_dependency_extras (docx, 3.12, --extra docx) (push) Has been cancelled
CI / test_unit_dependency_extras (docx, 3.13, --extra docx) (push) Has been cancelled
CI / test_unit_dependency_extras (markdown, 3.11, --extra md) (push) Has been cancelled
CI / test_unit_dependency_extras (markdown, 3.12, --extra md) (push) Has been cancelled
CI / test_unit_dependency_extras (markdown, 3.13, --extra md) (push) Has been cancelled
CI / test_unit_dependency_extras (odt, 3.11, --extra odt) (push) Has been cancelled
CI / test_unit_dependency_extras (odt, 3.12, --extra odt) (push) Has been cancelled
CI / test_unit_dependency_extras (odt, 3.13, --extra odt) (push) Has been cancelled
CI / test_unit_dependency_extras (pdf-image, 3.11, --extra pdf --extra image --extra paddleocr) (push) Has been cancelled
CI / test_unit_dependency_extras (pdf-image, 3.12, --extra pdf --extra image --extra paddleocr) (push) Has been cancelled
CI / test_unit_dependency_extras (pdf-image, 3.13, --extra pdf --extra image --extra paddleocr) (push) Has been cancelled
CI / test_unit_dependency_extras (pptx, 3.11, --extra pptx) (push) Has been cancelled
CI / test_unit_dependency_extras (pptx, 3.12, --extra pptx) (push) Has been cancelled
CI / test_unit_dependency_extras (pptx, 3.13, --extra pptx) (push) Has been cancelled
CI / test_unit_dependency_extras (pypandoc, 3.11, --extra epub --extra org --extra rtf --extra rst) (push) Has been cancelled
CI / test_unit_dependency_extras (pypandoc, 3.12, --extra epub --extra org --extra rtf --extra rst) (push) Has been cancelled
CI / test_unit_dependency_extras (pypandoc, 3.13, --extra epub --extra org --extra rtf --extra rst) (push) Has been cancelled
Build And Push Docker Image / set-short-sha (push) Has been cancelled
Partition Benchmark / setup (push) Has been cancelled
Partition Benchmark / Measure and compare partition() runtime (push) Has been cancelled
CI / test_unit_dependency_extras (xlsx, 3.13, --extra xlsx) (push) Has been cancelled
CI / test_ingest_src (3.12) (push) Has been cancelled
CI / test_json_to_markdown (3.12) (push) Has been cancelled
CI / changelog (push) Has been cancelled
CI / test_dockerfile (push) Has been cancelled
CodeQL / Analyze (python) (push) Has been cancelled
Build And Push Docker Image / build-images (linux/amd64, opensource-linux-8core) (push) Has been cancelled
Build And Push Docker Image / build-images (linux/arm64, ubuntu-24.04-arm) (push) Has been cancelled
Build And Push Docker Image / publish-images (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:33:56 +08:00

100 lines
2.6 KiB
Python

from typing import Any, Dict, List, TypedDict
from unstructured.documents.elements import ElementMetadata, Text
class Properties(TypedDict):
name: str
dataType: List[str]
exclude_metadata_keys = (
"coordinates",
"data_source",
"detection_class_prob",
"emphasized_texts",
"is_continuation",
"links",
"orig_elements",
"key_value_pairs",
)
def stage_for_weaviate(elements: List[Text]) -> List[Dict[str, Any]]:
"""Converts a list of elements into a list of dictionaries that can be uploaded to
Weaviate. The outputs will conform to the schema created with
create_unstructured_weaviate_class.
References
----------
https://weaviate.io/developers/weaviate/tutorials/import#batch-import-process
"""
data: List[Dict[str, Any]] = []
for element in elements:
properties = element.metadata.to_dict()
for k in exclude_metadata_keys:
if k in properties:
del properties[k]
properties["text"] = element.text
properties["category"] = element.category
data.append(properties)
return data
def create_unstructured_weaviate_class(class_name: str = "UnstructuredDocument"):
"""Creates a Weaviate schema class for Unstructured documents using the information
available in ElementMetadata.
Parameters
----------
class_name: str
The name to use for the Unstructured class in the schema.
Defaults to "UnstructuredDocument".
References
----------
https://weaviate.io/developers/weaviate/client-libraries/python#manual-batching
"""
properties: List[Properties] = [
{
"name": "text",
"dataType": ["text"],
},
{
"name": "category",
"dataType": ["text"],
},
]
for name, annotation in ElementMetadata.__annotations__.items():
if name not in exclude_metadata_keys:
data_type = _annotation_to_weaviate_data_type(annotation)
properties.append(
{
"name": name,
"dataType": data_type,
},
)
class_dict = {
"class": class_name,
"properties": properties,
}
return class_dict
def _annotation_to_weaviate_data_type(annotation: str):
if "str" in annotation:
return ["text"]
elif "int" in annotation:
return ["int"]
elif "float" in annotation:
return ["number"]
elif "date" in annotation:
return ["date"]
else:
raise ValueError(f"Annotation {annotation} does not map to a Weaviate dataType.")