Upload folder using huggingface_hub
Browse files- scrip2t.py +40 -0
- script.py +65 -0
scrip2t.py
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from datasets import load_dataset
|
2 |
+
dataset = load_dataset("yelp_review_full")
|
3 |
+
|
4 |
+
# لعرض عينة من البيانات
|
5 |
+
print(dataset["train"][100])
|
6 |
+
|
7 |
+
from transformers import AutoTokenizer
|
8 |
+
|
9 |
+
tokenizer = AutoTokenizer.from_pretrained("bert-base-cased")
|
10 |
+
|
11 |
+
def tokenize_function(examples):
|
12 |
+
return tokenizer(examples["text"], padding="max_length", truncation=True)
|
13 |
+
|
14 |
+
tokenized_datasets = dataset.map(tokenize_function, batched=True)
|
15 |
+
|
16 |
+
small_train_dataset = tokenized_datasets["train"].shuffle(seed=42).select(range(100))
|
17 |
+
small_eval_dataset = tokenized_datasets["test"].shuffle(seed=42).select(range(100))
|
18 |
+
|
19 |
+
from transformers import AutoModelForSequenceClassification, TrainingArguments, Trainer
|
20 |
+
|
21 |
+
model = AutoModelForSequenceClassification.from_pretrained("bert-base-cased", num_labels=5)
|
22 |
+
|
23 |
+
training_args = TrainingArguments(
|
24 |
+
output_dir="./results",
|
25 |
+
evaluation_strategy="epoch",
|
26 |
+
learning_rate=2e-5,
|
27 |
+
per_device_train_batch_size=16,
|
28 |
+
per_device_eval_batch_size=16,
|
29 |
+
num_train_epochs=3,
|
30 |
+
weight_decay=0.01,
|
31 |
+
)
|
32 |
+
|
33 |
+
trainer = Trainer(
|
34 |
+
model=model,
|
35 |
+
args=training_args,
|
36 |
+
train_dataset=small_train_dataset,
|
37 |
+
eval_dataset=small_eval_dataset,
|
38 |
+
)
|
39 |
+
|
40 |
+
trainer.train()
|
script.py
ADDED
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from datasets import load_dataset
|
2 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification, TrainingArguments, Trainer
|
3 |
+
from huggingface_hub import HfApi, Repository, login
|
4 |
+
import os
|
5 |
+
|
6 |
+
# تسجيل الدخول إلى حسابك في Hugging Face باستخدام التوكن من متغير البيئة
|
7 |
+
token = os.getenv('HUGGINGFACE_TOKEN')
|
8 |
+
if token is None:
|
9 |
+
raise ValueError("HUGGINGFACE_TOKEN environment variable not set")
|
10 |
+
|
11 |
+
login(token=token)
|
12 |
+
|
13 |
+
# تحميل مجموعة بيانات يلب
|
14 |
+
dataset = load_dataset("yelp_review_full")
|
15 |
+
|
16 |
+
# عرض عينة من البيانات
|
17 |
+
print(dataset["train"][100])
|
18 |
+
|
19 |
+
# تحميل التوكنايزر الخاص بـ BERT
|
20 |
+
tokenizer = AutoTokenizer.from_pretrained("bert-base-cased")
|
21 |
+
|
22 |
+
# تعريف دالة لتوكنايز النصوص
|
23 |
+
def tokenize_function(examples):
|
24 |
+
return tokenizer(examples["text"], padding="max_length", truncation=True)
|
25 |
+
|
26 |
+
# تطبيق عملية التوكنايز على مجموعة البيانات
|
27 |
+
tokenized_datasets = dataset.map(tokenize_function, batched=True)
|
28 |
+
|
29 |
+
# تحضير مجموعات بيانات صغيرة للتدريب والتقييم
|
30 |
+
small_train_dataset = tokenized_datasets["train"].shuffle(seed=42).select(range(100))
|
31 |
+
small_eval_dataset = tokenized_datasets["test"].shuffle(seed=42).select(range(100))
|
32 |
+
|
33 |
+
# تحميل نموذج BERT المسبق التدريب لتصنيف النصوص
|
34 |
+
model = AutoModelForSequenceClassification.from_pretrained("bert-base-cased", num_labels=5)
|
35 |
+
|
36 |
+
# إعدادات التدريب
|
37 |
+
training_args = TrainingArguments(
|
38 |
+
output_dir="./results",
|
39 |
+
evaluation_strategy="epoch",
|
40 |
+
learning_rate=2e-5,
|
41 |
+
per_device_train_batch_size=16,
|
42 |
+
per_device_eval_batch_size=16,
|
43 |
+
num_train_epochs=3,
|
44 |
+
weight_decay=0.01,
|
45 |
+
)
|
46 |
+
|
47 |
+
# تهيئة المدرب
|
48 |
+
trainer = Trainer(
|
49 |
+
model=model,
|
50 |
+
args=training_args,
|
51 |
+
train_dataset=small_train_dataset,
|
52 |
+
eval_dataset=small_eval_dataset,
|
53 |
+
)
|
54 |
+
|
55 |
+
# تدريب النموذج
|
56 |
+
trainer.train()
|
57 |
+
|
58 |
+
# رفع النموذج إلى Hugging Face
|
59 |
+
model_name = "yelp_review_classifier"
|
60 |
+
repo_url = HfApi().create_repo(name=model_name, private=False, token=token)
|
61 |
+
repo = Repository(local_dir=model_name, clone_from=repo_url)
|
62 |
+
|
63 |
+
model.save_pretrained(model_name)
|
64 |
+
tokenizer.save_pretrained(model_name)
|
65 |
+
repo.push_to_hub(commit_message="Initial commit", token=token)
|