Path: blob/master/cloud/notebooks/rest_api/deployments/foundation_models/Use watsonx, and Google `flan-ul2` to summarize Cybersecurity documents.ipynb
6405 views
Use watsonx, and Google flan-ul2
to summarize Cybersecurity 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 text summarization 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 ul2
model to summarize cybersecurity: SPEC5G Cellular Network Protocol.
Use case & dataset
5G is the 5th generation cellular network protocol. It is the state-of-the-art global wireless standard that enables an advanced kind of network designed to connect virtually everyone and everything with increased speed and reduced latency. Therefore, its development, analysis, and security are critical. However, all approaches to the 5G protocol development and security analysis, e.g., property extraction, protocol summarization, and semantic analysis of the protocol specifications and implementations are completely manual. To reduce such manual effort,foundation model are used to summarize the paragraphs automitically. The dataset that is used in this notebook has two columns which are paragraph and simplification(summary).
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 and import the datasets
and dependecies
Inferencing class
This cell defines a class that makes a REST API call to the watsonx Foundation Model inferencing API that we will use to generate output from the provided input. The class takes the access token created in the previous step, and uses it to make a REST API call with input, model id and model parameters. The response from the API call is returned as the cell output.
Action: Provide watsonx.ai Runtime url to work with watsonx.ai.
Define a Prompt
class for prompts generation.
watsonx API connection
This cell defines the credentials required to work with watsonx API for Foundation Model inferencing.
Action: Provide the IBM Cloud user API key. For details, see documentation.
Defining the project id
The API requires project id that provides the context for the call. We will obtain the id from the project in which this notebook runs:
Download the cybersecurity: SPEC5G Cellular Network Protocol
dataset.
Read the data.
Inspect data sample.
Check the sample text and summary length.
The original text lenght statistics.
The reference summary lenght statistics.
Split data to train and test
List available models
You need to specify model_id
that will be used for inferencing:
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.
Defining the model parameters
We need to provide a set of model parameters that will influence the result. Based on decoding strategy that ww have for the models, the parameters can change.
There are two decoding strategies: greedy
and sampling
.
We usually use greedy
for complaint classification, extraction and Q&A.
We usually use sampling
for content generation and summarization.
Generate the cybersecurity: SPEC5G Cellular Network Protocol summary using ul2
model.
Note: You might need to adjust model parameters
for different models or tasks, to do so please refer to documentation.
Initialize the Prompt
class.
Hint: Your authentication token might expire, if so please regenerate the access_token
reinitialize the Prompt
class.
Get the docs summaries.
Explore model output.
Score the model
Cosine Similarity
Note: To run the Score section for model scoring on the cybersecurity dataset please transform following markdown
cells to code
cells. Have in mind that scoring model on the whole test set can take significant amount of time.
In this sample notebook spacy
implementation of cosine similarity for en_core_web_md
corpus was used for cosine similarity calculation.
Tip: You might consider using bigger language corpus, different word embeddings and distance metrics for output summary scoring against the reference summary.
Get the true labels.
Get the prediction labels.
Use spacy
and en_core_web_md
corpus to calculate cosine similarity of generated and reference summaries.
Rouge Metric
Note: Rouge (Recall-Oriented Understudy for Gisting Evaluation) metric is a set of evaluation measures used in natural language processing (NLP) and specifically in text summarization tasks. Please refer to below link for more information: torchmetrics
Summary and next steps
You successfully completed this notebook!
You learned how to generate documents summaries with Google's flan-ul2
on watsonx.
Check out our Online Documentation for more samples, tutorials, documentation, how-tos, and blog posts.
Author: Kahila Mokhtari
Copyright © 2023-2025 IBM. This notebook and its source code are released under the terms of the MIT License.