Path: blob/master/cloud/notebooks/python_sdk/deployments/foundation_models/Use watsonx Text Extraction service to extract text from file.ipynb
6408 views
Use watsonx.ai Text Extraction service to extract text from file
Disclaimers
Use only Projects and Spaces that are available in watsonx context.
Notebook content
This notebook contains the steps and code demonstrating how to run a Text Extraction job using python SDK and then retrieve the results in the form of JSON file.
Some familiarity with Python is helpful. This notebook uses Python 3.11.
Learning goal
The purpose of this notebook is to demonstrate the usage a Text Extraction service and ibm-watsonx-ai
Python SDK to retrieve a text from file that is located at IBM Cloud Object Storage.
Contents
This notebook contains the following parts:
Set up the environment
Before you use the sample code in this notebook, you must perform the following setup tasks:
Create a watsonx.ai Runtime Service instance (a free plan is offered and information about how to create the instance can be found here).
Install required packages
Defining the watsonx.ai credentials
This cell defines the watsonx.ai credentials required to work with watsonx Foundation Model inferencing.
Action: Provide the IBM Cloud user API key. For details, see documentation.
Defining the project id
The Text Extraction service requires project id that provides the context for the call. We will obtain the id from the project in which this notebook runs. Otherwise, please provide the project id.
API Client initialization
The document, from which we are going to extract text, is located at IBM Cloud Object Storage (COS). In the following example we are going to use Granite Code Models paper as a source text document. Also, the final results file, which will contain extracted text and necessary metadata, will be placed in COS. Therefore, we use ibm_watsonx_ai.helpers.DataConnection
and ibm_watsonx_ai.helpers.S3Location
class to create a Python objects that will represent the references to the processed files. Please note that you have to create connection asset with your COS details (for detailed explanation how to do this see IBM Cloud Object Storage connection or check below cells).
Create connection to COS
You can skip this section if you already have connection asset with IBM Cloud Object Storage.
Upload file and create document and results reference
Finally, we can create Data Connection that represents document and results reference.
Since data connection for source and results files are ready, we can proceed to the text extraction run job step. To initialize Text Extraction manager we use TextExtractions
class.
When running job the steps for the text extraction pipeline can be specified. For more details about available steps see documentation. The list of steps available in sdk can be found below.
To view sample parameter values for the text extraction steps run get_example_values()
.
In our example we are going to use the following steps
Now, we can run Text Extraction job. Please note that to get results in a more readable format we set results_format
to "markdown"
. However, if you want to get a file with more detailed results please use "json"
.
We can list text extraction jobs using a proper list method.
Moreover, to get details of a particular text extraction request run following
Furthermore, to delete text extraction jub run use delete_job()
method.
Once the job extraction is completed we can download the results file and process it further.
Summary and next steps
You successfully completed this notebook!
You learned how to use TextExtractions
manager to run text extraction requests, check status of the submitted job and download a results file.
Check out our Online Documentation for more samples, tutorials, documentation, how-tos, and blog posts.
Authors:
Mateusz Świtała, Software Engineer at watsonx.ai.
Copyright © 2024-2025 IBM. This notebook and its source code are released under the terms of the MIT License.