Views
No views yet
pip install arabert transformers torch1# Import required Modules
2from transformers import AutoModelForSequenceClassification, AutoTokenizer
3import torch
4
5# Load model and Tokenizer
6model_name = 'hugsanaa/CyberAraBERT'
7model = AutoModelForSequenceClassification.from_pretrained(model_name, return_dict=False, num_labels=2)
8tokenizer = AutoTokenizer.from_pretrained(model_name)1# Example of Cyberbullying text
2text = "نايس موته يا اهبل"
3
4# Tokenize input
5inputs = tokenizer(text, return_tensor="pt", truncation = True, padding = True)
6
7# Make Predictions
8with torch.no_grad():
9 logits=model(**inputs).logits
10 predicted_Class = torch.argmax(logits)
11
12# Interpret results
13labels = ["Cyberbullying", "Not Cyberbullying"]
14print(f"Prediction: {labels[predicted_class]}")1import pandas as pd
2from transformers import pipeline
3import more_itertools
4from tqdm import tqdm_notebook as tqdm
5
6model = 'hugsanaa/CyberAraBERT'
7
8# load the dataset (the data must include text column)
9data = pd.read_csv(your_cyberbulling_data)
10
11# generate prediction pipeline
12pipe = pipeline("sentiment-analysis", model=model, device=0, return_all_scores =True, max_length=max_len, truncation=True)
13preds = []
14for s in tqdm(more_itertools.chunked(list(data['text']), 32)): # batching for faster inference
15 preds.extend(pipe(s))
16
17# Generate final predictions
18data[f'preds'] = preds
19final_pred = []
20for prediction in data['preds']:
21 final_pred.append(max(prediction, key=lambda x: x['score'])['label'])
22
23data[f'Final Prediction'] = final_pred| Class | Precision | Recall | F1-Score | Support |
|---|---|---|---|---|
| Not Cyberbullying | 0.9256 | 0.9043 | 0.9148 | 564 |
| Cyberbullying | 0.8453 | 0.8780 | 0.8613 | 336 |
| Overall / Avg. | 0.8956 | 0.8944 | 0.8948 | 900 |