Spaces:
Running
Running
File size: 1,033 Bytes
816b4b1 52419fe 816b4b1 52419fe 816b4b1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
import pickle
from typing import Any, Dict, List
from graphgen.bases.base_reader import BaseReader
class PickleReader(BaseReader):
"""
Read pickle files, requiring the top-level object to be List[Dict[str, Any]].
Columns:
- type: The type of the document (e.g., "text", "image", etc.)
- if type is "text", "content" column must be present.
"""
def read(self, file_path: str) -> List[Dict[str, Any]]:
with open(file_path, "rb") as f:
data = pickle.load(f)
if not isinstance(data, list):
raise ValueError("Pickle file must contain a list of documents.")
for doc in data:
if not isinstance(doc, dict):
raise ValueError("Every item in the list must be a dict.")
assert "type" in doc, f"Missing 'type' in document: {doc}"
if doc.get("type") == "text" and self.text_column not in doc:
raise ValueError(f"Missing '{self.text_column}' in document: {doc}")
return self.filter(data)
|