SmolDocling和數字化文件指南

SmolDocling和數字化文件指南

文章目录

  • 背景
  • 模型架構
  • SmolDocling演示
  • 先決條件
  • SmolDocling響應
  • SmolDocling的潛在用例
  • 小結

SmolDocling和數字化文件指南

有沒有想過,很少有 LLM 或某些工具能夠處理和理解包含多個表格和圖片的 PDF 檔案?它們很可能在底層使用了傳統的 OCR 或 VLM(視覺語言模型)。不過值得注意的是,傳統的 OCR 在識別影像中的手寫文字方面存在問題,甚至對不常見的字型或字元(例如研究論文中的複雜公式)也存在問題。VLM 在這方面做得很好,但它們可能難以理解表格資料的順序,也可能無法捕捉影像及其標題的空間關係。

那麼,解決方案是什麼呢?在這裡,我們將探索一個專注於解決所有這些問題的最新模型。SmolDocling 模型已在 Hugging Face 上公開發布。閒話少敘,讓我們開始深入瞭解吧。

背景

SmolDocling 是一個精巧但功能強大的 256MB 視覺語言模型,專為文件理解而設計。與重量級模型不同,它不需要大量的視訊記憶體來執行。它由一個視覺編碼器和一個緊湊型解碼器組成,經過訓練可生成 DocTags,這是一種 XML 風格的語言,用於對佈局、結構和內容進行編碼。其作者使用數百萬份包含公式、表格和程式碼片段的合成文件對其進行了訓練。另外值得注意的是,該模型構建於 Hugging Face 的 SmolVLM-256M 之上。在接下來的章節中,我們將更深入地瞭解其架構和演示。

模型架構

模型架構 

Source: Model Architecture of SmolDocling

從技術上講,SmolDocling 也是一個 VLM,但它擁有獨特的架構。SmolDocling 接收整頁文件影像,並使用視覺編碼器對其進行編碼,生成密集的視覺嵌入。然後,這些嵌入被投影並池化成固定數量的 token,以適應小型解碼器的輸入大小。同時,使用者提示被嵌入並與視覺特徵連線。然後,這個組合序列輸出一個結構化的 <doctag> token 流。結果是什麼?一個緊湊的、佈局感知的 XML 樣式 DocTags 序列,它同時捕獲內容和結構。現在,讓我們看看這個架構如何在演示中轉化為實際應用。

SmolDocling演示

先決條件

請務必建立您的 Hugging Face 帳戶並準備好您的訪問令牌,因為我們將使用 Hugging Face 進行此操作。

您可以在此處獲取您的訪問令牌

訪問令牌

注意:請確保您授予必要的許可權,例如訪問公共程式碼庫,並允許其進行推理呼叫。

我們使用管道載入模型(或者,您也可以選擇直接載入模型,這將在後續章節中進行探討)。

注意:如前所述,此模型一次處理文件的一張影像。您可以選擇使用此管道多次使用該模型來處理整個文件。

我將使用 Google Colab。請確保將執行時更改為 GPU:

Google Colab

from transformers import pipeline
pipe = pipeline("image-text-to-text", model="ds4sd/SmolDocling-256M-preview")
messages = [
{
"role": "user",
"content": [
{"type": "image", "url": "https://cdn.analyticsvidhya.com/wp-content/uploads/2024/05/Intro-1.jpg"},
{"type": "text", "text": "Which year was this conference held?"}
]
},
]
pipe(text=messages)

我提供了之前資料駭客峰會的圖片並問道:“Which year was this conference held?”

資料駭客峰會

SmolDocling響應

{'type': 'text', 'text': 'Which year was this conference held?'}]},
{'role': 'assistant', 'content': ' This conference was held in 2023.'}]}]

這是正確的嗎?如果你放大並仔細觀察,你會發現它確實是 DHS 2023。在視覺編碼器的幫助下,這個 256M 的引數似乎表現不錯。

DHS 2023

為了充分發揮它的潛力,你可以傳遞一個包含複雜影像和表格的完整文件作為練習。

現在讓我們嘗試使用另一種方法來訪問模型,即直接使用 transforms 模組載入它:

在這裡,我們將傳遞一個來自 SmolDocling 研究論文的影像片段,並從模型中獲取文件標籤作為輸出。

我們將傳遞給模型的影像:

傳遞給模型的影像

在繼續之前,請先安裝 docking 核心模組:

!pip install docling_core

載入模型並顯示以下提示:

from transformers import AutoProcessor, AutoModelForImageTextToText
from transformers.image_utils import load_image
image = load_image("/content/docling_screenshot.png")
processor = AutoProcessor.from_pretrained("ds4sd/SmolDocling-256M-preview")
model = AutoModelForImageTextToText.from_pretrained("ds4sd/SmolDocling-256M-preview")
messages = [
{
"role": "user",
"content": [
{"type": "image"},
{"type": "text", "text": "Convert this page to docling."}
]
}
]
prompt = processor.apply_chat_template(messages, add_generation_prompt=True)
inputs = processor(text=prompt, images=[image], return_tensors="pt")
generated_ids = model.generate(**inputs, max_new_tokens=8192)
prompt_length = inputs.input_ids.shape[1]
trimmed_generated_ids = generated_ids[:, prompt_length:]
doctags = processor.batch_decode(
trimmed_generated_ids,
skip_special_tokens=False,
)[0].lstrip()
print("DocTags output:\n", doctags)

顯示結果:

from docling_core.types.doc.document import DocTagsDocument
from docling_core.types.doc import DoclingDocument
doctags_doc = DocTagsDocument.from_doctags_and_image_pairs([doctags], [image])
doc = DoclingDocument.load_from_doctags(doctags_doc, document_name="MyDoc")
md = doc.export_to_markdown()
print(md)

SmolDocling 輸出:

Figure 1: SmolDocling/SmolVLM architecture. SmolDocling converts images of document pages to DocTags sequences. First, input images are encoded using a vision encoder and reshaped via projection and pooling. Then, the projected embeddings are concatenated with the text embeddings of the user prompt, possibly with interleaving. Finally, the sequence is used by an LLM to autoregressively predict the DocTags sequence.

<!– image –>

很高興看到 SmolDocling 談論 SmolDocling。文字內容似乎也很準確。思考這個模型的潛在用途很有趣。讓我們看幾個例子。

SmolDocling的潛在用例

作為視覺語言模型,SmolDocling 具有豐富的潛在用途,例如從結構化文件中提取資料,例如研究論文、財務報告和法律合同。

它甚至可以用於學術用途,例如將手寫筆記和答題副本數字化。在需要 OCR 或文件處理的應用程式中,還可以使用 SmolDocling 作為元件來建立流程。

小結

總而言之,SmolDocling 是一個精巧但實用的 256M 視覺語言模型,專為文件理解而設計。傳統的 OCR 難以處理手寫文字和不常見的字型,而 VLM 則經常丟失空間或表格上下文。這個緊湊的模型表現出色,並且有多種使用場景。如果您還沒有嘗試過這個模型,請嘗試一下,並在使用過程中遇到任何問題時告訴我。

評論留言