### Setup CAMeLBERT Environment using Conda Source: https://github.com/camel-lab/camelbert/blob/master/README.md This bash script demonstrates how to clone the CAMeLBERT repository, create a conda environment with specified Python version, activate it, and install all required dependencies using pip. Ensure you have conda and CUDA installed before running. ```bash git clone https://github.com/CAMeL-Lab/CAMeLBERT.git cd CAMeLBERT conda create -n CAMeLBERT python=3.7 conda activate CAMeLBERT pip install -r requirements.txt ``` -------------------------------- ### Convert Examples to Model Features Source: https://context7.com/camel-lab/camelbert/llms.txt Transforms raw text examples into input features suitable for NLP models. This Python snippet demonstrates using `convert_examples_to_features` with `ArabicSentimentProcessor` and an `AutoTokenizer`. It handles tokenization, padding, and alignment for classification tasks. ```python from text_classification.utils.data_utils import ( convert_examples_to_features, ArabicSentimentProcessor ) from transformers import AutoTokenizer, InputExample # Initialize tokenizer and processor tokenizer = AutoTokenizer.from_pretrained("CAMeL-Lab/bert-base-arabic-camelbert-msa") processor = ArabicSentimentProcessor() label_list = processor.get_labels() # ["positive", "negative", "neutral"] # Create example data examples = [ InputExample(guid="train-1", text_a="هذا المنتج رائع", label="positive"), InputExample(guid="train-2", text_a="تجربة سيئة جدا", label="negative"), InputExample(guid="train-3", text_a="منتج عادي", label="neutral") ] # Note: The actual call to convert_examples_to_features is omitted here as per the provided text. # It would typically look like: # features = convert_examples_to_features( # examples, label_list, 512, tokenizer, "bert" # ) ``` -------------------------------- ### Convert Text to Features for CAMeLBERT Source: https://context7.com/camel-lab/camelbert/llms.txt Converts input text examples into features suitable for CAMeLBERT models. This process involves tokenization, padding, and label assignment. It requires the examples, a tokenizer, max sequence length, label list, and output mode as input. ```python features = convert_examples_to_features( examples=examples, tokenizer=tokenizer, max_length=128, label_list=label_list, output_mode="classification", pad_on_left=False, pad_token=tokenizer.pad_token_id, pad_token_segment_id=0 ) # Access features for feature in features: print(f"Input IDs: {feature.input_ids[:10]}...") print(f"Attention Mask: {feature.attention_mask[:10]}...") print(f"Label: {feature.label}") ``` -------------------------------- ### Preprocess Arabic Tweets for Sentiment Analysis Source: https://context7.com/camel-lab/camelbert/llms.txt Processes Arabic tweets to prepare them for sentiment analysis. This Python script uses `ArabicSentimentProcessor` to normalize Unicode, remove diacritics, and clean URLs and mentions. It can also load training examples from a specified directory. ```python from text_classification.utils.data_utils import ArabicSentimentProcessor # Initialize the processor processor = ArabicSentimentProcessor() # Process a tweet raw_tweet = "هذا مثال رائع! https://example.com @username RT: الإمارات" processed_tweet = processor.process_tweet(raw_tweet) print(f"Original: {raw_tweet}") print(f"Processed: {processed_tweet}") # Load training examples train_examples = processor.get_train_examples("/path/to/data") # Returns list of InputExample objects with guid, text_a, and label attributes ``` -------------------------------- ### Fine-tune POS Tagging Model with CAMeLBERT Source: https://context7.com/camel-lab/camelbert/llms.txt Fine-tunes an Arabic Part-of-Speech tagging model using treebank datasets (PATB, ARZATB, GUMAR) and CAMeLBERT. It involves setting environment variables for data and model configuration, and running the token classification script. The output is saved to a specified directory. ```bash export DATA_DIR=/path/to/patb_pos_data export MAX_LENGTH=512 export BERT_MODEL=CAMeL-Lab/bert-base-arabic-camelbert-msa export OUTPUT_DIR=./output/pos_tagger export BATCH_SIZE=32 export NUM_EPOCHS=10 export SAVE_STEPS=500 export SEED=12345 python token-classification/run_token_classification.py \ --data_dir $DATA_DIR \ --labels $DATA_DIR/labels.txt \ --model_name_or_path $BERT_MODEL \ --output_dir $OUTPUT_DIR \ --max_seq_length $MAX_LENGTH \ --num_train_epochs $NUM_EPOCHS \ --per_device_train_batch_size $BATCH_SIZE \ --save_steps $SAVE_STEPS \ --seed $SEED \ --overwrite_output_dir \ --overwrite_cache \ --do_train \ --do_eval ``` -------------------------------- ### Configure Multi-GPU Training for CAMeLBERT Source: https://context7.com/camel-lab/camelbert/llms.txt Sets up distributed training arguments and initializes a Hugging Face Trainer for multi-GPU fine-tuning of CAMeLBERT models. It leverages PyTorch and the Transformers library to handle distributed training automatically. ```python import torch from transformers import TrainingArguments, Trainer from token_classification.utils import TokenClassificationDataSet # Set up training arguments for multi-GPU training_args = TrainingArguments( output_dir="./output/multi_gpu_ner", num_train_epochs=3, per_device_train_batch_size=32, per_device_eval_batch_size=32, warmup_steps=500, weight_decay=0.01, logging_steps=100, save_steps=1000, evaluation_strategy="steps", eval_steps=500, save_total_limit=3, seed=12345, local_rank=-1, # Set by torch.distributed.launch fp16=torch.cuda.is_available(), dataloader_num_workers=4, ) # Initialize trainer (automatically detects multiple GPUs) trainer = Trainer( model=model, args=training_args, train_dataset=train_dataset, eval_dataset=eval_dataset, compute_metrics=compute_metrics_fn ) # Train on all available GPUs trainer.train() ``` -------------------------------- ### Fine-tune CAMeLBERT for NER (Bash) Source: https://github.com/camel-lab/camelbert/blob/master/README.md Bash script to fine-tune CAMeLBERT models for Named Entity Recognition (NER) experiments. Requires setting environment variables for data directory, model paths, and training parameters. Outputs are saved to a specified directory. ```bash export DATA_DIR=/path/to/data # Should contain train/dev/test/labels files export MAX_LENGTH=512 export BERT_MODEL=/path/to/pretrained_model/ # Or huggingface model id export OUTPUT_DIR=/path/to/output_dir export BATCH_SIZE=32 export NUM_EPOCHS=3 export SAVE_STEPS=750 export SEED=12345 python run_token_classification.py \ --data_dir $DATA_DIR \ --labels $DATA_DIR/labels.txt \ --model_name_or_path $BERT_MODEL \ --output_dir $OUTPUT_DIR \ --max_seq_length $MAX_LENGTH \ --num_train_epochs $NUM_EPOCHS \ --per_device_train_batch_size $BATCH_SIZE \ --save_steps $SAVE_STEPS \ --seed $SEED \ --overwrite_output_dir \ --overwrite_cache \ --do_train \ --do_predict ``` -------------------------------- ### Dialect Identification with NADI using CAMeLBERT (Bash) Source: https://context7.com/camel-lab/camelbert/llms.txt A bash script demonstrating how to fine-tune CAMeLBERT for country-level Arabic dialect identification using the NADI dataset. It sets environment variables for data paths and model configuration, then runs the text classification script. ```bash export DATA_DIR=/path/to/nadi_country_data export TASK_NAME=arabic_did_nadi_country export MODEL_PATH=CAMeL-Lab/bert-base-arabic-camelbert-da-sentiment export OUTPUT_DIR=./output/nadi_country_classifier python text-classification/run_text_classification.py \ --model_type bert \ --model_name_or_path $MODEL_PATH \ --task_name $TASK_NAME \ --do_train \ --do_eval \ --do_pred \ --write_preds \ --eval_all_checkpoints \ --save_steps 500 \ --data_dir $DATA_DIR \ --max_seq_length 128 \ --per_gpu_train_batch_size 32 \ --per_gpu_eval_batch_size 32 \ --learning_rate 3e-5 \ --num_train_epochs 10.0 \ --overwrite_output_dir \ --overwrite_cache \ --output_dir $OUTPUT_DIR \ --seed 12345 ``` -------------------------------- ### Fine-tune NER Model with CAMeLBERT Source: https://context7.com/camel-lab/camelbert/llms.txt Trains an Arabic Named Entity Recognition model using the ANERCorp dataset and CAMeLBERT. Requires setting environment variables for data directory, model path, and training parameters. Outputs the trained model to a specified directory. ```bash export DATA_DIR=/path/to/anercorp_data export MAX_LENGTH=512 export BERT_MODEL=CAMeL-Lab/bert-base-arabic-camelbert-mix export OUTPUT_DIR=./output/ner_model export BATCH_SIZE=32 export NUM_EPOCHS=3 export SAVE_STEPS=750 export SEED=12345 python token-classification/run_token_classification.py \ --data_dir $DATA_DIR \ --labels $DATA_DIR/labels.txt \ --model_name_or_path $BERT_MODEL \ --output_dir $OUTPUT_DIR \ --max_seq_length $MAX_LENGTH \ --num_train_epochs $NUM_EPOCHS \ --per_device_train_batch_size $BATCH_SIZE \ --save_steps $SAVE_STEPS \ --seed $SEED \ --overwrite_output_dir \ --overwrite_cache \ --do_train \ --do_predict ``` -------------------------------- ### Fine-tune CAMeLBERT for POS Tagging (Bash) Source: https://github.com/camel-lab/camelbert/blob/master/README.md Bash script for fine-tuning CAMeLBERT models on Part-of-Speech (POS) tagging tasks across different Arabic dialects (MSA, EGY, GLF). It requires similar environment variable configurations as NER fine-tuning, with a different number of epochs. ```bash export DATA_DIR=/path/to/data # Should contain train/dev/test/labels files export MAX_LENGTH=512 export BERT_MODEL=/path/to/pretrained_model/ # Or huggingface model id export OUTPUT_DIR=/path/to/output_dir export BATCH_SIZE=32 export NUM_EPOCHS=10 export SAVE_STEPS=500 export SEED=12345 python run_token_classification.py \ --data_dir $DATA_DIR \ --labels $DATA_DIR/labels.txt \ --model_name_or_path $BERT_MODEL \ --output_dir $OUTPUT_DIR \ --max_seq_length $MAX_LENGTH \ --num_train_epochs $NUM_EPOCHS \ --per_device_train_batch_size $BATCH_SIZE \ --save_steps $SAVE_STEPS \ --seed $SEED \ --overwrite_output_dir \ --overwrite_cache \ --do_train \ --do_eval ``` -------------------------------- ### Load Pre-trained CAMeLBERT for Sequence Classification Source: https://context7.com/camel-lab/camelbert/llms.txt Loads a pre-trained CAMeLBERT model from Hugging Face for sequence classification tasks like sentiment analysis. It initializes the configuration, tokenizer, and model, then performs inference on sample text. ```python from transformers import ( AutoConfig, AutoModelForSequenceClassification, AutoTokenizer ) # Load model for sentiment analysis (3 classes) model_name = "CAMeL-Lab/bert-base-arabic-camelbert-msa" num_labels = 3 label_list = ["positive", "negative", "neutral"] config = AutoConfig.from_pretrained( model_name, num_labels=num_labels, label2id={label: i for i, label in enumerate(label_list)}, id2label={i: label for i, label in enumerate(label_list)}, finetuning_task="arabic_sentiment" ) tokenizer = AutoTokenizer.from_pretrained(model_name) model = AutoModelForSequenceClassification.from_pretrained( model_name, config=config ) # Perform inference text = "هذا الفندق ممتاز والخدمة رائعة" inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True) outputs = model(**inputs) predictions = outputs.logits.argmax(dim=-1) print(f"Text: {text}") print(f"Predicted label: {label_list[predictions.item()]") ``` -------------------------------- ### Load Token Classification Datasets Source: https://context7.com/camel-lab/camelbert/llms.txt Loads and caches datasets for token classification tasks like NER and POS tagging. It requires a tokenizer, labels, and data directory. The `TokenClassificationDataSet` class handles feature extraction and caching, supporting different split modes (train, dev). ```python from token_classification.utils import TokenClassificationDataSet, Split, get_labels from transformers import AutoTokenizer # Load tokenizer and labels tokenizer = AutoTokenizer.from_pretrained("CAMeL-Lab/bert-base-arabic-camelbert-msa") labels = get_labels("/path/to/labels.txt") # Create training dataset train_dataset = TokenClassificationDataSet( data_dir="/path/to/data", tokenizer=tokenizer, labels=labels, model_type="bert", max_seq_length=512, overwrite_cache=False, mode=Split.train ) # Create evaluation dataset eval_dataset = TokenClassificationDataSet( data_dir="/path/to/data", tokenizer=tokenizer, labels=labels, model_type="bert", max_seq_length=512, overwrite_cache=False, mode=Split.dev ) print(f"Training examples: {len(train_dataset)}") print(f"Evaluation examples: {len(eval_dataset)}") ``` -------------------------------- ### Fine-tune CAMeLBERT for Dialect Identification Source: https://github.com/camel-lab/camelbert/blob/master/README.md This Python script fine-tunes CAMeLBERT models for Arabic dialect identification. Users need to set `DATA_DIR` and choose a `TASK_NAME` from the available options (e.g., `arabic_did_madar_26`). The script uses `run_text_classification.py` and requires specifying the model path and output directory. Ensure the correct dataset paths and configurations are provided. ```bash export DATA_DIR=/path/to/data export TASK_NAME=arabic_did_madar_26 # or arabic_did_madar_6, arabic_did_madar_twitter, arabic_did_nadi_country python run_text_classification.py \ --model_type bert \ --model_name_or_path /path/to/pretrained_model/ \ --task_name $TASK_NAME \ --do_train \ --do_eval \ --eval_all_checkpoints \ --save_steps 500 \ --data_dir $DATA_DIR \ --max_seq_length 128 \ --per_gpu_train_batch_size 32 \ --per_gpu_eval_batch_size 32 \ --learning_rate 3e-5 \ --num_train_epochs 10.0 \ --overwrite_output_dir \ --overwrite_cache \ --output_dir /path/to/output_dir \ --seed 12345 ``` -------------------------------- ### Fine-tune CAMeLBERT for Arabic Poetry Classification Source: https://context7.com/camel-lab/camelbert/llms.txt This script fine-tunes a CAMeLBERT model for classifying Arabic poetry into different meter types. It depends on the Transformers library and PyTorch. Key parameters include model path, task name, data directory, and output directory. ```bash export DATA_DIR=/path/to/apcd_poetry_data export TASK_NAME=arabic_poetry export MODEL_PATH=CAMeL-Lab/bert-base-arabic-camelbert-ca export OUTPUT_DIR=./output/poetry_classifier python text-classification/run_text_classification.py \ --model_type bert \ --model_name_or_path $MODEL_PATH \ --task_name $TASK_NAME \ --do_train \ --do_eval \ --eval_all_checkpoints \ --save_steps 5000 \ --data_dir $DATA_DIR \ --max_seq_length 128 \ --per_gpu_train_batch_size 32 \ --per_gpu_eval_batch_size 32 \ --learning_rate 3e-5 \ --num_train_epochs 3.0 \ --overwrite_output_dir \ --overwrite_cache \ --output_dir $OUTPUT_DIR \ --seed 12345 # Expected output: # Loading features from cached file # ***** Running training ***** # Num examples = 13665 # Instantaneous batch size per GPU = 32 # ***** Eval results ***** # f1 = 0.9245 # accuracy = 0.9251 ``` -------------------------------- ### Fine-tune CAMeLBERT for Poetry Classification Source: https://github.com/camel-lab/camelbert/blob/master/README.md This Python script is used to fine-tune CAMeLBERT models for Arabic poetry classification. Set the `DATA_DIR` and `TASK_NAME` environment variables appropriately. The script leverages `run_text_classification.py` and requires paths for the pretrained model and output directory. It supports training, evaluation, and checkpoint selection based on dev set performance. ```bash export DATA_DIR=/path/to/data export TASK_NAME=arabic_poetry python run_text_classification.py \ --model_type bert \ --model_name_or_path /path/to/pretrained_model/ \ --task_name $TASK_NAME \ --do_train \ --do_eval \ --eval_all_checkpoints \ --save_steps 5000 \ --data_dir $DATA_DIR \ --max_seq_length 128 \ --per_gpu_train_batch_size 32 \ --per_gpu_eval_batch_size 32 \ --learning_rate 3e-5 \ --num_train_epochs 3.0 \ --overwrite_output_dir \ --overwrite_cache \ --output_dir /path/to/output_dir \ --seed 12345 ``` -------------------------------- ### CAMeLBERT Citation (BibTeX) Source: https://github.com/camel-lab/camelbert/blob/master/README.md BibTeX entry for citing the CAMeLBERT paper. This should be included in LaTeX documents when using the models or referring to the research. ```bibtex @inproceedings{inoue-etal-2021-interplay, title = "The Interplay of Variant, Size, and Task Type in {A}rabic Pre-trained Language Models", author = "Inoue, Go and Alhafni, Bashar and Baimukan, Nurpeiis and Bouamor, Houda and Habash, Nizar", booktitle = "Proceedings of the Sixth Arabic Natural Language Processing Workshop", month = apr, year = "2021", address = "Kyiv, Ukraine (Online)", publisher = "Association for Computational Linguistics", abstract = "In this paper, we explore the effects of language variants, data sizes, and fine-tuning task types in Arabic pre-trained language models. To do so, we build three pre-trained language models across three variants of Arabic: Modern Standard Arabic (MSA), dialectal Arabic, and classical Arabic, in addition to a fourth language model which is pre-trained on a mix of the three. We also examine the importance of pre-training data size by building additional models that are pre-trained on a scaled-down set of the MSA variant. We compare our different models to each other, as well as to eight publicly available models by fine-tuning them on five NLP tasks spanning 12 datasets. Our results suggest that the variant proximity of pre-training data to fine-tuning data is more important than the pre-training data size. We exploit this insight in defining an optimized system selection model for the studied tasks.", } ``` -------------------------------- ### Fine-tune CAMeLBERT for Arabic Dialect Identification Source: https://context7.com/camel-lab/camelbert/llms.txt This script fine-tunes a CAMeLBERT model for Arabic dialect identification using the MADAR corpus. It requires the Transformers library and a PyTorch environment. The script accepts model path, task name, data directory, and output directory as arguments. ```bash export DATA_DIR=/path/to/madar_corpus_26 export TASK_NAME=arabic_did_madar_26 export MODEL_PATH=CAMeL-Lab/bert-base-arabic-camelbert-da export OUTPUT_DIR=./output/dialect_identification_26 python text-classification/run_text_classification.py \ --model_type bert \ --model_name_or_path $MODEL_PATH \ --task_name $TASK_NAME \ --do_train \ --do_eval \ --eval_all_checkpoints \ --save_steps 500 \ --data_dir $DATA_DIR \ --max_seq_length 128 \ --per_gpu_train_batch_size 32 \ --per_gpu_eval_batch_size 32 \ --learning_rate 3e-5 \ --num_train_epochs 10.0 \ --overwrite_output_dir \ --overwrite_cache \ --output_dir $OUTPUT_DIR \ --seed 12345 # Expected output: # Creating features from dataset file at /path/to/madar_corpus_26 # **LABEL MAP** # {'KHA': 0, 'TUN': 1, 'MOS': 2, 'CAI': 3, ...} # ***** Eval results checkpoint-5000 ***** # f1 = 0.6892 # accuracy = 0.6890 ``` -------------------------------- ### Fine-tune CAMeLBERT for Sentiment Analysis Source: https://github.com/camel-lab/camelbert/blob/master/README.md This Python script fine-tunes CAMeLBERT models for sentiment analysis tasks. It requires setting the `DATA_DIR` and `TASK_NAME` environment variables. The script utilizes the `run_text_classification.py` utility and accepts various arguments for model configuration, training, and evaluation. Ensure the model path and output directory are correctly specified. ```bash export DATA_DIR=/path/to/data export TASK_NAME=arabic_sentiment python run_text_classification.py \ --model_type bert \ --model_name_or_path /path/to/pretrained_model/ \ --task_name $TASK_NAME \ --do_train \ --do_eval \ --eval_all_checkpoints \ --save_steps 500 \ --data_dir $DATA_DIR \ --max_seq_length 128 \ --per_gpu_train_batch_size 32 \ --per_gpu_eval_batch_size 32 \ --learning_rate 3e-5 \ --num_train_epochs 3.0 \ --overwrite_output_dir \ --overwrite_cache \ --output_dir /path/to/output_dir \ --seed 12345 ``` -------------------------------- ### Fine-tune CAMeLBERT for Arabic Sentiment Analysis Source: https://context7.com/camel-lab/camelbert/llms.txt This script fine-tunes a CAMeLBERT model for Arabic sentiment analysis. It requires the Transformers library and a PyTorch environment. The script takes the model path, task name, data directory, and output directory as input. ```bash export DATA_DIR=/path/to/arabic_sentiment_data export TASK_NAME=arabic_sentiment export MODEL_PATH=CAMeL-Lab/bert-base-arabic-camelbert-msa export OUTPUT_DIR=./output/sentiment_model python text-classification/run_text_classification.py \ --model_type bert \ --model_name_or_path $MODEL_PATH \ --task_name $TASK_NAME \ --do_train \ --do_eval \ --eval_all_checkpoints \ --save_steps 500 \ --data_dir $DATA_DIR \ --max_seq_length 128 \ --per_gpu_train_batch_size 32 \ --per_gpu_eval_batch_size 32 \ --learning_rate 3e-5 \ --num_train_epochs 3.0 \ --overwrite_output_dir \ --overwrite_cache \ --output_dir $OUTPUT_DIR \ --seed 12345 # Expected output: # ***** Running training ***** # Num examples = 15000 # Num Epochs = 3 # Total optimization steps = 1407 # ... # ***** Eval results ***** # f1 = 0.8523 # accuracy = 0.8456 # precision = 0.8498 # recall = 0.8549 ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.