File size: 992 Bytes
9bb2e82 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
---
pipeline_tag: zero-shot-classification
---
# Install transformers library
!pip install transformers
# Import necessary libraries
import pandas as pd
from transformers import pipeline
from google.colab import files
# Upload the file
uploaded = files.upload()
# Load the dataset
file_name = 'publications.csv' # Use the file name as uploaded
df = pd.read_csv(file_name)
# Display the first few rows of the dataset
df.head()
# Load the zero-shot classification pipeline
classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
# Define the categories
candidate_labels = ["World", "Sports", "Business", "Science and Technology", "Entertainment", "Lifestyle"]
# Classify each news article
results = []
for article in df['headline']:
result = classifier(article, candidate_labels)
category = result['labels'][0]
results.append(category)
# Add the results to the DataFrame
df['category'] = results
# Display the categorized DataFrame
df.head(10) |