Views
No views yet
1import torch
2from transformers import BertForSequenceClassification, BertTokenizer, BertConfig, pipeline
3
4# Load the tokenizer and model
5tokenizer = BertTokenizer.from_pretrained("nfhakim/police-sentiment-c1-v2")
6config = BertConfig.from_pretrained("nfhakim/police-sentiment-c1-v2")
7model = BertForSequenceClassification.from_pretrained("nfhakim/police-sentiment-c1-v2", config=config)1# Initialize the pipeline
2nlp = pipeline("text-classification", model=model, tokenizer=tokenizer)
3
4# Define a function to handle input text
5def classify_text(text):
6 # Tokenize the text and truncate to the first 512 tokens if necessary
7 inputs = tokenizer(text, truncation=True, max_length=512, return_tensors="pt")
8
9 # Use the model to classify the text
10 results = nlp(inputs['input_ids'])
11 return results
12
13# Example usage
14input_text = "Your input text here"
15output = classify_text(input_text)
16print(output)
17