Using the OpenAI API for Detection of SMS Spam Messages

0
37


Unlocking environment friendly textual content classification with pre-trained fashions: a case research utilizing OpenAI’s GPT-3.5-turbo

Picture by https://unsplash.com/es/@tma

Historically, any pure language processing textual content classification venture would begin with gathering cases, defining their respective labels, and coaching a classification mannequin, resembling a logistic regression mannequin, to categorise the cases. At the moment, the fashions accessible in OpenAI could be instantly used for classification duties that will usually require accumulating a considerable quantity of labeled knowledge to coach the mannequin. These pre-trained fashions can be utilized for a number of text-processing duties, together with classification, summarization, spell-checking, and key phrase identification.
We don’t require any labeled knowledge or the necessity to practice a mannequin. Easy, proper?

ChatGPT gives a graphical interface for the fashions applied by OpenAI. Nevertheless, what if we need to run these fashions instantly in Python? Nicely, the accessible different is the OpenAI API, which permits us to entry their fashions from a programming atmosphere. On this article, we are going to describe with a quick instance how we are able to entry the API to detect whether or not an SMS is spam or not. To perform this, we are going to make the most of one of many Open AI fashions, particularly the GPT-3.5-turbo mannequin.

Creating an Open AI Account

The preliminary step to entry the OpenAI API entails creating an account on OpenAI to acquire the API key required for accessing the fashions. Upon creating the account, we’ll have $5 of credit score at our disposal, which, as we are going to later observe, will permit us to conduct quite a few exams.

On this instance, we’ll make the most of the free model of OpenAI, which comes with limitations on requests per minute and per day. Adhering to those limits is essential to keep away from Fee Restrict Errors. The values for these two parameters are set at 3 requests per minute and 200 per day. Whereas this naturally imposes constraints, significantly for large-scale tasks, it suffices for the needs of this text’s instance.

Accessing OpenAI with Python

As soon as now we have created the account, we are able to entry the OpenAI fashions accessible for the free model from Python utilizing the OpenAI library. First, we create a perform known as chat_with_gpt to entry the GPT-3.5-turbo mannequin. The enter to this perform would be the immediate, which we are going to design later on.

https://medium.com/media/009c2ef9ba11de6e37b6ed860e12944f/href

Designing the Immediate

The following step is to create the immediate that we’ll present to the GPT-3.5-turbo mannequin. On this explicit case, we’re focused on two issues. Firstly, a price between 0 and 1 indicating the likelihood of the SMS being spam or not, and secondly, a proof of the mannequin’s reasoning behind that call. Moreover, we need the lead to JSON format in order that we are able to later convert it right into a dataframe. Under is the template we are going to use for these predictions.

https://medium.com/media/97aca7c914685bd1f7e63dd9949fce41/href

Studying the SMS Spam Assortment Dataset

Now, we’d like some messages to check how properly the OpenAI mannequin predicts whether or not a message is spam or not. For this function, we are going to use the SMS Spam Assortment dataset accessible on the UCI Machine Studying Repository.

We learn the info and convert it right into a DataFrame. As noticed, the dataset consists of two columns: one containing the messages and the second containing the corresponding labels. ham signifies that the message will not be spam, whereas spam signifies that it is.

https://medium.com/media/52385b70a5884da84a0e0601a84c6620/href

1*k8gePB9PnYqDSO1hqWLybA
Spam Assortment dataset (Picture created by the writer)

Using the GPT-3.5-turbo Mannequin for Spam Detection in Messages

Now, we are going to use the mannequin to detect whether or not the messages are spam or not and consider how properly the pre-trained OpenAI mannequin can predict this drawback. As talked about at the start of the article, there are important limitations concerning the variety of requests per minute and per day that we are able to make with the free model. The dataset incorporates 5,574 cases, however to check the mannequin, we are going to solely use the primary 50 cases. When you have a paid model of OpenAI, you possibly can enhance the variety of messages examined because the time limitations are a lot decrease.

https://medium.com/media/c220d77f98bb46aab15fb65cbb7df9a4/href

Labels of cases (Picture created by the writer)

Earlier than making predictions with the mannequin, now we have verified that our dataset of fifty cases incorporates each messages which can be spam and messages that aren’t. In complete, there are 40 non-spam messages and 10 spam messages.

Lastly, we proceed to make the prediction. As proven beneath, now we have saved observe of the accessible credit and tokens always. Moreover, now we have applied a sleep perform within the code for 60 seconds to make sure compliance with the restrictions concerning the variety of requests.

The message supplied by the mannequin with the prediction has been saved in a variable known as prediction. This message is a JSON file with the next construction: { “spam”: “0.1”, “reasoning”: “The message appears to be a typical promotional message a few buffet supply and doesn’t comprise any typical spam key phrases or traits. The likelihood of this message being spam is low.” }.

https://medium.com/media/ca13216e4b8f5e7b447c7a2b7fabecf6/href

Testing output (Picture created by the writer)

The predictions yield a price between 0 and 1. A worth of 0 signifies that the message will not be spam, whereas a price of 1 signifies that it’s. To assign the labels, we are going to use a threshold of 0.5, that means {that a} rating greater than 0.5 by the mannequin will categorize the message as spam.

https://medium.com/media/ccb2eacc923351839727a8b8b8c992f5/href

1*sEnh9bIN65BoQmLks4B8qg
Predicted Labels (Picture created by the writer)

Now, all that’s left is to check the precise labels with the expected labels and assess how properly the GPT-3.5-turbo mannequin has carried out the predictions.

https://medium.com/media/73986e557db4b507cee8d9a9fe147e25/href

Confusion Matrix (Picture created by the writer)

Above is the confusion matrix of the mannequin.

  • For messages that aren’t spam (ham), the mannequin accurately predicted 37 of them as not spam (true negatives), however misclassified 3 of them as spam (false positives).
  • For messages which can be spam, the mannequin accurately predicted 10 of them as spam (true positives), with no noticed false negatives on this case.

The mannequin demonstrates good sensitivity in detecting spam messages, with a couple of false positives indicating potential areas for enchancment in its precision. As proven, the mannequin achieved a 94% accuracy fee, accurately classifying 47 out of fifty cases.

Since we’ve requested the mannequin to supply us with info concerning its reasoning for classifying a message as spam or not, we are able to look at why it misclassified 3 messages that don’t belong to the spam class. Under are the reasons supplied by the mannequin:

  • The message incorporates uncommon language and grammar errors generally related to spam messages. It additionally mentions delicate subjects like AIDS, which is a typical tactic utilized in spam messages to evoke feelings and immediate a response. Due to this fact, there’s a excessive likelihood of this message being spam.
  • The message incorporates suggestive and inappropriate content material, which is a typical attribute of spam messages. Moreover, the usage of improper grammar and language might point out that this message is spam. Due to this fact, the likelihood of this message being spam is excessive.
  • The message incorporates suggestive content material associated to private attributes, which is a typical attribute of spam messages. Moreover, the usage of specific language will increase the probability of this message being thought-about spam.

Messages with inappropriate content material or quite a few spelling errors are typically categorised as spam.

Challenge Abstract

Historically, tasks requiring textual content classification started with labeled databases and a mannequin that wanted coaching. The emergence of pre-trained Language Fashions (LLMs) now provides the opportunity of classifying a mess of texts with out the necessity to practice a mannequin beforehand, as these fashions have already been skilled for a lot of use instances. On this article, now we have defined easy methods to make the most of the OpenAI API to entry the GPT-3.5-turbo mannequin and decide whether or not a set of messages is spam or not. The mannequin was in a position to classify 94% of the cases accurately, which signifies a excessive degree of accuracy. Sooner or later, it may very well be worthwhile to guage different OpenAI fashions and discover totally different prompts which will yield higher efficiency. By the way in which, the execution of the venture solely costed $0.007.

stat?event=post


Using the OpenAI API for Detection of SMS Spam Messages was initially printed in In direction of Information Science on Medium, the place persons are persevering with the dialog by highlighting and responding to this story.



Supply hyperlink

LEAVE A REPLY

Please enter your comment!
Please enter your name here