Skip to content

Cli

DocumentProcessor

Source code in Docs2KG/cli.py
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class DocumentProcessor:
    PROCESSORS = {
        ".pdf": PDFDocling,
        ".docx": DOCXMammoth,
        ".html": HTMLDocling,
        ".epub": EPUBDigitization,
    }

    @classmethod
    def get_processor(cls, file_path: Path) -> Optional[Type]:
        """Get the appropriate processor for the file type."""
        return cls.PROCESSORS.get(file_path.suffix.lower())

    @classmethod
    def get_supported_formats(cls) -> str:
        """Get a string of supported file formats."""
        return ", ".join(cls.PROCESSORS.keys())

get_processor(file_path) classmethod

Get the appropriate processor for the file type.

Source code in Docs2KG/cli.py
30
31
32
33
@classmethod
def get_processor(cls, file_path: Path) -> Optional[Type]:
    """Get the appropriate processor for the file type."""
    return cls.PROCESSORS.get(file_path.suffix.lower())

get_supported_formats() classmethod

Get a string of supported file formats.

Source code in Docs2KG/cli.py
35
36
37
38
@classmethod
def get_supported_formats(cls) -> str:
    """Get a string of supported file formats."""
    return ", ".join(cls.PROCESSORS.keys())

batch_process(input_dir, project_id, formats, agent_name, agent_type)

Process all supported documents in a directory.

INPUT_DIR: Directory containing documents to process

Source code in Docs2KG/cli.py
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
@cli.command()
@click.argument(
    "input_dir",
    type=click.Path(exists=True),
    default=PROJECT_CONFIG.data.input_dir.as_posix(),
)
@click.option(
    "--project-id",
    "-p",
    default="default",
    help="Project ID for the knowledge graph construction",
)
@click.option(
    "--formats",
    "-f",
    help='Comma-separated list of file formats to process (e.g., "pdf,docx,html")',
)
@click.option(
    "--agent-name",
    "-n",
    default="phi3.5",
    help="Name of the agent to use for NER extraction",
)
@click.option(
    "--agent-type",
    "-t",
    default="ollama",
    help="Type of the agent to use for NER extraction",
)
def batch_process(input_dir, project_id, formats, agent_name, agent_type):
    """Process all supported documents in a directory.

    INPUT_DIR: Directory containing documents to process
    """
    input_dir = Path(input_dir)

    # Filter formats if specified
    if formats:
        allowed_formats = {f".{fmt.lower().strip()}" for fmt in formats.split(",")}
        supported_formats = set(DocumentProcessor.PROCESSORS.keys())
        invalid_formats = allowed_formats - supported_formats
        if invalid_formats:
            raise click.ClickException(
                f"Unsupported format(s): {', '.join(invalid_formats)}. "
                f"Supported formats are: {DocumentProcessor.get_supported_formats()}"
            )
    else:
        allowed_formats = set(DocumentProcessor.PROCESSORS.keys())

    # Find all files with supported extensions
    files_to_process = []
    for ext in allowed_formats:
        files_to_process.extend(input_dir.glob(f"*{ext}"))

    if not files_to_process:
        logger.warning(
            f"No supported documents found in {input_dir}. "
            f"Looking for: {', '.join(allowed_formats)}"
        )
        return

    logger.info(f"Found {len(files_to_process)} documents to process")

    for file_path in files_to_process:
        try:
            process_single_file(file_path, project_id, agent_name, agent_type)
        except Exception as e:
            logger.error(f"Error processing {file_path.name}: {str(e)}")
            continue

    logger.info("Batch processing completed")

cli()

Docs2KG - Document to Knowledge Graph conversion tool.

Supports multiple document formats: PDF, DOCX, HTML, and EPUB.

Source code in Docs2KG/cli.py
41
42
43
44
45
46
47
48
49
50
51
52
@click.group()
def cli():
    """Docs2KG - Document to Knowledge Graph conversion tool.

    Supports multiple document formats: PDF, DOCX, HTML, and EPUB.
    """
    logger.info("Welcome to Docs2KG!")
    logger.info(f"Configuration loaded from: {os.environ.get('CONFIG_FILE')}")
    logger.info(f"Input directory: {PROJECT_CONFIG.data.input_dir}")
    logger.info(f"Output directory: {PROJECT_CONFIG.data.output_dir}")
    logger.info(f"Ontology directory: {PROJECT_CONFIG.data.ontology_dir}")
    logger.info("---")

list_formats()

List all supported document formats.

Source code in Docs2KG/cli.py
218
219
220
221
222
@cli.command()
def list_formats():
    """List all supported document formats."""
    supported_formats = DocumentProcessor.get_supported_formats()
    click.echo(f"Supported document formats: {supported_formats}")

neo4j(project_id, mode, neo4j_uri, neo4j_user, neo4j_password, reset_db)

Load data to Neo4j database.

Source code in Docs2KG/cli.py
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
@cli.command()
@click.argument("project_id", type=str)
# import or export mode
@click.option(
    "--mode",
    "-m",
    type=click.Choice(["import", "export", "load", "docker_start", "docker_stop"]),
    default="load",
    help="Mode of operation (import or export)",
)
@click.option(
    "--neo4j-uri",
    "-u",
    default="bolt://localhost:7687",
    help="URI for the Neo4j database",
)
@click.option(
    "--neo4j-user",
    "-U",
    default="neo4j",
    help="Username for the Neo4j database",
)
@click.option(
    "--neo4j-password",
    "-P",
    default="testpassword",
    help="Password for the Neo4j database",
)
@click.option(
    "--reset_db",
    "-r",
    is_flag=True,
    help="Reset the database before loading data",
    default=False,
)
def neo4j(project_id, mode, neo4j_uri, neo4j_user, neo4j_password, reset_db):
    """Load data to Neo4j database."""
    input_path = PROJECT_CONFIG.data.output_dir / "projects" / project_id / "layout"
    logger.info(f"Processing input: {input_path}")

    if mode == "docker_start":
        docker_compose = """
services:
  neo4j:
    image: neo4j:latest
    container_name: neo4j
    environment:
      - NEO4J_AUTH=neo4j/testpassword
      - NEO4JLABS_PLUGINS=["apoc"]
      - NEO4J_apoc_import_file_enabled=true
      - NEO4J_apoc_export_file_enabled=true
      - NEO4J_dbms_security_procedures_unrestricted=apoc.*
    ports:
      - 7474:7474
      - 7687:7687
    volumes:
      - neo4j_data:/data
      - neo4j_logs:/logs
      - neo4j_import:/import
      - neo4j_plugins:/plugins

volumes:
  neo4j_data:
  neo4j_logs:
  neo4j_import:
  neo4j_plugins:
    """

        # Write docker-compose file
        with open(PROJECT_CONFIG.data.output_dir / "docker-compose.yml", "w") as f:
            f.write(docker_compose)

        logger.info("Created docker-compose.yml file")

        try:
            # Try using docker compose first (newer syntax)
            subprocess.run(
                [
                    "docker",
                    "compose",
                    "-f",
                    (PROJECT_CONFIG.data.output_dir / "docker-compose.yml").as_posix(),
                    "up",
                    "-d",
                ],
                check=True,
            )
        except subprocess.CalledProcessError:
            try:
                # Fall back to docker-compose if the above fails
                subprocess.run(
                    [
                        "docker-compose",
                        "-f",
                        (
                            PROJECT_CONFIG.data.output_dir / "docker-compose.yml"
                        ).as_posix(),
                        "up",
                        "-d",
                    ],
                    check=True,
                )
            except subprocess.CalledProcessError as e:
                logger.error("Failed to start Docker container: %s", e)
                raise

        logger.info("Neo4j container is starting up")
        logger.info("Access Neo4j Browser at http://localhost:7474")
        return

    if mode == "docker_stop":
        try:
            # Try using docker compose first (newer syntax)
            subprocess.run(
                [
                    "docker",
                    "compose",
                    "-f",
                    (PROJECT_CONFIG.data.output_dir / "docker-compose.yml").as_posix(),
                    "down",
                ],
                check=True,
            )
        except subprocess.CalledProcessError:
            try:
                # Fall back to docker-compose if the above fails
                subprocess.run(
                    [
                        "docker-compose",
                        "-f",
                        (
                            PROJECT_CONFIG.data.output_dir / "docker-compose.yml"
                        ).as_posix(),
                        "down",
                    ],
                    check=True,
                )
            except subprocess.CalledProcessError as e:
                logger.error("Failed to stop Docker container: %s", e)
                raise

        logger.info("Neo4j container is stopping")
        return

    # Initialize Neo4j transformer
    transformer = Neo4jTransformer(
        project_id=project_id,
        uri=neo4j_uri,
        username=neo4j_user,
        password=neo4j_password,
        reset_database=reset_db,
    )

    if mode == "load":
        if input_path.is_dir():
            for file_path in input_path.glob("*.json"):
                # if it is schema.json, skip
                if file_path.name == "schema.json":
                    continue
                try:
                    transformer.transform_and_load(file_path)
                except Exception as e:
                    logger.error(f"Error loading {file_path.name}: {str(e)}")
                    logger.exception(e)
                    continue
        else:
            try:
                transformer.transform_and_load(input_path)
            except Exception as e:
                logger.error(f"Error loading {input_path.name}: {str(e)}")

    elif mode == "export":
        transformer.export()
    elif mode == "import":
        project_json_path = (
            PROJECT_CONFIG.data.output_dir
            / "projects"
            / project_id
            / "neo4j_export.json"
        )
        transformer.import_from_json(project_json_path)
    else:
        raise click.ClickException("Invalid mode of operation")

process_document(file_path, project_id, agent_name, agent_type)

Process a single document file.

FILE_PATH: Path to the document file (PDF, DOCX, HTML, or EPUB)

Source code in Docs2KG/cli.py
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
@cli.command()
@click.argument("file_path", type=click.Path(exists=True))
@click.option(
    "--project-id",
    "-p",
    default="default",
    help="Project ID for the knowledge graph construction",
)
@click.option(
    "--agent-name",
    "-n",
    default="phi3.5",
    help="Name of the agent to use for NER extraction",
)
@click.option(
    "--agent-type",
    "-t",
    default="ollama",
    help="Type of the agent to use for NER extraction",
)
def process_document(file_path, project_id, agent_name, agent_type):
    """Process a single document file.

    FILE_PATH: Path to the document file (PDF, DOCX, HTML, or EPUB)
    """
    file_path = Path(file_path)
    logger.info(f"Processing document: {file_path}")
    process_single_file(file_path, project_id, agent_name, agent_type)

process_single_file(file_path, project_id, agent_name, agent_type)

Process a single document file.

Source code in Docs2KG/cli.py
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
def process_single_file(
    file_path: Path, project_id: str, agent_name: str, agent_type: str
):
    """Process a single document file."""
    processor_class = DocumentProcessor.get_processor(file_path)
    if not processor_class:
        supported_formats = DocumentProcessor.get_supported_formats()
        raise click.ClickException(
            f"Unsupported file format: {file_path.suffix}. "
            f"Supported formats are: {supported_formats}"
        )

    # Step 1: Process document
    processor = processor_class(file_path=file_path)
    processor.process()

    # Step 2: Get markdown file path
    md_files = (
        PROJECT_CONFIG.data.output_dir
        / file_path.stem
        / processor_class.__name__
        / f"{file_path.stem}.md"
    )

    if not md_files.exists():
        logger.error(f"Markdown file not found: {md_files}")
        raise click.ClickException("Document processing failed")

    # Step 3: Construct Layout KG
    layout_kg_construction = LayoutKGConstruction(project_id)
    layout_kg_construction.construct(
        [{"content": md_files.read_text(), "filename": md_files.stem}]
    )

    # Step 4: Get JSON file path
    example_json = (
        PROJECT_CONFIG.data.output_dir
        / "projects"
        / project_id
        / "layout"
        / f"{file_path.stem}.json"
    )

    if not example_json.exists():
        logger.error(f"Layout KG JSON file not found: {example_json}")
        raise click.ClickException("Layout KG construction failed")

    # Step 5: Extract entities
    entity_extractor = NERSpacyMatcher(project_id)
    entity_extractor.construct_kg([example_json])

    # Step 6: Extract via prompt-based NER
    ner_extractor = NERLLMPromptExtractor(
        project_id=project_id, agent_name=agent_name, agent_type=agent_type
    )
    ner_extractor.construct_kg([example_json])

    logger.info(f"Successfully processed {file_path.name}")