Python 不可迭代进程的进度条

Python Progress Bar for non-iterable process

我正在使用这个 Notebook,其中 Apply DocumentClassifier 部分更改如下。

Jupyter 实验室,内核:conda_mxnet_latest_p37.

tqdm 是一个进度条包装器。它似乎在 for loopsCLI 上都有效。但是,我想在线使用它:

classified_docs = doc_classifier.predict(docs_to_classify)

这是一个迭代过程,但在引擎盖下。

如何将 tqdm 应用于此行?


代码单元格:

doc_dir = "GRIs/"  # contains 2 .pdfs

with open('filt_gri.txt', 'r') as filehandle:
    tags = [current_place.rstrip() for current_place in filehandle.readlines()]


doc_classifier = TransformersDocumentClassifier(model_name_or_path="cross-encoder/nli-distilroberta-base",
                                                task="zero-shot-classification",
                                                labels=tags,
                                                batch_size=2)

# convert to Document using a fieldmap for custom content fields the classification should run on
docs_to_classify = [Document.from_dict(d) for d in docs_sliding_window]

# classify using gpu, batch_size makes sure we do not run out of memory
classified_docs = doc_classifier.predict(docs_to_classify)

基于此TDS Article;所有 Python 进度条库都适用于 for loops。假设地,我可以更改 predict() 函数并附加到那里,但这实在是太麻烦了。

注意:如果确实有针对不可迭代 “可访问” 进程的解决方案,我很乐意删除此答案。