Tweet Classification with Twitter Search API V2, OpenAI API, and Python

Build an application to classify Tweets, retrieved from the Twitter V2 API using the OpenAI classification API.

In this tutorial, we will build an application that will classify Tweets, retrieved from the Twitter V2 API using the OpenAI classification API. We will combine both:

  • OpenAI API for AI functions to perform the classification.
  • The Twitter API V2 to search for the tweets we are going to classify.
Tweet Classification with OpenAI GPT3 and Python

This tutorial has both a YouTube video that fully covers all the steps and the GitHub repo with the code.

GitHub Starting Code Repo:

GitHub – skolo-online/twitter-v2-api

GitHub Finishing Code Repo:

GitHub – skolo-online/Twitter-Search-API-V2-Tweet-Classification-OpenAI

Youtube Video:

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

Steps we are going to cover to complete Tweet Classification with OpenAI API:

  1. Download the starting code — the Flask Application
  2. Create AI training document
  3. Upload training document to OpenAI
  4. Classify tweets
  5. Update the Flask application with classification functionality

Download the Starting Twitter API V2 Code

Clone the repo:

git clone https://github.com/skolo-online/twitter-v2-api.git

Enter your API keys as required in the config file, install the pip libraries required and run the application. Make sure you can see the app from the Twitter tutorial, everything else builds on it and you need it to proceed with the rest of the tutorial.

Create an AI Classification Document

All AI models need to be trained to perform the task that they are built for. OpenAI provides the models, and some training but certain tasks are too specific and require targeted training.

Tweet classification is just one of those tasks that required specific training because every use case is different. In our example we will be classifying Tweets into three categories:

  • Positive
  • Negative
  • Neutral

Therefore we need to create a file with example tweets that we classify into these groups above so we can teach/train the AI how to classify the tweets. So we will tell it, what a positive tweet looks like, same with negative and neutral.

In training data, the more variety we give it — the better the model will be at classifying the results, but with tweets specifically, the training data must be similar to the actual tweets that will be classified.

Therefore, we will be pulling the Tweets from Twitter using our API V2 Tweepy code and manually classifying them.

https://medium.com/media/6e4c1779cbde3cb9a5f38b8f82ca5b26/href

If you run this function:

return50Tweets(query)

At the bottom of the file, and provide a query — you will get a list of tweets saved in a JSONL file in the root of your app. Each line of the file will look like this:

{"text": "@Tbose01 I\u2019m attending a funeral in Polokwane \ud83d\ude01"}

What you need to do is add a label to every line on this file like so:

{"text": "@Tbose01 I\u2019m attending a funeral in Polokwane \ud83d\ude01", "label": "Negative"}

Upload training document to OpenAI for Classification Tasks

Once you have created the document, you need to upload it to OpenAI. The documentation that explains this process can be found here:

OpenAI API

The code for doing that will look like this:

import os
import openai
import config
openai.api_key = config.OPENAI_API_KEY
def uploadClassificationDocument():
filename = 'tweet-training.jsonl'
response = openai.File.create(file=open(filename), purpose="classifications")
return response

Run the code:

def uploadClassificationDocument()

Save the response, it should look like this:

# {
# "bytes": 9401,
# "created_at": 111111111111,
# "filename": "tweet-training.jsonl",
# "id": "file-1234567890123456",
# "object": "file",
# "purpose": "classifications",
# "status": "uploaded",
# "status_details": null
# }

Classify Tweets

Take note of the “id” — you will need it to run the second part of the code:

class_file = "file-1234567890123456"

def classifyTweet(query):
response = openai.Classification.create(
file=class_file,
query=query,
search_model="ada",
model="curie",
max_examples=3)
return response['label']

Update the Flask Application with Tweet Classification

The final Flask application should look like this:

Classified Tweets

The last step is to integrate the above code into our existing Flask application so that a user can enter the search query from the front-end of the application and view the results of that search — classified into the specific buckets pre-defined in the training file.

The full explanation is available on the YouTube video at the top of this tutorial.

The Flask App.py Update

Inside the app.py file, we need to create a new route for the second page like so:

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

This route will work as the first one on the home page, when you first access the route the default query will be run:

query = 'Polokwane lang:en -is:retweet'

The maximum number of results that will be fetched is 20, you can adjust this number between 10 and 100.

The tweets will be classified into these three python lists that will be made available to the frontend.

positiveTweets = []
negativeTweets = []
neutralTweets = []

The Python Flask HTML Template for Classification

The front-end template (class.html) will look like so:

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

Tutorial Comments

Feel free to leave comments, notes, or suggestions on this approach for Tweet classification.

More content at plainenglish.io. Sign up for our free weekly newsletter. Get exclusive access to writing opportunities and advice in our community Discord.


Tweet Classification with Twitter Search API V2, OpenAI API, and Python was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Skolo Online Learning

Build an application to classify Tweets, retrieved from the Twitter V2 API using the OpenAI classification API.

In this tutorial, we will build an application that will classify Tweets, retrieved from the Twitter V2 API using the OpenAI classification API. We will combine both:

  • OpenAI API for AI functions to perform the classification.
  • The Twitter API V2 to search for the tweets we are going to classify.
Tweet Classification with OpenAI GPT3 and Python

This tutorial has both a YouTube video that fully covers all the steps and the GitHub repo with the code.

GitHub Starting Code Repo:

GitHub - skolo-online/twitter-v2-api

GitHub Finishing Code Repo:

GitHub - skolo-online/Twitter-Search-API-V2-Tweet-Classification-OpenAI

Youtube Video:

Steps we are going to cover to complete Tweet Classification with OpenAI API:

  1. Download the starting code — the Flask Application
  2. Create AI training document
  3. Upload training document to OpenAI
  4. Classify tweets
  5. Update the Flask application with classification functionality

Download the Starting Twitter API V2 Code

Clone the repo:

git clone https://github.com/skolo-online/twitter-v2-api.git

Enter your API keys as required in the config file, install the pip libraries required and run the application. Make sure you can see the app from the Twitter tutorial, everything else builds on it and you need it to proceed with the rest of the tutorial.

Create an AI Classification Document

All AI models need to be trained to perform the task that they are built for. OpenAI provides the models, and some training but certain tasks are too specific and require targeted training.

Tweet classification is just one of those tasks that required specific training because every use case is different. In our example we will be classifying Tweets into three categories:

  • Positive
  • Negative
  • Neutral

Therefore we need to create a file with example tweets that we classify into these groups above so we can teach/train the AI how to classify the tweets. So we will tell it, what a positive tweet looks like, same with negative and neutral.

In training data, the more variety we give it — the better the model will be at classifying the results, but with tweets specifically, the training data must be similar to the actual tweets that will be classified.

Therefore, we will be pulling the Tweets from Twitter using our API V2 Tweepy code and manually classifying them.

If you run this function:

return50Tweets(query)

At the bottom of the file, and provide a query — you will get a list of tweets saved in a JSONL file in the root of your app. Each line of the file will look like this:

{"text": "@Tbose01 I\u2019m attending a funeral in Polokwane \ud83d\ude01"}

What you need to do is add a label to every line on this file like so:

{"text": "@Tbose01 I\u2019m attending a funeral in Polokwane \ud83d\ude01", "label": "Negative"}

Upload training document to OpenAI for Classification Tasks

Once you have created the document, you need to upload it to OpenAI. The documentation that explains this process can be found here:

OpenAI API

The code for doing that will look like this:

import os
import openai
import config
openai.api_key = config.OPENAI_API_KEY
def uploadClassificationDocument():
filename = 'tweet-training.jsonl'
response = openai.File.create(file=open(filename), purpose="classifications")
return response

Run the code:

def uploadClassificationDocument()

Save the response, it should look like this:

# {
# "bytes": 9401,
# "created_at": 111111111111,
# "filename": "tweet-training.jsonl",
# "id": "file-1234567890123456",
# "object": "file",
# "purpose": "classifications",
# "status": "uploaded",
# "status_details": null
# }

Classify Tweets

Take note of the “id” — you will need it to run the second part of the code:

class_file = "file-1234567890123456"

def classifyTweet(query):
response = openai.Classification.create(
file=class_file,
query=query,
search_model="ada",
model="curie",
max_examples=3)
return response['label']

Update the Flask Application with Tweet Classification

The final Flask application should look like this:

Classified Tweets

The last step is to integrate the above code into our existing Flask application so that a user can enter the search query from the front-end of the application and view the results of that search — classified into the specific buckets pre-defined in the training file.

The full explanation is available on the YouTube video at the top of this tutorial.

The Flask App.py Update

Inside the app.py file, we need to create a new route for the second page like so:

This route will work as the first one on the home page, when you first access the route the default query will be run:

query = 'Polokwane lang:en -is:retweet'

The maximum number of results that will be fetched is 20, you can adjust this number between 10 and 100.

The tweets will be classified into these three python lists that will be made available to the frontend.

positiveTweets = []
negativeTweets = []
neutralTweets = []

The Python Flask HTML Template for Classification

The front-end template (class.html) will look like so:

Tutorial Comments

Feel free to leave comments, notes, or suggestions on this approach for Tweet classification.

More content at plainenglish.io. Sign up for our free weekly newsletter. Get exclusive access to writing opportunities and advice in our community Discord.


Tweet Classification with Twitter Search API V2, OpenAI API, and Python was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Skolo Online Learning


Print Share Comment Cite Upload Translate Updates
APA

Skolo Online Learning | Sciencx (2022-01-12T22:25:35+00:00) Tweet Classification with Twitter Search API V2, OpenAI API, and Python. Retrieved from https://www.scien.cx/2022/01/12/tweet-classification-with-twitter-search-api-v2-openai-api-and-python/

MLA
" » Tweet Classification with Twitter Search API V2, OpenAI API, and Python." Skolo Online Learning | Sciencx - Wednesday January 12, 2022, https://www.scien.cx/2022/01/12/tweet-classification-with-twitter-search-api-v2-openai-api-and-python/
HARVARD
Skolo Online Learning | Sciencx Wednesday January 12, 2022 » Tweet Classification with Twitter Search API V2, OpenAI API, and Python., viewed ,<https://www.scien.cx/2022/01/12/tweet-classification-with-twitter-search-api-v2-openai-api-and-python/>
VANCOUVER
Skolo Online Learning | Sciencx - » Tweet Classification with Twitter Search API V2, OpenAI API, and Python. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/01/12/tweet-classification-with-twitter-search-api-v2-openai-api-and-python/
CHICAGO
" » Tweet Classification with Twitter Search API V2, OpenAI API, and Python." Skolo Online Learning | Sciencx - Accessed . https://www.scien.cx/2022/01/12/tweet-classification-with-twitter-search-api-v2-openai-api-and-python/
IEEE
" » Tweet Classification with Twitter Search API V2, OpenAI API, and Python." Skolo Online Learning | Sciencx [Online]. Available: https://www.scien.cx/2022/01/12/tweet-classification-with-twitter-search-api-v2-openai-api-and-python/. [Accessed: ]
rf:citation
» Tweet Classification with Twitter Search API V2, OpenAI API, and Python | Skolo Online Learning | Sciencx | https://www.scien.cx/2022/01/12/tweet-classification-with-twitter-search-api-v2-openai-api-and-python/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.