1. खास जानकारी
Document AI क्या है?
Document AI, दस्तावेज़ों को समझने में मदद करने वाला एक समाधान है. यह अनस्ट्रक्चर्ड डेटा (जैसे, दस्तावेज़, ईमेल, इनवॉइस, फ़ॉर्म वगैरह) को लेता है और डेटा को समझने, उसका विश्लेषण करने, और उसे इस्तेमाल करने में आसानी पैदा करता है. यह एपीआई, कॉन्टेंट क्लासिफ़िकेशन, इकाई एक्सट्रैक्शन, ऐडवांस खोज वगैरह के ज़रिए स्ट्रक्चर उपलब्ध कराता है.
इस लैब में, Python के साथ Document AI API का इस्तेमाल करके, ऑप्टिकल कैरेक्टर रिकग्निशन (ओसीआर) करने का तरीका बताया गया है.
हम ए.ए. मिल्ने के क्लासिक उपन्यास "विनी द पूह" की पीडीएफ़ फ़ाइल का इस्तेमाल करेंगे. यह हाल ही में अमेरिका में पब्लिक डोमेन का हिस्सा बन गई है. इस फ़ाइल को Google Books ने स्कैन और डिजिटाइज़ किया है.
आपको क्या सीखने को मिलेगा
- Document AI API को चालू करने का तरीका
- एपीआई अनुरोधों की पुष्टि करने का तरीका
- Python के लिए क्लाइंट लाइब्रेरी इंस्टॉल करने का तरीका
- ऑनलाइन और बैच प्रोसेसिंग एपीआई का इस्तेमाल करने का तरीका
- किसी PDF फ़ाइल से टेक्स्ट पार्स करने का तरीका
आपको इन चीज़ों की ज़रूरत होगी
- Google Cloud प्रोजेक्ट
- कोई ब्राउज़र, जैसे कि Chrome या Firefox
- Python (3.9+) का इस्तेमाल करने की जानकारी
सर्वे
इस ट्यूटोरियल का इस्तेमाल कैसे किया जाएगा?
Python के साथ अपने अनुभव को आप क्या रेटिंग देंगे?
Google Cloud की सेवाओं को इस्तेमाल करने के अपने अनुभव को आप क्या रेटिंग देंगे?
2. सेटअप और ज़रूरी शर्तें
अपने हिसाब से एनवायरमेंट सेट अप करना
- Cloud Console में साइन इन करें. इसके बाद, नया प्रोजेक्ट बनाएं या किसी मौजूदा प्रोजेक्ट का फिर से इस्तेमाल करें. (अगर आपके पास पहले से Gmail या Google Workspace खाता नहीं है, तो आपको एक खाता बनाना होगा.)



प्रोजेक्ट आईडी याद रखें. यह सभी Google Cloud प्रोजेक्ट के लिए एक यूनीक नाम होता है. (ऊपर दिया गया प्रोजेक्ट आईडी पहले ही इस्तेमाल किया जा चुका है. इसलिए, यह आपके लिए काम नहीं करेगा. माफ़ करें!). आपको यह आईडी बाद में PROJECT_ID के तौर पर देना होगा.
- इसके बाद, Google Cloud के संसाधनों का इस्तेमाल करने के लिए, आपको Cloud Console में बिलिंग चालू करनी होगी.
"सफ़ाई करना" सेक्शन में दिए गए निर्देशों का पालन ज़रूर करें. इस सेक्शन में, संसाधनों को बंद करने का तरीका बताया गया है, ताकि इस ट्यूटोरियल के बाद आपसे बिलिंग न की जाए. Google Cloud के नए उपयोगकर्ता, मुफ़्त में आज़माने के लिए 300 डॉलर के प्रोग्राम में शामिल हो सकते हैं.
Cloud Shell शुरू करें
Google Cloud को अपने लैपटॉप से रिमोटली ऐक्सेस किया जा सकता है. हालांकि, इस कोडलैब में Google Cloud Shell का इस्तेमाल किया गया है. यह क्लाउड में चलने वाला कमांड लाइन एनवायरमेंट है.
Cloud Shell चालू करें
- Cloud Console में, Cloud Shell चालू करें
पर क्लिक करें
अगर आपने पहले कभी Cloud Shell का इस्तेमाल नहीं किया है, तो आपको एक इंटरमीडिएट स्क्रीन (नीचे फ़ोल्ड की गई) दिखेगी. इसमें Cloud Shell के बारे में जानकारी दी गई होगी. अगर ऐसा है, तो जारी रखें पर क्लिक करें. इसके बाद, आपको यह स्क्रीन कभी नहीं दिखेगी. एक बार दिखने वाली स्क्रीन ऐसी दिखती है:
Cloud Shell को चालू करने और उससे कनेक्ट होने में कुछ ही सेकंड लगते हैं.
Cloud Shell, आपको क्लाउड में होस्ट की गई वर्चुअल मशीन का टर्मिनल ऐक्सेस देता है. वर्चुअल मशीन में, डेवलपमेंट के लिए ज़रूरी सभी टूल शामिल होते हैं. यह 5 जीबी की होम डायरेक्ट्री उपलब्ध कराता है और Google Cloud में चलता है. इससे नेटवर्क की परफ़ॉर्मेंस और पुष्टि करने की प्रोसेस बेहतर होती है. इस कोडलैब में ज़्यादातर काम, सिर्फ़ ब्राउज़र की मदद से किया जा सकता है.
Cloud Shell से कनेक्ट होने के बाद, आपको दिखेगा कि आपकी पुष्टि पहले ही हो चुकी है और प्रोजेक्ट को आपके प्रोजेक्ट आईडी पर पहले ही सेट कर दिया गया है.
- पुष्टि करें कि आपने Cloud Shell में पुष्टि कर ली है. इसके लिए, यह कमांड चलाएं:
gcloud auth list
कमांड आउटपुट
Credentialed Accounts
ACTIVE ACCOUNT
* <my_account>@<my_domain.com>
To set the active account, run:
$ gcloud config set account `ACCOUNT`
gcloud config list project
कमांड आउटपुट
[core] project = <PROJECT_ID>
अगर ऐसा नहीं है, तो इस कमांड का इस्तेमाल करके इसे सेट किया जा सकता है:
gcloud config set project <PROJECT_ID>
कमांड आउटपुट
Updated property [core/project].
3. Document AI API चालू करना
Document AI का इस्तेमाल शुरू करने से पहले, आपको एपीआई चालू करना होगा. इसके लिए, gcloud कमांड लाइन इंटरफ़ेस या Cloud Console का इस्तेमाल किया जा सकता है.
gcloud CLI का इस्तेमाल करना
- अगर Cloud Shell का इस्तेमाल नहीं किया जा रहा है, तो अपनी लोकल मशीन पर
gcloudCLI इंस्टॉल करें में दिया गया तरीका अपनाएं. - इन
gcloudकमांड का इस्तेमाल करके, एपीआई चालू किए जा सकते हैं.
gcloud services enable documentai.googleapis.com storage.googleapis.com
आपको कुछ ऐसा दिखेगा:
Operation "operations/..." finished successfully.
Cloud Console का इस्तेमाल करना
अपने ब्राउज़र में Cloud Console खोलें.
- कंसोल में सबसे ऊपर मौजूद खोज बार का इस्तेमाल करके, "Document AI API" खोजें. इसके बाद, अपने Google Cloud प्रोजेक्ट में एपीआई का इस्तेमाल करने के लिए, चालू करें पर क्लिक करें

- Google Cloud Storage API के लिए, पिछले चरण को दोहराएं.
अब Document AI का इस्तेमाल किया जा सकता है!
4. प्रोसेसर बनाना और उसकी जांच करना
आपको सबसे पहले, Document OCR प्रोसेसर का एक इंस्टेंस बनाना होगा. यह इंस्टेंस, डेटा निकालने का काम करेगा. यह काम, Cloud Console या Processor Management API का इस्तेमाल करके किया जा सकता है.
Cloud Console
- कंसोल में, Document AI Platform की खास जानकारी पर जाएं

- प्रोसेसर एक्सप्लोर करें पर क्लिक करें और दस्तावेज़ का ओसीआर
चुनें - इसे
codelab-ocrनाम दें. इसके अलावा, कोई ऐसा नाम भी दिया जा सकता है जो आपको याद रहे. इसके बाद, सूची में से सबसे मिलता-जुलता क्षेत्र चुनें. - प्रोसेसर बनाने के लिए, बनाएं पर क्लिक करें
- अपने प्रोसेसर आईडी को कॉपी करें. आपको इसका इस्तेमाल बाद में अपने कोड में करना होगा.

कंसोल में कोई दस्तावेज़ अपलोड करके, अपने प्रोसेसर को आज़माया जा सकता है. जांच के लिए दस्तावेज़ अपलोड करें पर क्लिक करें और पार्स करने के लिए कोई दस्तावेज़ चुनें.
यहां दी गई PDF फ़ाइल डाउनलोड करें. इसमें हमारे नॉवल के पहले तीन पेज शामिल हैं.

आपका आउटपुट ऐसा दिखना चाहिए: 
Python क्लाइंट लाइब्रेरी
Python क्लाइंट लाइब्रेरी की मदद से, Document AI प्रोसेसर मैनेज करने का तरीका जानने के लिए, इस कोडलैब को फ़ॉलो करें:
5. एपीआई अनुरोधों की पुष्टि करना
Document AI API से अनुरोध करने के लिए, आपको सेवा खाता इस्तेमाल करना होगा. सेवा खाता आपके प्रोजेक्ट से जुड़ा होता है. इसका इस्तेमाल Python क्लाइंट लाइब्रेरी, एपीआई अनुरोध करने के लिए करती है. किसी अन्य उपयोगकर्ता खाते की तरह, सेवा खाते को भी एक ईमेल पते से दिखाया जाता है. इस सेक्शन में, Cloud SDK का इस्तेमाल करके एक सेवा खाता बनाया जाएगा. इसके बाद, सेवा खाते के तौर पर पुष्टि करने के लिए ज़रूरी क्रेडेंशियल बनाए जाएंगे.
सबसे पहले, Cloud Shell खोलें और अपने PROJECT_ID के साथ एक एनवायरमेंट वैरिएबल सेट करें. इसका इस्तेमाल इस कोडलैब में किया जाएगा:
export GOOGLE_CLOUD_PROJECT=$(gcloud config get-value core/project)
इसके बाद, Document AI API को ऐक्सेस करने के लिए, नया सेवा खाता बनाएं. इसके लिए, इनका इस्तेमाल करें:
gcloud iam service-accounts create my-docai-sa \
--display-name "my-docai-service-account"
इसके बाद, अपने सेवा खाते को अपने प्रोजेक्ट में Document AI और Cloud Storage को ऐक्सेस करने की अनुमतियां दें.
gcloud projects add-iam-policy-binding ${GOOGLE_CLOUD_PROJECT} \
--member="serviceAccount:my-docai-sa@${GOOGLE_CLOUD_PROJECT}.iam.gserviceaccount.com" \
--role="roles/documentai.admin"
gcloud projects add-iam-policy-binding ${GOOGLE_CLOUD_PROJECT} \
--member="serviceAccount:my-docai-sa@${GOOGLE_CLOUD_PROJECT}.iam.gserviceaccount.com" \
--role="roles/storage.admin"
gcloud projects add-iam-policy-binding ${GOOGLE_CLOUD_PROJECT} \
--member="serviceAccount:my-docai-sa@${GOOGLE_CLOUD_PROJECT}.iam.gserviceaccount.com" \
--role="roles/serviceusage.serviceUsageConsumer"
इसके बाद, ऐसे क्रेडेंशियल बनाएं जिनका इस्तेमाल करके, आपका Python कोड आपके नए सेवा खाते के तौर पर लॉगिन कर सके. इन क्रेडेंशियल को बनाएं और इन्हें JSON फ़ाइल ~/key.json के तौर पर सेव करें. इसके लिए, यह निर्देश इस्तेमाल करें:
gcloud iam service-accounts keys create ~/key.json \
--iam-account my-docai-sa@${GOOGLE_CLOUD_PROJECT}.iam.gserviceaccount.com
आखिर में, GOOGLE_APPLICATION_CREDENTIALS एनवायरमेंट वैरिएबल सेट करें. इसका इस्तेमाल लाइब्रेरी, आपके क्रेडेंशियल ढूंढने के लिए करती है. पुष्टि करने के इस तरीके के बारे में ज़्यादा जानने के लिए, गाइड देखें. एनवायरमेंट वैरिएबल को, क्रेडेंशियल की उस JSON फ़ाइल के पूरे पाथ पर सेट किया जाना चाहिए जिसे आपने बनाया है. इसके लिए, यह तरीका अपनाएं:
export GOOGLE_APPLICATION_CREDENTIALS="/path/to/key.json"
6. क्लाइंट लाइब्रेरी इंस्टॉल करना
Document AI, Cloud Storage, और Document AI Toolbox के लिए Python क्लाइंट लाइब्रेरी इंस्टॉल करें:
pip3 install --upgrade google-cloud-documentai
pip3 install --upgrade google-cloud-storage
pip3 install --upgrade google-cloud-documentai-toolbox
आपको कुछ ऐसा दिखेगा:
... Installing collected packages: google-cloud-documentai Successfully installed google-cloud-documentai-2.15.0 . . Installing collected packages: google-cloud-storage Successfully installed google-cloud-storage-2.9.0 . . Installing collected packages: google-cloud-documentai-toolbox Successfully installed google-cloud-documentai-toolbox-0.6.0a0
अब Document AI API का इस्तेमाल किया जा सकता है!
7. सैंपल PDF डाउनलोड करें
हमारे पास एक सैंपल दस्तावेज़ है, जिसमें उपन्यास के पहले तीन पेज शामिल हैं.
इस लिंक का इस्तेमाल करके, PDF डाउनलोड किया जा सकता है. इसके बाद, इसे Cloud Shell इंस्टेंस पर अपलोड करें.
इसे हमारे सार्वजनिक Google Cloud Storage बकेट से भी डाउनलोड किया जा सकता है. इसके लिए, gsutil का इस्तेमाल करें.
gsutil cp gs://cloud-samples-data/documentai/codelabs/ocr/Winnie_the_Pooh_3_Pages.pdf .
8. ऑनलाइन प्रोसेसिंग का अनुरोध करना
इस चरण में, ऑनलाइन प्रोसेसिंग (सिंक्रोनस) एपीआई का इस्तेमाल करके, उपन्यास के पहले तीन पेजों को प्रोसेस किया जाएगा. यह तरीका, डिवाइस में सेव किए गए छोटे दस्तावेज़ों के लिए सबसे सही है. हर प्रोसेसर टाइप के लिए, ज़्यादा से ज़्यादा पेजों और फ़ाइल के साइज़ के बारे में जानने के लिए, प्रोसेसर की पूरी सूची देखें.
online_processing.py नाम की फ़ाइल बनाने के लिए, Cloud Shell Editor या अपनी लोकल मशीन पर मौजूद टेक्स्ट एडिटर का इस्तेमाल करें. इसके बाद, यहां दिया गया कोड इस्तेमाल करें.
YOUR_PROJECT_ID, YOUR_PROJECT_LOCATION, YOUR_PROCESSOR_ID, और FILE_PATH को अपने एनवायरमेंट के हिसाब से सही वैल्यू से बदलें.
online_processing.py
from google.api_core.client_options import ClientOptions
from google.cloud import documentai
PROJECT_ID = "YOUR_PROJECT_ID"
LOCATION = "YOUR_PROJECT_LOCATION" # Format is 'us' or 'eu'
PROCESSOR_ID = "YOUR_PROCESSOR_ID" # Create processor in Cloud Console
# The local file in your current working directory
FILE_PATH = "Winnie_the_Pooh_3_Pages.pdf"
# Refer to https://cloud.google.com/document-ai/docs/file-types
# for supported file types
MIME_TYPE = "application/pdf"
# Instantiates a client
docai_client = documentai.DocumentProcessorServiceClient(
client_options=ClientOptions(api_endpoint=f"{LOCATION}-documentai.googleapis.com")
)
# The full resource name of the processor, e.g.:
# projects/project-id/locations/location/processor/processor-id
# You must create new processors in the Cloud Console first
RESOURCE_NAME = docai_client.processor_path(PROJECT_ID, LOCATION, PROCESSOR_ID)
# Read the file into memory
with open(FILE_PATH, "rb") as image:
image_content = image.read()
# Load Binary Data into Document AI RawDocument Object
raw_document = documentai.RawDocument(content=image_content, mime_type=MIME_TYPE)
# Configure the process request
request = documentai.ProcessRequest(name=RESOURCE_NAME, raw_document=raw_document)
# Use the Document AI client to process the sample form
result = docai_client.process_document(request=request)
document_object = result.document
print("Document processing complete.")
print(f"Text: {document_object.text}")
कोड चलाएं. इससे टेक्स्ट एक्सट्रैक्ट हो जाएगा और कंसोल पर प्रिंट हो जाएगा.
हमारे सैंपल दस्तावेज़ का इस्तेमाल करने पर, आपको यह आउटपुट दिखेगा:
Document processing complete. Text: CHAPTER I IN WHICH We Are Introduced to Winnie-the-Pooh and Some Bees, and the Stories Begin Here is Edward Bear, coming downstairs now, bump, bump, bump, on the back of his head, behind Christopher Robin. It is, as far as he knows, the only way of coming downstairs, but sometimes he feels that there really is another way, if only he could stop bumping for a moment and think of it. And then he feels that perhaps there isn't. Anyhow, here he is at the bottom, and ready to be introduced to you. Winnie-the-Pooh. When I first heard his name, I said, just as you are going to say, "But I thought he was a boy?" "So did I," said Christopher Robin. "Then you can't call him Winnie?" "I don't." "But you said " ... Digitized by Google
9. एक साथ कई अनुरोध करने की सुविधा का इस्तेमाल करना
अब मान लें कि आपको पूरी नॉवल का टेक्स्ट पढ़ना है.
- ऑनलाइन प्रोसेसिंग में, भेजे जा सकने वाले पेजों की संख्या और फ़ाइल के साइज़ की सीमाएं होती हैं. साथ ही, यह सुविधा हर एपीआई कॉल के लिए सिर्फ़ एक दस्तावेज़ फ़ाइल की अनुमति देती है.
- बैच प्रोसेसिंग की मदद से, बड़ी या एक से ज़्यादा फ़ाइलों को एसिंक्रोनस तरीके से प्रोसेस किया जा सकता है.
इस चरण में, हम Document AI Batch Processing API की मदद से "Winnie the Pooh" नॉवेल को प्रोसेस करेंगे. इसके बाद, टेक्स्ट को Google Cloud Storage बकेट में सेव करेंगे.
बैच प्रोसेसिंग में, अनुरोधों को एसिंक्रोनस तरीके से मैनेज करने के लिए लंबे समय तक चलने वाली कार्रवाइयां का इस्तेमाल किया जाता है. इसलिए, हमें ऑनलाइन प्रोसेसिंग के मुकाबले अलग तरीके से अनुरोध करना होता है और आउटपुट पाना होता है. हालांकि, ऑनलाइन या बैच प्रोसेसिंग का इस्तेमाल करने पर, आउटपुट एक ही Document ऑब्जेक्ट फ़ॉर्मैट में होगा.
इस चरण में, Document AI को प्रोसेस करने के लिए खास दस्तावेज़ देने का तरीका बताया गया है. बाद के चरण में, दस्तावेज़ों की पूरी डायरेक्ट्री को प्रोसेस करने का तरीका बताया जाएगा.
Cloud Storage में PDF अपलोड करना
फ़िलहाल, batch_process_documents() तरीके से सिर्फ़ Google Cloud Storage से फ़ाइलें स्वीकार की जाती हैं. ऑब्जेक्ट स्ट्रक्चर के बारे में ज़्यादा जानकारी के लिए, documentai_v1.types.BatchProcessRequest देखें.
इस उदाहरण के लिए, हमारी सैंपल बकेट से सीधे फ़ाइल पढ़ी जा सकती है.
gsutil का इस्तेमाल करके, फ़ाइल को अपने बकेट में भी कॉपी किया जा सकता है...
gsutil cp gs://cloud-samples-data/documentai/codelabs/ocr/Winnie_the_Pooh.pdf gs://YOUR_BUCKET_NAME/
...या यहां दिए गए लिंक से, नॉवेल की सैंपल फ़ाइल डाउनलोड करें और इसे अपने बकेट में अपलोड करें.
एपीआई के आउटपुट को सेव करने के लिए, आपको GCS बकेट की भी ज़रूरत होगी.
स्टोरेज बकेट बनाने का तरीका जानने के लिए, Cloud Storage के दस्तावेज़ पढ़ें.
batch_process_documents() तरीके का इस्तेमाल करना
batch_processing.py नाम की एक फ़ाइल बनाएं और नीचे दिए गए कोड का इस्तेमाल करें.
YOUR_PROJECT_ID, YOUR_PROCESSOR_LOCATION, YOUR_PROCESSOR_ID, YOUR_INPUT_URI, और YOUR_OUTPUT_URI को अपने एनवायरमेंट के हिसाब से सही वैल्यू से बदलें.
पक्का करें कि YOUR_INPUT_URI सीधे तौर पर PDF फ़ाइल पर ले जाता हो. उदाहरण के लिए: gs://cloud-samples-data/documentai/codelabs/ocr/Winnie_the_Pooh.pdf.
batch_processing.py
"""
Makes a Batch Processing Request to Document AI
"""
import re
from google.api_core.client_options import ClientOptions
from google.api_core.exceptions import InternalServerError
from google.api_core.exceptions import RetryError
from google.cloud import documentai
from google.cloud import storage
# TODO(developer): Fill these variables before running the sample.
project_id = "YOUR_PROJECT_ID"
location = "YOUR_PROCESSOR_LOCATION" # Format is "us" or "eu"
processor_id = "YOUR_PROCESSOR_ID" # Create processor before running sample
gcs_output_uri = "YOUR_OUTPUT_URI" # Must end with a trailing slash `/`. Format: gs://bucket/directory/subdirectory/
processor_version_id = (
"YOUR_PROCESSOR_VERSION_ID" # Optional. Example: pretrained-ocr-v1.0-2020-09-23
)
# TODO(developer): If `gcs_input_uri` is a single file, `mime_type` must be specified.
gcs_input_uri = "YOUR_INPUT_URI" # Format: `gs://bucket/directory/file.pdf` or `gs://bucket/directory/`
input_mime_type = "application/pdf"
field_mask = "text,entities,pages.pageNumber" # Optional. The fields to return in the Document object.
def batch_process_documents(
project_id: str,
location: str,
processor_id: str,
gcs_input_uri: str,
gcs_output_uri: str,
processor_version_id: str = None,
input_mime_type: str = None,
field_mask: str = None,
timeout: int = 400,
):
# You must set the api_endpoint if you use a location other than "us".
opts = ClientOptions(api_endpoint=f"{location}-documentai.googleapis.com")
client = documentai.DocumentProcessorServiceClient(client_options=opts)
if not gcs_input_uri.endswith("/") and "." in gcs_input_uri:
# Specify specific GCS URIs to process individual documents
gcs_document = documentai.GcsDocument(
gcs_uri=gcs_input_uri, mime_type=input_mime_type
)
# Load GCS Input URI into a List of document files
gcs_documents = documentai.GcsDocuments(documents=[gcs_document])
input_config = documentai.BatchDocumentsInputConfig(gcs_documents=gcs_documents)
else:
# Specify a GCS URI Prefix to process an entire directory
gcs_prefix = documentai.GcsPrefix(gcs_uri_prefix=gcs_input_uri)
input_config = documentai.BatchDocumentsInputConfig(gcs_prefix=gcs_prefix)
# Cloud Storage URI for the Output Directory
gcs_output_config = documentai.DocumentOutputConfig.GcsOutputConfig(
gcs_uri=gcs_output_uri, field_mask=field_mask
)
# Where to write results
output_config = documentai.DocumentOutputConfig(gcs_output_config=gcs_output_config)
if processor_version_id:
# The full resource name of the processor version, e.g.:
# projects/{project_id}/locations/{location}/processors/{processor_id}/processorVersions/{processor_version_id}
name = client.processor_version_path(
project_id, location, processor_id, processor_version_id
)
else:
# The full resource name of the processor, e.g.:
# projects/{project_id}/locations/{location}/processors/{processor_id}
name = client.processor_path(project_id, location, processor_id)
request = documentai.BatchProcessRequest(
name=name,
input_documents=input_config,
document_output_config=output_config,
)
# BatchProcess returns a Long Running Operation (LRO)
operation = client.batch_process_documents(request)
# Continually polls the operation until it is complete.
# This could take some time for larger files
# Format: projects/{project_id}/locations/{location}/operations/{operation_id}
try:
print(f"Waiting for operation {operation.operation.name} to complete...")
operation.result(timeout=timeout)
# Catch exception when operation doesn"t finish before timeout
except (RetryError, InternalServerError) as e:
print(e.message)
# NOTE: Can also use callbacks for asynchronous processing
#
# def my_callback(future):
# result = future.result()
#
# operation.add_done_callback(my_callback)
# Once the operation is complete,
# get output document information from operation metadata
metadata = documentai.BatchProcessMetadata(operation.metadata)
if metadata.state != documentai.BatchProcessMetadata.State.SUCCEEDED:
raise ValueError(f"Batch Process Failed: {metadata.state_message}")
storage_client = storage.Client()
print("Output files:")
# One process per Input Document
for process in list(metadata.individual_process_statuses):
# output_gcs_destination format: gs://BUCKET/PREFIX/OPERATION_NUMBER/INPUT_FILE_NUMBER/
# The Cloud Storage API requires the bucket name and URI prefix separately
matches = re.match(r"gs://(.*?)/(.*)", process.output_gcs_destination)
if not matches:
print(
"Could not parse output GCS destination:",
process.output_gcs_destination,
)
continue
output_bucket, output_prefix = matches.groups()
# Get List of Document Objects from the Output Bucket
output_blobs = storage_client.list_blobs(output_bucket, prefix=output_prefix)
# Document AI may output multiple JSON files per source file
for blob in output_blobs:
# Document AI should only output JSON files to GCS
if blob.content_type != "application/json":
print(
f"Skipping non-supported file: {blob.name} - Mimetype: {blob.content_type}"
)
continue
# Download JSON File as bytes object and convert to Document Object
print(f"Fetching {blob.name}")
document = documentai.Document.from_json(
blob.download_as_bytes(), ignore_unknown_fields=True
)
# For a full list of Document object attributes, please reference this page:
# https://cloud.google.com/python/docs/reference/documentai/latest/google.cloud.documentai_v1.types.Document
# Read the text recognition output from the processor
print("The document contains the following text:")
print(document.text)
if __name__ == "__main__":
batch_process_documents(
project_id=project_id,
location=location,
processor_id=processor_id,
gcs_input_uri=gcs_input_uri,
gcs_output_uri=gcs_output_uri,
input_mime_type=input_mime_type,
field_mask=field_mask,
)
कोड चलाएं. आपको पूरी नॉवल का टेक्स्ट, एक्सट्रैक्ट किया गया और आपके कंसोल में प्रिंट किया गया दिखेगा.
इस प्रोसेस को पूरा होने में कुछ समय लग सकता है, क्योंकि फ़ाइल का साइज़ पिछले उदाहरण से काफ़ी बड़ा है. (ओह, परेशानी...)
हालांकि, Batch Processing API की मदद से, आपको एक ऑपरेशन आईडी मिलेगा. इसका इस्तेमाल, टास्क पूरा होने के बाद GCS से आउटपुट पाने के लिए किया जा सकता है.
आपका आउटपुट कुछ ऐसा दिखना चाहिए:
Waiting for operation projects/PROJECT_NUMBER/locations/LOCATION/operations/OPERATION_NUMBER to complete... Document processing complete. Fetching docai-output/OPERATION_NUMBER/0/Winnie_the_Pooh-0.json Fetching docai-output/OPERATION_NUMBER/0/Winnie_the_Pooh-1.json Fetching docai-output/OPERATION_NUMBER/0/Winnie_the_Pooh-10.json Fetching docai-output/OPERATION_NUMBER/0/Winnie_the_Pooh-11.json Fetching docai-output/OPERATION_NUMBER/0/Winnie_the_Pooh-12.json Fetching docai-output/OPERATION_NUMBER/0/Winnie_the_Pooh-13.json Fetching docai-output/OPERATION_NUMBER/0/Winnie_the_Pooh-14.json Fetching docai-output/OPERATION_NUMBER/0/Winnie_the_Pooh-15.json Fetching docai-output/OPERATION_NUMBER/0/Winnie_the_Pooh-16.json Fetching docai-output/OPERATION_NUMBER/0/Winnie_the_Pooh-17.json Fetching docai-output/OPERATION_NUMBER/0/Winnie_the_Pooh-18.json Fetching docai-output/OPERATION_NUMBER/0/Winnie_the_Pooh-2.json Fetching docai-output/OPERATION_NUMBER/0/Winnie_the_Pooh-3.json Fetching docai-output/OPERATION_NUMBER/0/Winnie_the_Pooh-4.json Fetching docai-output/OPERATION_NUMBER/0/Winnie_the_Pooh-5.json Fetching docai-output/OPERATION_NUMBER/0/Winnie_the_Pooh-6.json Fetching docai-output/OPERATION_NUMBER/0/Winnie_the_Pooh-7.json Fetching docai-output/OPERATION_NUMBER/0/Winnie_the_Pooh-8.json Fetching docai-output/OPERATION_NUMBER/0/Winnie_the_Pooh-9.json This is a reproduction of a library book that was digitized by Google as part of an ongoing effort to preserve the information in books and make it universally accessible. TM Google books https://books.google.com ..... He nodded and went out ... and in a moment I heard Winnie-the-Pooh -bump, bump, bump-go-ing up the stairs behind him. Digitized by Google
10. किसी डायरेक्ट्री के लिए बैच प्रोसेसिंग का अनुरोध करना
कभी-कभी, आपको हर दस्तावेज़ को अलग-अलग लिस्ट किए बिना, दस्तावेज़ों की पूरी डायरेक्ट्री को प्रोसेस करना पड़ सकता है. batch_process_documents() तरीके से, खास दस्तावेज़ों की सूची या डायरेक्ट्री पाथ का इनपुट दिया जा सकता है.
इस चरण में, दस्तावेज़ फ़ाइलों की पूरी डायरेक्ट्री को प्रोसेस करने का तरीका बताया गया है. ज़्यादातर कोड, पिछले चरण की तरह ही काम करता है. सिर्फ़ BatchProcessRequest के साथ भेजे गए GCS यूआरआई में अंतर होता है.
हमारे सैंपल बकेट में एक डायरेक्ट्री है. इसमें उपन्यास के कई पेज अलग-अलग फ़ाइलों में मौजूद हैं.
gs://cloud-samples-data/documentai/codelabs/ocr/multi-document/
फ़ाइलों को सीधे पढ़ा जा सकता है या उन्हें अपने Cloud Storage बकेट में कॉपी किया जा सकता है.
पिछले चरण के कोड को फिर से चलाएं. साथ ही, YOUR_INPUT_URI को Cloud Storage में मौजूद किसी डायरेक्ट्री से बदलें.
कोड चलाएं. आपको Cloud Storage डायरेक्ट्री में मौजूद सभी दस्तावेज़ फ़ाइलों से निकाला गया टेक्स्ट दिखेगा.
आपका आउटपुट कुछ ऐसा दिखना चाहिए:
Waiting for operation projects/PROJECT_NUMBER/locations/LOCATION/operations/OPERATION_NUMBER to complete... Document processing complete. Fetching docai-output/OPERATION_NUMBER/0/Winnie_the_Pooh_Page_0-0.json Fetching docai-output/OPERATION_NUMBER/1/Winnie_the_Pooh_Page_1-0.json Fetching docai-output/OPERATION_NUMBER/2/Winnie_the_Pooh_Page_10-0.json Fetching docai-output/OPERATION_NUMBER/3/Winnie_the_Pooh_Page_12-0.json Fetching docai-output/OPERATION_NUMBER/4/Winnie_the_Pooh_Page_16-0.json Fetching docai-output/OPERATION_NUMBER/5/Winnie_the_Pooh_Page_7-0.json Introduction (I₂ F YOU happen to have read another book about Christopher Robin, you may remember th CHAPTER I IN WHICH We Are Introduced to Winnie-the-Pooh and Some Bees, and the Stories Begin HERE is 10 WINNIE-THE-POOH "I wonder if you've got such a thing as a balloon about you?" "A balloon?" "Yes, 12 WINNIE-THE-POOH and you took your gun with you, just in case, as you always did, and Winnie-the-P 16 WINNIE-THE-POOH this song, and one bee sat down on the nose of the cloud for a moment, and then g WE ARE INTRODUCED 7 "Oh, help!" said Pooh, as he dropped ten feet on the branch below him. "If only
11. Document AI Toolbox की मदद से, बैच प्रोसेसिंग के जवाब को मैनेज करना
Cloud Storage के साथ इंटिग्रेट करने की वजह से, बैच प्रोसेसिंग को पूरा करने के लिए कई चरणों को पूरा करना पड़ता है. इनपुट दस्तावेज़ के साइज़ के हिसाब से, Document आउटपुट को कई .json फ़ाइलों में "शार्ड" भी किया जा सकता है.
Document AI टूलबॉक्स Python SDK को Document AI के साथ पोस्ट-प्रोसेसिंग और अन्य सामान्य टास्क को आसान बनाने के लिए बनाया गया था. इस लाइब्रेरी को Document AI क्लाइंट लाइब्रेरी के साथ इस्तेमाल करने के लिए बनाया गया है. इसे Document AI क्लाइंट लाइब्रेरी के विकल्प के तौर पर इस्तेमाल नहीं किया जा सकता. पूरी जानकारी के लिए, रेफ़रंस दस्तावेज़ पर जाएं.
इस चरण में, Document AI Toolbox का इस्तेमाल करके, बैच प्रोसेसिंग का अनुरोध करने और आउटपुट पाने का तरीका बताया गया है.
batch_processing_toolbox.py
"""
Makes a Batch Processing Request to Document AI using Document AI Toolbox
"""
from google.api_core.client_options import ClientOptions
from google.cloud import documentai
from google.cloud import documentai_toolbox
# TODO(developer): Fill these variables before running the sample.
project_id = "YOUR_PROJECT_ID"
location = "YOUR_PROCESSOR_LOCATION" # Format is "us" or "eu"
processor_id = "YOUR_PROCESSOR_ID" # Create processor before running sample
gcs_output_uri = "YOUR_OUTPUT_URI" # Must end with a trailing slash `/`. Format: gs://bucket/directory/subdirectory/
processor_version_id = (
"YOUR_PROCESSOR_VERSION_ID" # Optional. Example: pretrained-ocr-v1.0-2020-09-23
)
# TODO(developer): If `gcs_input_uri` is a single file, `mime_type` must be specified.
gcs_input_uri = "YOUR_INPUT_URI" # Format: `gs://bucket/directory/file.pdf`` or `gs://bucket/directory/``
input_mime_type = "application/pdf"
field_mask = "text,entities,pages.pageNumber" # Optional. The fields to return in the Document object.
def batch_process_toolbox(
project_id: str,
location: str,
processor_id: str,
gcs_input_uri: str,
gcs_output_uri: str,
processor_version_id: str = None,
input_mime_type: str = None,
field_mask: str = None,
):
# You must set the api_endpoint if you use a location other than "us".
opts = ClientOptions(api_endpoint=f"{location}-documentai.googleapis.com")
client = documentai.DocumentProcessorServiceClient(client_options=opts)
if not gcs_input_uri.endswith("/") and "." in gcs_input_uri:
# Specify specific GCS URIs to process individual documents
gcs_document = documentai.GcsDocument(
gcs_uri=gcs_input_uri, mime_type=input_mime_type
)
# Load GCS Input URI into a List of document files
gcs_documents = documentai.GcsDocuments(documents=[gcs_document])
input_config = documentai.BatchDocumentsInputConfig(gcs_documents=gcs_documents)
else:
# Specify a GCS URI Prefix to process an entire directory
gcs_prefix = documentai.GcsPrefix(gcs_uri_prefix=gcs_input_uri)
input_config = documentai.BatchDocumentsInputConfig(gcs_prefix=gcs_prefix)
# Cloud Storage URI for the Output Directory
gcs_output_config = documentai.DocumentOutputConfig.GcsOutputConfig(
gcs_uri=gcs_output_uri, field_mask=field_mask
)
# Where to write results
output_config = documentai.DocumentOutputConfig(gcs_output_config=gcs_output_config)
if processor_version_id:
# The full resource name of the processor version, e.g.:
# projects/{project_id}/locations/{location}/processors/{processor_id}/processorVersions/{processor_version_id}
name = client.processor_version_path(
project_id, location, processor_id, processor_version_id
)
else:
# The full resource name of the processor, e.g.:
# projects/{project_id}/locations/{location}/processors/{processor_id}
name = client.processor_path(project_id, location, processor_id)
request = documentai.BatchProcessRequest(
name=name,
input_documents=input_config,
document_output_config=output_config,
)
# BatchProcess returns a Long Running Operation (LRO)
operation = client.batch_process_documents(request)
# Operation Name Format: projects/{project_id}/locations/{location}/operations/{operation_id}
documents = documentai_toolbox.document.Document.from_batch_process_operation(
location=location, operation_name=operation.operation.name
)
for document in documents:
# Read the text recognition output from the processor
print("The document contains the following text:")
# Truncated at 100 characters for brevity
print(document.text[:100])
if __name__ == "__main__":
batch_process_toolbox(
project_id=project_id,
location=location,
processor_id=processor_id,
gcs_input_uri=gcs_input_uri,
gcs_output_uri=gcs_output_uri,
input_mime_type=input_mime_type,
field_mask=field_mask,
)
12. बधाई हो
आपने ऑनलाइन प्रोसेसिंग, बैच प्रोसेसिंग, और Document AI टूलबॉक्स का इस्तेमाल करके, किसी उपन्यास से टेक्स्ट निकालने के लिए Document AI का इस्तेमाल किया है.
हमारा सुझाव है कि आप अन्य दस्तावेज़ों का इस्तेमाल करके देखें. साथ ही, प्लैटफ़ॉर्म पर उपलब्ध अन्य प्रोसेसर के बारे में जानें.
खाली करने के लिए जगह
इस ट्यूटोरियल में इस्तेमाल किए गए संसाधनों के लिए, अपने Google Cloud खाते से शुल्क न लिए जाने के लिए:
- Cloud Console में, संसाधन मैनेज करें पेज पर जाएं.
- प्रोजेक्ट की सूची में, अपना प्रोजेक्ट चुनें. इसके बाद, मिटाएं पर क्लिक करें.
- डायलॉग बॉक्स में, प्रोजेक्ट आईडी टाइप करें. इसके बाद, प्रोजेक्ट मिटाने के लिए बंद करें पर क्लिक करें.
ज़्यादा जानें
इन फ़ॉलो-अप कोडलैब की मदद से, Document AI के बारे में ज़्यादा जानें.
- Document AI की मदद से फ़ॉर्म पार्स करना (Python)
- Document AI (Python) के साथ खास प्रोसेसर
- Python की मदद से Document AI प्रोसेसर मैनेज करना
- Document AI: ह्यूमन इन द लूप
संसाधन
- द फ़्यूचर ऑफ़ डॉक्यूमेंट्स - YouTube प्लेलिस्ट
- Document AI से जुड़े दस्तावेज़
- Document AI Python क्लाइंट लाइब्रेरी
- Document AI के सैंपल की रिपॉज़िटरी
लाइसेंस
इस काम के लिए, Creative Commons एट्रिब्यूशन 2.0 जेनेरिक लाइसेंस के तहत लाइसेंस मिला है.