Spaces:
Runtime error
Runtime error
Commit ·
6a92f1f
1
Parent(s): 97c4c9c
إضافة ملف التدريب ومكتباته
Browse files- requirements.txt +3 -2
- train.py +105 -0
requirements.txt
CHANGED
|
@@ -1,7 +1,8 @@
|
|
| 1 |
transformers==4.35.2
|
| 2 |
torch==2.1.1
|
| 3 |
-
gradio==4.
|
| 4 |
datasets==2.15.0
|
| 5 |
-
scikit-learn==1.2.2
|
| 6 |
numpy==1.26.2
|
| 7 |
regex==2023.10.3
|
|
|
|
|
|
|
|
|
| 1 |
transformers==4.35.2
|
| 2 |
torch==2.1.1
|
| 3 |
+
gradio==4.13.0
|
| 4 |
datasets==2.15.0
|
|
|
|
| 5 |
numpy==1.26.2
|
| 6 |
regex==2023.10.3
|
| 7 |
+
scikit-learn==1.3.2
|
| 8 |
+
tensorboard==2.15.1
|
train.py
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification, TrainingArguments, Trainer
|
| 3 |
+
from datasets import load_dataset, Dataset
|
| 4 |
+
import numpy as np
|
| 5 |
+
from sklearn.metrics import accuracy_score, precision_recall_fscore_support
|
| 6 |
+
|
| 7 |
+
def compute_metrics(pred):
|
| 8 |
+
labels = pred.label_ids
|
| 9 |
+
preds = pred.predictions.argmax(-1)
|
| 10 |
+
precision, recall, f1, _ = precision_recall_fscore_support(labels, preds, average='weighted')
|
| 11 |
+
acc = accuracy_score(labels, preds)
|
| 12 |
+
return {
|
| 13 |
+
'accuracy': acc,
|
| 14 |
+
'f1': f1,
|
| 15 |
+
'precision': precision,
|
| 16 |
+
'recall': recall
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
class ArabicTextTrainer:
|
| 20 |
+
def __init__(self, model_name="CAMeL-Lab/bert-base-arabic-camelbert-msa", num_labels=3):
|
| 21 |
+
self.tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 22 |
+
self.model = AutoModelForSequenceClassification.from_pretrained(model_name, num_labels=num_labels)
|
| 23 |
+
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 24 |
+
self.model.to(self.device)
|
| 25 |
+
|
| 26 |
+
def tokenize_data(self, examples):
|
| 27 |
+
return self.tokenizer(
|
| 28 |
+
examples['text'],
|
| 29 |
+
padding='max_length',
|
| 30 |
+
truncation=True,
|
| 31 |
+
max_length=128
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
def prepare_dataset(self, dataset):
|
| 35 |
+
tokenized_dataset = dataset.map(self.tokenize_data, batched=True)
|
| 36 |
+
tokenized_dataset = tokenized_dataset.remove_columns(['text'])
|
| 37 |
+
tokenized_dataset = tokenized_dataset.rename_column('label', 'labels')
|
| 38 |
+
tokenized_dataset.set_format('torch')
|
| 39 |
+
return tokenized_dataset
|
| 40 |
+
|
| 41 |
+
def train(self, train_dataset, eval_dataset=None, output_dir="./results", num_train_epochs=3):
|
| 42 |
+
training_args = TrainingArguments(
|
| 43 |
+
output_dir=output_dir,
|
| 44 |
+
num_train_epochs=num_train_epochs,
|
| 45 |
+
per_device_train_batch_size=16,
|
| 46 |
+
per_device_eval_batch_size=16,
|
| 47 |
+
warmup_steps=500,
|
| 48 |
+
weight_decay=0.01,
|
| 49 |
+
logging_dir='./logs',
|
| 50 |
+
logging_steps=10,
|
| 51 |
+
evaluation_strategy="epoch" if eval_dataset else "no",
|
| 52 |
+
save_strategy="epoch",
|
| 53 |
+
load_best_model_at_end=True if eval_dataset else False,
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
trainer = Trainer(
|
| 57 |
+
model=self.model,
|
| 58 |
+
args=training_args,
|
| 59 |
+
train_dataset=train_dataset,
|
| 60 |
+
eval_dataset=eval_dataset,
|
| 61 |
+
compute_metrics=compute_metrics,
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
print("بدء التدريب...")
|
| 65 |
+
trainer.train()
|
| 66 |
+
|
| 67 |
+
if eval_dataset:
|
| 68 |
+
print("تقييم النموذج...")
|
| 69 |
+
results = trainer.evaluate()
|
| 70 |
+
print(f"نتائج التقييم: {results}")
|
| 71 |
+
|
| 72 |
+
print("حفظ النموذج...")
|
| 73 |
+
self.model.save_pretrained(output_dir)
|
| 74 |
+
self.tokenizer.save_pretrained(output_dir)
|
| 75 |
+
print("تم حفظ النموذج بنجاح!")
|
| 76 |
+
|
| 77 |
+
def main():
|
| 78 |
+
# مثال على كيفية استخدام المدرب
|
| 79 |
+
# يمكنك تغيير مجموعة البيانات حسب احتياجاتك
|
| 80 |
+
print("تحميل مجموعة البيانات...")
|
| 81 |
+
|
| 82 |
+
# مثال على تحميل مجموعة بيانات من Hugging Face
|
| 83 |
+
# dataset = load_dataset("arabic_dataset_name")
|
| 84 |
+
|
| 85 |
+
# أو إنشاء مجموعة بيانات من قائمة
|
| 86 |
+
example_data = {
|
| 87 |
+
'text': ["نص إيجابي", "نص محايد", "نص سلبي"],
|
| 88 |
+
'label': [2, 1, 0] # 2: إيجابي، 1: محايد، 0: سلبي
|
| 89 |
+
}
|
| 90 |
+
dataset = Dataset.from_dict(example_data)
|
| 91 |
+
|
| 92 |
+
# تقسيم البيانات إلى مجموعتي تدريب واختبار
|
| 93 |
+
dataset = dataset.train_test_split(test_size=0.2)
|
| 94 |
+
|
| 95 |
+
trainer = ArabicTextTrainer()
|
| 96 |
+
|
| 97 |
+
# تجهيز البيانات
|
| 98 |
+
train_dataset = trainer.prepare_dataset(dataset['train'])
|
| 99 |
+
eval_dataset = trainer.prepare_dataset(dataset['test'])
|
| 100 |
+
|
| 101 |
+
# بدء التدريب
|
| 102 |
+
trainer.train(train_dataset, eval_dataset)
|
| 103 |
+
|
| 104 |
+
if __name__ == "__main__":
|
| 105 |
+
main()
|