Path: blob/master/cpd5.0/notebooks/python_sdk/deployments/foundation_models/Use watsonx, and Google `flan-t5-xxl` to find sentiments of legal documents.ipynb
6405 views
Use watsonx, and Google flan-t5-xxl
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.11.
Learning goal
The goal of this notebook is to demonstrate how to use google/flan-t5-xxl
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 and import the ibm-watsonx-ai
and dependecies
Note: ibm-watsonx-ai
documentation can be found here.
Connection to WML
Authenticate the Watson Machine Learning service on IBM Cloud Pack for Data. You need to provide platform url
, your username
and api_key
.
Alternatively you can use username
and password
to authenticate WML services.
Defining the project id
The Foundation Model 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.
Download the legal documents
dataset.
Read the data.
Prepare dataset label map.
Inspect data sample.
Split the data into training and test sets.
List available models
All avaliable models are presented under ModelTypes class. For more information refer to documentation.
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 google/flan-t5-xxl
model.
Get the docs summaries.
Explore model output.
Get the true labels.
Get the prediction labels.
Calculate the accuracy score.
Summary and next steps
You successfully completed this notebook!.
You learned how to find sentiments of legal documents with Google's flan-t5-xxl
on watsonx.
Check out our Online Documentation for more samples, tutorials, documentation, how-tos, and blog posts.
Authors:
Mateusz Szewczyk, Software Engineer at Watson Machine Learning.
Copyright © 2023-2025 IBM. This notebook and its source code are released under the terms of the MIT License.