imvladikon/hebrew_summarizer

finetuning experiments on summarization tasks for Hebrew

Python

1

3 commits

updated Jul 9, 2023

See the code

README

Summarization Experiments for Hebrew

This directory contains examples for finetuning and evaluating transformers on summarization tasks. Based on the HuggingFace Transformers examples.

Installation

To install the requirements for this example, run:

pip install -r requirements.txt

For development:

pip install -r requirements-dev.txt

Datasets:

Checkpoints:

Training

Here is an example on a summarization task:

python -m summarizer.cli \
    --model_name_or_path "google/mt5-small" \
    --do_train \
    --do_eval \
    --dataset_name <dataset_name> \
    --dataset_config <dataset_config> \
    --source_prefix "summarize: " \
    --output_dir <output_dir_for_checkpoints> \
    --per_device_train_batch_size=4 \
    --per_device_eval_batch_size=4 \
    --overwrite_output_dir \
    --predict_with_generate

Note: only T5 models t5-small, t5-base, t5-large, t5-3b and t5-11b must use an additional argument: --source_prefix "summarize: ".

Resume training

To resume training from a checkpoint, you can use the --resume_from_checkpoint argument. Which is optional, if not specified, in case of --overwrite_output_dir is set, the latest checkpoint in the output directory will be used.

Using local data

For using local data, you can use the --train_file and --validation_file arguments. And need to specify the --text_column and --summary_column, example:

python -m summarizer.cli \
    --model_name_or_path "google/mt5-small" \
    --do_train \
    --do_eval \
    --train_file <path_to_csv_or_jsonlines_file> \
    --validation_file <path_to_csv_or_jsonlines_file> \
    --source_prefix "summarize: " \
    --output_dir <output_dir_for_checkpoints> \
    --overwrite_output_dir \
    --per_device_train_batch_size=4 \
    --per_device_eval_batch_size=4 \
    --predict_with_generate

The task of summarization supports custom CSV and JSONLINES formats.

Custom CSV Files

If it's a csv file the training and validation files should have a column for the inputs texts and a column for the summaries.

If the csv file has just two columns as in the following example:

text,summary
"I'm sitting here in a boring room. It's just another rainy Sunday afternoon. I'm wasting my time I got nothing to do. I'm hanging around I'm waiting for you. But nothing ever happens. And I wonder","I'm sitting in a room where I'm waiting for something to happen"
"I see trees so green, red roses too. I see them bloom for me and you. And I think to myself what a wonderful world. I see skies so blue and clouds so white. The bright blessed day, the dark sacred night. And I think to myself what a wonderful world.","I'm a gardener and I'm a big fan of flowers."
"Christmas time is here. Happiness and cheer. Fun for all that children call. Their favorite time of the year. Snowflakes in the air. Carols everywhere. Olden times and ancient rhymes. Of love and dreams to share","It's that time of year again."

The first column is assumed to be for text and the second is for summary.

If the csv file has multiple columns, you can then specify the names of the columns to use:

    --text_column text_column_name \
    --summary_column summary_column_name \

For example if the columns were:

id,date,text,summary

and you wanted to select only text and summary, then you'd pass these additional arguments:

    --text_column text \
    --summary_column summary \

Custom JSONLINES Files

The second supported format is jsonlines. Here is an example of a jsonlines custom data file.

{"text": "I'm sitting here in a boring room. It's just another rainy Sunday afternoon. I'm wasting my time I got nothing to do. I'm hanging around I'm waiting for you. But nothing ever happens. And I wonder", "summary": "I'm sitting in a room where I'm waiting for something to happen"}
{"text": "I see trees so green, red roses too. I see them bloom for me and you. And I think to myself what a wonderful world. I see skies so blue and clouds so white. The bright blessed day, the dark sacred night. And I think to myself what a wonderful world.", "summary": "I'm a gardener and I'm a big fan of flowers."}
{"text": "Christmas time is here. Happiness and cheer. Fun for all that children call. Their favorite time of the year. Snowflakes in the air. Carols everywhere. Olden times and ancient rhymes. Of love and dreams to share", "summary": "It's that time of year again."}

Same as with the CSV files, by default the first value will be used as the text record and the second as the summary record. Therefore you can use any key names for the entries, in this example text and summary were used.

And as with the CSV files, you can specify which values to select from the file, by explicitly specifying the corresponding key names. In our example this again would be:

    --text_column text \
    --summary_column summary \

Scripts

Check examples in the scripts folder. To run the script, you can use the following command:

chmod +x scripts/<script_name>.sh
./scripts/<script_name>.sh

Evaluation

To evaluate a model on a summarization task, you can use the --do_eval argument. Metrics are computed and logged into output_dir. Currently, the following metrics are supported: ROUGE by hugginface (see here for more details)

Testing

To generate summaries on a summarization task, you can use the --do_predict argument. Predictions are saved into output_dir as generated_predictions.txt.

Inference

from hebrew_summarizer import HebrewSummarizationPipeline

summarizer = HebrewSummarizationPipeline.from_pretrained("imvladikon/het5_small_summarization")
text = """
צרפת ממשיכה לבעור: לאחר ארבעה ימים של עימותים אלימים בין מתפרעים לכוחות הביטחון בכל רחבי צרפת, היום (שבת) התקיימה הלוויתו של הנער האלג'יראי, נאהל בן ה-17, שנורה למוות על ידי שוטר לאחר שנחשד בגניבת רכב. לבקשת משפחתו, ההלוויה התקיימה כאירוע מצומצמם שבו השתתפו בני משפחה וחברים בלבד. לאחר שארונו של נאהל הוצא מהמסגד בעיר נאנטר, אלפים קראו "לעשיית צדק עבורו".במקביל, המשטרה הצרפתית נערכת להמשך המהומות בעשרות מוקדים ברחבי המדינה, כשבמהלך הלילה נעצרו 1,300 בני אדם. משרד הפנים הצרפתי הודיע כי במהלך האירועים הוצתו 1,350 כלי רכב, ו-234 הצתות של מבנים. כמו כן, על פי הנתונים נגרם נזק ל-200 מרכזי קניות, 200 סופרמרקטים ו-250 סניפי בנק.
""".strip()
print(summarizer(text,
           max_length=50,
           num_beams=4,
           no_repeat_ngram_size=2,
           early_stopping=True)[0]["summary_text"])

imvladikon/hebrew_summarizer

finetuning experiments on summarization tasks for Hebrew

Python

1

3 commits

updated Jul 9, 2023

See the code

README

Summarization Experiments for Hebrew

This directory contains examples for finetuning and evaluating transformers on summarization tasks. Based on the HuggingFace Transformers examples.

Installation

To install the requirements for this example, run:

pip install -r requirements.txt

For development:

pip install -r requirements-dev.txt

Datasets:

Checkpoints:

Training

Here is an example on a summarization task:

python -m summarizer.cli \
    --model_name_or_path "google/mt5-small" \
    --do_train \
    --do_eval \
    --dataset_name <dataset_name> \
    --dataset_config <dataset_config> \
    --source_prefix "summarize: " \
    --output_dir <output_dir_for_checkpoints> \
    --per_device_train_batch_size=4 \
    --per_device_eval_batch_size=4 \
    --overwrite_output_dir \
    --predict_with_generate

Note: only T5 models t5-small, t5-base, t5-large, t5-3b and t5-11b must use an additional argument: --source_prefix "summarize: ".

Resume training

To resume training from a checkpoint, you can use the --resume_from_checkpoint argument. Which is optional, if not specified, in case of --overwrite_output_dir is set, the latest checkpoint in the output directory will be used.

Using local data

For using local data, you can use the --train_file and --validation_file arguments. And need to specify the --text_column and --summary_column, example:

python -m summarizer.cli \
    --model_name_or_path "google/mt5-small" \
    --do_train \
    --do_eval \
    --train_file <path_to_csv_or_jsonlines_file> \
    --validation_file <path_to_csv_or_jsonlines_file> \
    --source_prefix "summarize: " \
    --output_dir <output_dir_for_checkpoints> \
    --overwrite_output_dir \
    --per_device_train_batch_size=4 \
    --per_device_eval_batch_size=4 \
    --predict_with_generate

The task of summarization supports custom CSV and JSONLINES formats.

Custom CSV Files

If it's a csv file the training and validation files should have a column for the inputs texts and a column for the summaries.

If the csv file has just two columns as in the following example:

text,summary
"I'm sitting here in a boring room. It's just another rainy Sunday afternoon. I'm wasting my time I got nothing to do. I'm hanging around I'm waiting for you. But nothing ever happens. And I wonder","I'm sitting in a room where I'm waiting for something to happen"
"I see trees so green, red roses too. I see them bloom for me and you. And I think to myself what a wonderful world. I see skies so blue and clouds so white. The bright blessed day, the dark sacred night. And I think to myself what a wonderful world.","I'm a gardener and I'm a big fan of flowers."
"Christmas time is here. Happiness and cheer. Fun for all that children call. Their favorite time of the year. Snowflakes in the air. Carols everywhere. Olden times and ancient rhymes. Of love and dreams to share","It's that time of year again."

The first column is assumed to be for text and the second is for summary.

If the csv file has multiple columns, you can then specify the names of the columns to use:

    --text_column text_column_name \
    --summary_column summary_column_name \

For example if the columns were:

id,date,text,summary

and you wanted to select only text and summary, then you'd pass these additional arguments:

    --text_column text \
    --summary_column summary \

Custom JSONLINES Files

The second supported format is jsonlines. Here is an example of a jsonlines custom data file.

{"text": "I'm sitting here in a boring room. It's just another rainy Sunday afternoon. I'm wasting my time I got nothing to do. I'm hanging around I'm waiting for you. But nothing ever happens. And I wonder", "summary": "I'm sitting in a room where I'm waiting for something to happen"}
{"text": "I see trees so green, red roses too. I see them bloom for me and you. And I think to myself what a wonderful world. I see skies so blue and clouds so white. The bright blessed day, the dark sacred night. And I think to myself what a wonderful world.", "summary": "I'm a gardener and I'm a big fan of flowers."}
{"text": "Christmas time is here. Happiness and cheer. Fun for all that children call. Their favorite time of the year. Snowflakes in the air. Carols everywhere. Olden times and ancient rhymes. Of love and dreams to share", "summary": "It's that time of year again."}

Same as with the CSV files, by default the first value will be used as the text record and the second as the summary record. Therefore you can use any key names for the entries, in this example text and summary were used.

And as with the CSV files, you can specify which values to select from the file, by explicitly specifying the corresponding key names. In our example this again would be:

    --text_column text \
    --summary_column summary \

Scripts

Check examples in the scripts folder. To run the script, you can use the following command:

chmod +x scripts/<script_name>.sh
./scripts/<script_name>.sh

Evaluation

To evaluate a model on a summarization task, you can use the --do_eval argument. Metrics are computed and logged into output_dir. Currently, the following metrics are supported: ROUGE by hugginface (see here for more details)

Testing

To generate summaries on a summarization task, you can use the --do_predict argument. Predictions are saved into output_dir as generated_predictions.txt.

Inference

from hebrew_summarizer import HebrewSummarizationPipeline

summarizer = HebrewSummarizationPipeline.from_pretrained("imvladikon/het5_small_summarization")
text = """
צרפת ממשיכה לבעור: לאחר ארבעה ימים של עימותים אלימים בין מתפרעים לכוחות הביטחון בכל רחבי צרפת, היום (שבת) התקיימה הלוויתו של הנער האלג'יראי, נאהל בן ה-17, שנורה למוות על ידי שוטר לאחר שנחשד בגניבת רכב. לבקשת משפחתו, ההלוויה התקיימה כאירוע מצומצמם שבו השתתפו בני משפחה וחברים בלבד. לאחר שארונו של נאהל הוצא מהמסגד בעיר נאנטר, אלפים קראו "לעשיית צדק עבורו".במקביל, המשטרה הצרפתית נערכת להמשך המהומות בעשרות מוקדים ברחבי המדינה, כשבמהלך הלילה נעצרו 1,300 בני אדם. משרד הפנים הצרפתי הודיע כי במהלך האירועים הוצתו 1,350 כלי רכב, ו-234 הצתות של מבנים. כמו כן, על פי הנתונים נגרם נזק ל-200 מרכזי קניות, 200 סופרמרקטים ו-250 סניפי בנק.
""".strip()
print(summarizer(text,
           max_length=50,
           num_beams=4,
           no_repeat_ngram_size=2,
           early_stopping=True)[0]["summary_text"])