Path: blob/master/cpd5.2/notebooks/python_sdk/deployments/foundation_models/Use watsonx, and `granite-guardian-3-2b` to find sentiments of legal documents.ipynb
6412 views
Use watsonx, and ibm/granite-guardian-3-2b
to analyze sentiments of legal documents
Disclaimers
Use only Projects and Spaces that are available in watsonx context.
Notebook content
This notebook contains the steps and code to demonstrate support of sentiment analysis in watsonx. It introduces commands for data retrieval and model testing.
Some familiarity with Python is helpful. This notebook uses Python 3.12.
Learning goal
The goal of this notebook is to demonstrate how to use ibm/granite-guardian-3-2b
model to analyze sentiments of legal documents.
Use case & dataset
One of the key use cases of legal sentiment analysis is in assisting legal professionals in predicting case outcomes. By analyzing the sentiment expressed in previous court decisions and related documents, sentiment analysis algorithms can identify patterns and correlations between the sentiment and the final verdict. This can help lawyers and judges in assessing the strength of legal arguments, evaluating the potential impact of public opinion on the case, and making more accurate predictions about the likely outcome of ongoing cases. The dataset consists of two colums; the phrases and the sentiments.
Contents
This notebook contains the following parts:
Install dependencies
Note: ibm-watsonx-ai
documentation can be found here.
Successfully installed wget-3.2
Successfully installed joblib-1.5.0 numpy-2.2.5 scikit-learn-1.6.1 scipy-1.15.3 threadpoolctl-3.6.0
Successfully installed anyio-4.9.0 certifi-2025.4.26 charset-normalizer-3.4.2 h11-0.16.0 httpcore-1.0.9 httpx-0.28.1 ibm-cos-sdk-2.14.0 ibm-cos-sdk-core-2.14.0 ibm-cos-sdk-s3transfer-2.14.0 ibm-watsonx-ai-1.3.13 idna-3.10 jmespath-1.0.1 lomond-0.3.3 pandas-2.2.3 pytz-2025.2 requests-2.32.2 sniffio-1.3.1 tabulate-0.9.0 typing_extensions-4.13.2 tzdata-2025.2 urllib3-2.4.0
Define credentials
Authenticate the watsonx.ai Runtime service on IBM Cloud Pak for Data. You need to provide the admin's username
and the platform url
.
Use the admin's api_key
to authenticate watsonx.ai Runtime services:
Alternatively you can use the admin's password
:
Working with projects
First of all, you need to create a project that will be used for your work. If you do not have a project created already, follow the steps below:
Open IBM Cloud Pak main page
Click all projects
Create an empty project
Copy
project_id
from url and paste it below
Action: Assign project ID below
Create APIClient
instance
Download the legal documents
dataset
Read the data
Replace numeric sentiment values with text labels
Inspect data sample
Split the data into training and test sets.
List available models
You need to specify model_id
that will be used for inferencing:
Defining the model parameters
You might need to adjust model parameters
for different models or tasks, to do so please refer to documentation.
Initialize the model
Initialize the ModelInference
class with previous set params.
Model's details
Define instructions for the model.
Prepare model inputs for zero-shot example - use below zero_shot_inputs
.
Prepare model inputs for few-shot examples - use below few_shot_inputs
.
Generate the sentiments of legal documents using ibm/granite-guardian-3-2b
model.
Get the docs summaries
Get the true labels
Get the predicted labels
Calculate the accuracy score
Summary and next steps
You successfully completed this notebook!
You learned how to find sentiments of legal documents with ibm/granite-guardian-3-2b
on watsonx.
Check out our Online Documentation for more samples, tutorials, documentation, how-tos, and blog posts.
Authors:
Mateusz Szewczyk, Software Engineer at watsonx.ai.
Copyright © 2023-2025 IBM. This notebook and its source code are released under the terms of the MIT License.