forked from NVIDIA/nv-ingest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipeline.py
713 lines (600 loc) · 24.1 KB
/
pipeline.py
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
49
50
51
52
53
54
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
113
114
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
143
144
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
216
217
218
219
220
221
222
223
224
225
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
# SPDX-FileCopyrightText: Copyright (c) 2024, NVIDIA CORPORATION & AFFILIATES.
# All rights reserved.
# SPDX-License-Identifier: Apache-2.0
import json
import logging
import math
import os
import typing
from datetime import datetime
import click
from morpheus.config import Config
from morpheus.config import CppConfig
from morpheus.config import PipelineModes
from morpheus.messages import ControlMessage
from morpheus.pipeline.pipeline import Pipeline
from morpheus.stages.general.linear_modules_source import LinearModuleSourceStage
from morpheus.stages.general.linear_modules_stage import LinearModulesStage
from morpheus.utils.logger import configure_logging
from pydantic import ValidationError
from nv_ingest.modules.injectors.metadata_injector import MetadataInjectorLoaderFactory
from nv_ingest.modules.sinks.redis_task_sink import RedisTaskSinkLoaderFactory
from nv_ingest.modules.sinks.vdb_task_sink import VDBTaskSinkLoaderFactory
from nv_ingest.modules.sources.redis_task_source import RedisTaskSourceLoaderFactory
from nv_ingest.modules.telemetry.job_counter import JobCounterLoaderFactory
from nv_ingest.modules.telemetry.otel_meter import OpenTelemetryMeterLoaderFactory
from nv_ingest.modules.telemetry.otel_tracer import OpenTelemetryTracerLoaderFactory
from nv_ingest.modules.transforms.embed_extractions import EmbedExtractionsLoaderFactory
from nv_ingest.modules.transforms.nemo_doc_splitter import NemoDocSplitterLoaderFactory
from nv_ingest.schemas.ingest_pipeline_config_schema import IngestPipelineConfigSchema
from nv_ingest.stages.docx_extractor_stage import generate_docx_extractor_stage
from nv_ingest.stages.filters import generate_dedup_stage
from nv_ingest.stages.filters import generate_image_filter_stage
from nv_ingest.stages.pdf_extractor_stage import generate_pdf_extractor_stage
from nv_ingest.stages.pptx_extractor_stage import generate_pptx_extractor_stage
from nv_ingest.stages.storages.image_storage_stage import ImageStorageStage
from nv_ingest.stages.transforms.image_caption_extraction import generate_caption_extraction_stage
from nv_ingest.util.converters.containers import merge_dict
from nv_ingest.util.logging.configuration import LogLevel
from nv_ingest.util.logging.configuration import configure_logging as configure_local_logging
from nv_ingest.util.schema.schema_validator import validate_schema
logger = logging.getLogger(__name__)
configure_local_logging(logger, os.getenv("INGEST_LOG_LEVEL", "INFO"))
def validate_positive(ctx, param, value):
if value <= 0:
raise click.BadParameter("must be a positive integer")
return value
def get_message_provider_config():
message_provider_host = os.environ.get("MESSAGE_CLIENT_HOST", "localhost")
message_provider_port = os.environ.get("MESSAGE_CLIENT_PORT", "6379")
logger.info(f"MESSAGE_CLIENT_HOST: {message_provider_host}")
logger.info(f"MESSAGE_CLIENT_PORT: {message_provider_port}")
return message_provider_host, message_provider_port
def get_caption_classifier_service():
triton_service_caption_classifier = os.environ.get(
"CAPTION_CLASSIFIER_GRPC_TRITON",
"",
)
triton_service_caption_classifier_name = os.environ.get(
"CAPTION_CLASSIFIER_MODEL_NAME",
"",
)
logger.info(f"CAPTION_CLASSIFIER_GRPC_TRITON: {triton_service_caption_classifier}")
return triton_service_caption_classifier, triton_service_caption_classifier_name
def get_yolox_service_table_detection():
grpc_endpoint = os.environ.get(
"TABLE_DETECTION_GRPC_TRITON",
"",
)
http_endpoint = os.environ.get(
"TABLE_DETECTION_HTTP_TRITON",
"",
)
auth_token = os.environ.get(
"NVIDIA_BUILD_API_KEY",
"",
) or os.environ.get(
"NGC_API_KEY",
"",
)
logger.info(f"TABLE_DETECTION_GRPC_TRITON: {grpc_endpoint}")
logger.info(f"TABLE_DETECTION_HTTP_TRITON: {http_endpoint}")
return grpc_endpoint, http_endpoint, auth_token
def get_paddle_service_table_detection():
grpc_endpoint = os.environ.get(
"PADDLE_GRPC_ENDPOINT",
"",
)
http_endpoint = os.environ.get(
"PADDLE_HTTP_ENDPOINT",
"",
)
auth_token = os.environ.get(
"NVIDIA_BUILD_API_KEY",
"",
) or os.environ.get(
"NGC_API_KEY",
"",
)
logger.info(f"PADDLE_GRPC_ENDPOINT: {grpc_endpoint}")
logger.info(f"PADDLE_HTTP_ENDPOINT: {http_endpoint}")
return grpc_endpoint, http_endpoint, auth_token
def get_deplot_service_table_detection():
grpc_endpoint = os.environ.get(
"DEPLOT_GRPC_ENDPOINT",
"",
)
http_endpoint = os.environ.get(
"DEPLOT_HTTP_ENDPOINT",
"",
)
auth_token = os.environ.get(
"NVIDIA_BUILD_API_KEY",
"",
) or os.environ.get(
"NGC_API_KEY",
"",
)
logger.info(f"DEPLOT_GRPC_ENDPOINT: {grpc_endpoint}")
logger.info(f"DEPLOT_HTTP_ENDPOINT: {http_endpoint}")
return grpc_endpoint, http_endpoint, auth_token
def get_cached_service_table_detection():
grpc_endpoint = os.environ.get(
"CACHED_GRPC_ENDPOINT",
"",
)
http_endpoint = os.environ.get(
"CACHED_HTTP_ENDPOINT",
"",
)
auth_token = os.environ.get(
"NVIDIA_BUILD_API_KEY",
"",
) or os.environ.get(
"NGC_API_KEY",
"",
)
logger.info(f"CACHED_GRPC_ENDPOINT: {grpc_endpoint}")
logger.info(f"CACHED_HTTP_ENDPOINT: {http_endpoint}")
return grpc_endpoint, http_endpoint, auth_token
def get_default_cpu_count():
default_cpu_count = os.environ.get("NV_INGEST_MAX_UTIL", int(max(1, math.floor(len(os.sched_getaffinity(0))))))
return default_cpu_count
def add_source_stage(pipe, morpheus_pipeline_config, ingest_config, message_provider_host, message_provider_port):
source_module_loader = RedisTaskSourceLoaderFactory.get_instance(
module_name="redis_listener",
module_config=ingest_config.get(
"redis_task_source",
{
"redis_client": {
"host": message_provider_host,
"port": message_provider_port,
}
},
),
)
source_stage = pipe.add_stage(
LinearModuleSourceStage(
morpheus_pipeline_config,
source_module_loader,
output_type=ControlMessage,
output_port_name="output",
)
)
return source_stage
def add_submitted_job_counter_stage(pipe, morpheus_pipeline_config, ingest_config):
submitted_job_counter_loader = JobCounterLoaderFactory.get_instance(
module_name="submitted_job_counter",
module_config=ingest_config.get(
"submitted_job_counter_module",
{
"name": "submitted_jobs",
},
),
)
submitted_job_counter_stage = pipe.add_stage(
LinearModulesStage(
morpheus_pipeline_config,
submitted_job_counter_loader,
input_type=ControlMessage,
output_type=ControlMessage,
input_port_name="input",
output_port_name="output",
)
)
return submitted_job_counter_stage
def add_metadata_injector_stage(pipe, morpheus_pipeline_config):
metadata_injector_loader = MetadataInjectorLoaderFactory.get_instance(
module_name="metadata_injection", module_config={}
)
metadata_injector_stage = pipe.add_stage(
LinearModulesStage(
morpheus_pipeline_config,
metadata_injector_loader,
input_type=ControlMessage,
output_type=ControlMessage,
input_port_name="input",
output_port_name="output",
)
)
return metadata_injector_stage
def add_pdf_extractor_stage(pipe, morpheus_pipeline_config, ingest_config, default_cpu_count):
yolox_grpc, yolox_http, yolox_auth = get_yolox_service_table_detection()
paddle_grpc, paddle_http, paddle_auth = get_paddle_service_table_detection()
deplot_grpc, deplot_http, deplot_auth = get_deplot_service_table_detection()
cached_grpc, cached_http, cached_auth = get_cached_service_table_detection()
pdf_content_extractor_config = ingest_config.get(
"pdf_content_extraction_module",
{
"pdfium_config": {
"cached_endpoints": (cached_grpc, cached_http),
"deplot_endpoints": (deplot_grpc, deplot_http),
"paddle_endpoints": (paddle_grpc, paddle_http),
"yolox_endpoints": (yolox_grpc, yolox_http),
"auth_token": yolox_auth, # All auth tokens are the same for the moment
}
},
)
pdf_extractor_stage = pipe.add_stage(
generate_pdf_extractor_stage(
morpheus_pipeline_config,
pdf_content_extractor_config,
pe_count=8,
task="extract",
task_desc="pdf_content_extractor",
)
)
return pdf_extractor_stage
def add_docx_extractor_stage(pipe, morpheus_pipeline_config, default_cpu_count):
docx_extractor_stage = pipe.add_stage(
generate_docx_extractor_stage(
morpheus_pipeline_config,
pe_count=1,
task="extract",
task_desc="docx_content_extractor",
)
)
return docx_extractor_stage
def add_pptx_extractor_stage(pipe, morpheus_pipeline_config, default_cpu_count):
pptx_extractor_stage = pipe.add_stage(
generate_pptx_extractor_stage(
morpheus_pipeline_config,
pe_count=1,
task="extract",
task_desc="pptx_content_extractor",
)
)
return pptx_extractor_stage
def add_image_dedup_stage(pipe, morpheus_pipeline_config, ingest_config, default_cpu_count):
image_dedup_config = ingest_config.get("dedup_module", {})
image_dedup_stage = pipe.add_stage(
generate_dedup_stage(
morpheus_pipeline_config,
image_dedup_config,
pe_count=2,
task="dedup",
task_desc="dedup_images",
)
)
return image_dedup_stage
def add_image_filter_stage(pipe, morpheus_pipeline_config, ingest_config, default_cpu_count):
image_filter_config = ingest_config.get("image_filter", {})
image_filter_stage = pipe.add_stage(
generate_image_filter_stage(
morpheus_pipeline_config,
image_filter_config,
pe_count=2,
task="filter",
task_desc="filter_images",
)
)
return image_filter_stage
def add_nemo_splitter_stage(pipe, morpheus_pipeline_config, ingest_config):
nemo_splitter_loader = NemoDocSplitterLoaderFactory.get_instance(
module_name="nemo_doc_splitter",
module_config=ingest_config.get("text_splitting_module", {}),
)
nemo_splitter_stage = pipe.add_stage(
LinearModulesStage(
morpheus_pipeline_config,
nemo_splitter_loader,
input_type=ControlMessage,
output_type=ControlMessage,
input_port_name="input",
output_port_name="output",
)
)
return nemo_splitter_stage
def add_image_caption_stage(pipe, morpheus_pipeline_config, ingest_config, default_cpu_count):
endpoint_url, model_name = get_caption_classifier_service()
image_caption_config = ingest_config.get(
"image_caption_extraction_module",
{
"caption_classifier_model_name": model_name,
"endpoint_url": endpoint_url,
},
)
image_caption_stage = pipe.add_stage(
generate_caption_extraction_stage(
morpheus_pipeline_config,
image_caption_config,
pe_count=2,
task="caption",
task_desc="caption_ext",
)
)
return image_caption_stage
def add_embed_extractions_stage(pipe, morpheus_pipeline_config, ingest_config):
api_key = os.getenv("NGC_API_KEY", "ngc_api_key")
embedding_nim_endpoint = os.getenv("EMBEDDING_NIM_ENDPOINT", "http://embedding:8000/v1")
embed_extractions_loader = EmbedExtractionsLoaderFactory.get_instance(
module_name="embed_extractions",
module_config=ingest_config.get(
"embed_extractions_module", {"api_key": api_key, "embedding_nim_endpoint": embedding_nim_endpoint}
),
)
embed_extractions_stage = pipe.add_stage(
LinearModulesStage(
morpheus_pipeline_config,
embed_extractions_loader,
input_type=ControlMessage,
output_type=ControlMessage,
input_port_name="input",
output_port_name="output",
)
)
return embed_extractions_stage
def add_image_storage_stage(pipe, morpheus_pipeline_config):
image_storage_stage = pipe.add_stage(ImageStorageStage(morpheus_pipeline_config))
return image_storage_stage
def add_sink_stage(pipe, morpheus_pipeline_config, ingest_config, message_provider_host, message_provider_port):
sink_module_loader = RedisTaskSinkLoaderFactory.get_instance(
module_name="redis_task_sink",
module_config=ingest_config.get(
"redis_task_sink",
{
"redis_client": {
"host": message_provider_host,
"port": message_provider_port,
}
},
),
)
sink_stage = pipe.add_stage(
LinearModulesStage(
morpheus_pipeline_config,
sink_module_loader,
input_type=typing.Any,
output_type=ControlMessage,
input_port_name="input",
output_port_name="output",
)
)
return sink_stage
def add_otel_tracer_stage(pipe, morpheus_pipeline_config, ingest_config):
endpoint = os.getenv("OTEL_EXPORTER_OTLP_ENDPOINT", "http://localhost:4317")
otel_tracer_loader = OpenTelemetryTracerLoaderFactory.get_instance(
module_name="otel_tracer",
module_config=ingest_config.get(
"otel_tracer_module",
{
"otel_endpoint": endpoint,
},
),
)
otel_tracer_stage = pipe.add_stage(
LinearModulesStage(
morpheus_pipeline_config,
otel_tracer_loader,
input_type=ControlMessage,
output_type=ControlMessage,
input_port_name="input",
output_port_name="output",
)
)
return otel_tracer_stage
def add_otel_meter_stage(pipe, morpheus_pipeline_config, ingest_config, message_provider_host, message_provider_port):
endpoint = os.getenv("OTEL_EXPORTER_OTLP_ENDPOINT", "http://localhost:4317")
otel_meter_loader = OpenTelemetryMeterLoaderFactory.get_instance(
module_name="otel_meter",
module_config=ingest_config.get(
"otel_meter_module",
{
"redis_client": {
"host": message_provider_host,
"port": message_provider_port,
},
"otel_endpoint": endpoint,
},
),
)
otel_meter_stage = pipe.add_stage(
LinearModulesStage(
morpheus_pipeline_config,
otel_meter_loader,
input_type=ControlMessage,
output_type=ControlMessage,
input_port_name="input",
output_port_name="output",
)
)
return otel_meter_stage
def add_completed_job_counter_stage(pipe, morpheus_pipeline_config, ingest_config):
completed_job_counter_loader = JobCounterLoaderFactory.get_instance(
module_name="completed_job_counter",
module_config=ingest_config.get(
"completed_job_counter_module",
{
"name": "completed_jobs",
},
),
)
completed_job_counter_stage = pipe.add_stage(
LinearModulesStage(
morpheus_pipeline_config,
completed_job_counter_loader,
input_type=ControlMessage,
output_type=ControlMessage,
input_port_name="input",
output_port_name="output",
)
)
return completed_job_counter_stage
def add_vdb_task_sink_stage(pipe, morpheus_pipeline_config, ingest_config):
milvus_endpoint = os.getenv("MILVUS_ENDPOINT", "http://milvus:19530")
vdb_task_sink_loader = VDBTaskSinkLoaderFactory.get_instance(
module_name="vdb_task_sink",
module_config=ingest_config.get(
"vdb_task_sink_module",
{
"service_kwargs": {
"uri": milvus_endpoint,
}
},
),
)
vdb_task_sink_stage = pipe.add_stage(
LinearModulesStage(
morpheus_pipeline_config,
vdb_task_sink_loader,
input_type=ControlMessage,
output_type=ControlMessage,
input_port_name="input",
output_port_name="output",
)
)
return vdb_task_sink_stage
def setup_ingestion_pipeline(
pipe: Pipeline, morpheus_pipeline_config: Config, ingest_config: typing.Dict[str, typing.Any]
):
message_provider_host, message_provider_port = get_message_provider_config()
default_cpu_count = get_default_cpu_count()
# Pre-processing stages
source_stage = add_source_stage(
pipe, morpheus_pipeline_config, ingest_config, message_provider_host, message_provider_port
)
submitted_job_counter_stage = add_submitted_job_counter_stage(pipe, morpheus_pipeline_config, ingest_config)
metadata_injector_stage = add_metadata_injector_stage(pipe, morpheus_pipeline_config)
# Primitive extraction
pdf_extractor_stage = add_pdf_extractor_stage(pipe, morpheus_pipeline_config, ingest_config, default_cpu_count)
docx_extractor_stage = add_docx_extractor_stage(pipe, morpheus_pipeline_config, default_cpu_count)
pptx_extractor_stage = add_pptx_extractor_stage(pipe, morpheus_pipeline_config, default_cpu_count)
# Post-processing
image_dedup_stage = add_image_dedup_stage(pipe, morpheus_pipeline_config, ingest_config, default_cpu_count)
image_filter_stage = add_image_filter_stage(pipe, morpheus_pipeline_config, ingest_config, default_cpu_count)
# Transforms and data synthesis
nemo_splitter_stage = add_nemo_splitter_stage(pipe, morpheus_pipeline_config, ingest_config)
embed_extractions_stage = add_embed_extractions_stage(pipe, morpheus_pipeline_config, ingest_config)
# Storage and output
image_storage_stage = add_image_storage_stage(pipe, morpheus_pipeline_config)
sink_stage = add_sink_stage(
pipe, morpheus_pipeline_config, ingest_config, message_provider_host, message_provider_port
)
vdb_task_sink_stage = add_vdb_task_sink_stage(pipe, morpheus_pipeline_config, ingest_config)
# Telemetry (Note: everything after the sync stage is out of the hot path, please keep it that way)
otel_tracer_stage = add_otel_tracer_stage(pipe, morpheus_pipeline_config, ingest_config)
otel_meter_stage = add_otel_meter_stage(
pipe, morpheus_pipeline_config, ingest_config, message_provider_host, message_provider_port
)
completed_job_counter_stage = add_completed_job_counter_stage(pipe, morpheus_pipeline_config, ingest_config)
# Add edges
pipe.add_edge(source_stage, submitted_job_counter_stage)
pipe.add_edge(submitted_job_counter_stage, metadata_injector_stage)
pipe.add_edge(metadata_injector_stage, pdf_extractor_stage)
pipe.add_edge(pdf_extractor_stage, docx_extractor_stage)
pipe.add_edge(docx_extractor_stage, pptx_extractor_stage)
pipe.add_edge(pptx_extractor_stage, image_dedup_stage)
pipe.add_edge(image_dedup_stage, image_filter_stage)
pipe.add_edge(image_filter_stage, nemo_splitter_stage)
pipe.add_edge(nemo_splitter_stage, embed_extractions_stage)
pipe.add_edge(embed_extractions_stage, image_storage_stage)
pipe.add_edge(image_storage_stage, vdb_task_sink_stage)
pipe.add_edge(vdb_task_sink_stage, sink_stage)
pipe.add_edge(sink_stage, otel_meter_stage)
pipe.add_edge(otel_meter_stage, otel_tracer_stage)
pipe.add_edge(otel_tracer_stage, completed_job_counter_stage)
def pipeline(morpheus_pipeline_config, ingest_config) -> float:
logger.info("Starting pipeline setup")
pipe = Pipeline(morpheus_pipeline_config)
start_abs = datetime.now()
setup_ingestion_pipeline(pipe, morpheus_pipeline_config, ingest_config)
end_setup = start_run = datetime.now()
setup_elapsed = (end_setup - start_abs).total_seconds()
logger.info(f"Pipeline setup completed in {setup_elapsed:.2f} seconds")
logger.info("Running pipeline")
pipe.run()
end_run = datetime.now()
run_elapsed = (end_run - start_run).total_seconds()
total_elapsed = (end_run - start_abs).total_seconds()
logger.info(f"Pipeline run completed in {run_elapsed:.2f} seconds")
logger.info(f"Total time elapsed: {total_elapsed:.2f} seconds")
return total_elapsed
@click.command()
@click.option(
"--ingest_config_path", type=str, envvar="NV_INGEST_CONFIG_PATH", help="Path to the JSON configuration file."
)
@click.option("--use_cpp", is_flag=True, help="Use C++ backend.")
@click.option("--pipeline_batch_size", default=256, type=int, help="Batch size for the pipeline.")
@click.option("--enable_monitor", is_flag=True, help="Enable monitoring.")
@click.option("--feature_length", default=512, type=int, help="Feature length.")
@click.option("--num_threads", default=get_default_cpu_count(), type=int, help="Number of threads.")
@click.option("--model_max_batch_size", default=256, type=int, help="Model max batch size.")
@click.option(
"--caption_batch_size",
default=8,
callback=validate_positive,
type=int,
help="Number of captions to process in a batch. Must be a positive integer.",
)
@click.option(
"--mode",
type=click.Choice([mode.value for mode in PipelineModes], case_sensitive=False),
default=PipelineModes.NLP.value,
help="Pipeline mode.",
)
@click.option(
"--log_level",
type=click.Choice([level.value for level in LogLevel], case_sensitive=False),
default="INFO",
show_default=True,
help="Log level.",
)
def cli(
ingest_config_path,
caption_batch_size,
use_cpp,
pipeline_batch_size,
enable_monitor,
feature_length,
num_threads,
model_max_batch_size,
mode,
log_level,
):
"""
Command line interface for configuring and running the pipeline with specified options.
"""
# Convert log level from string to logging level
log_level_mapping = {
"DEBUG": logging.DEBUG,
"INFO": logging.INFO,
"WARNING": logging.WARNING,
"ERROR": logging.ERROR,
"CRITICAL": logging.CRITICAL,
}
# Check for INGEST_LOG_LEVEL environment variable
env_log_level = os.getenv("INGEST_LOG_LEVEL")
if env_log_level:
log_level = env_log_level
log_level = log_level_mapping.get(log_level.upper(), logging.INFO)
logging.basicConfig(level=log_level)
configure_logging(log_level=log_level)
CppConfig.set_should_use_cpp(use_cpp)
morpheus_pipeline_config = Config()
morpheus_pipeline_config.debug = True if log_level == "DEBUG" else False
morpheus_pipeline_config.log_level = log_level
morpheus_pipeline_config.pipeline_batch_size = pipeline_batch_size
morpheus_pipeline_config.enable_monitor = enable_monitor
morpheus_pipeline_config.feature_length = feature_length
morpheus_pipeline_config.num_threads = num_threads
morpheus_pipeline_config.model_max_batch_size = model_max_batch_size
morpheus_pipeline_config.mode = PipelineModes[mode.upper()]
cli_ingest_config = {} # TODO: Create a config for CLI overrides -- not necessary yet.
if ingest_config_path:
ingest_config = validate_schema(ingest_config_path)
else:
ingest_config = {}
# Merge command-line options with file configuration
final_ingest_config = merge_dict(ingest_config, cli_ingest_config)
# Validate final configuration using Pydantic
try:
validated_config = IngestPipelineConfigSchema(**final_ingest_config)
click.echo(f"Configuration loaded and validated: {validated_config}")
except ValidationError as e:
click.echo(f"Validation error: {e}")
raise
logger.debug(f"Ingest Configuration:\n{json.dumps(final_ingest_config, indent=2)}")
logger.debug(f"Morpheus configuration:\n{morpheus_pipeline_config}")
pipeline(morpheus_pipeline_config, final_ingest_config)
if __name__ == "__main__":
cli()