Spaces:
Runtime error
Runtime error
File size: 1,495 Bytes
105b369 |
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
import json
from pathlib import Path
from typing import List
from phi.document.base import Document
from phi.document.reader.base import Reader
from phi.utils.log import logger
class JSONReader(Reader):
"""Reader for JSON files"""
chunk: bool = False
def read(self, path: Path) -> List[Document]:
if not path:
raise ValueError("No path provided")
if not path.exists():
raise FileNotFoundError(f"Could not find file: {path}")
try:
logger.info(f"Reading: {path}")
json_name = path.name.split(".")[0]
json_contents = json.loads(path.read_text("utf-8"))
if isinstance(json_contents, dict):
json_contents = [json_contents]
documents = [
Document(
name=json_name,
id=f"{json_name}_{page_number}",
meta_data={"page": page_number},
content=json.dumps(content),
)
for page_number, content in enumerate(json_contents, start=1)
]
if self.chunk:
logger.debug("Chunking documents not yet supported for JSONReader")
# chunked_documents = []
# for document in documents:
# chunked_documents.extend(self.chunk_document(document))
# return chunked_documents
return documents
except Exception:
raise
|