Skip to content

Instantly share code, notes, and snippets.

@moneebullah25
Created June 7, 2023 06:50
Show Gist options
  • Select an option

  • Save moneebullah25/be7fce17a312d0b62293cd4e2bbc6c0c to your computer and use it in GitHub Desktop.

Select an option

Save moneebullah25/be7fce17a312d0b62293cd4e2bbc6c0c to your computer and use it in GitHub Desktop.
Using Hugging Face paraphrasing tools
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"kernelspec": {
"language": "python",
"display_name": "Python 3",
"name": "python3"
},
"language_info": {
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"version": "3.6.4",
"file_extension": ".py",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"name": "python",
"mimetype": "text/x-python"
},
"colab": {
"provenance": []
}
},
"nbformat_minor": 0,
"nbformat": 4,
"cells": [
{
"cell_type": "code",
"source": [
"!pip install -q sentencepiece transformers xformers"
],
"metadata": {
"_uuid": "8f2839f25d086af736a60e9eeb907d3b93b6e0e5",
"_cell_guid": "b1076dfc-b9ad-4769-8c92-a6c4dae69d19",
"execution": {
"iopub.status.busy": "2022-02-28T17:55:06.238535Z",
"iopub.execute_input": "2022-02-28T17:55:06.239695Z",
"iopub.status.idle": "2022-02-28T17:55:18.70821Z",
"shell.execute_reply.started": "2022-02-28T17:55:06.239637Z",
"shell.execute_reply": "2022-02-28T17:55:18.706879Z"
},
"trusted": true,
"id": "_ryvXJGfcilm"
},
"execution_count": 22,
"outputs": []
},
{
"cell_type": "code",
"source": [
"from transformers import *"
],
"metadata": {
"execution": {
"iopub.status.busy": "2022-02-28T17:55:18.7115Z",
"iopub.execute_input": "2022-02-28T17:55:18.711868Z",
"iopub.status.idle": "2022-02-28T17:55:18.722161Z",
"shell.execute_reply.started": "2022-02-28T17:55:18.711821Z",
"shell.execute_reply": "2022-02-28T17:55:18.721168Z"
},
"trusted": true,
"id": "OEfCyAGVcilo"
},
"execution_count": 23,
"outputs": []
},
{
"cell_type": "code",
"source": [
"input_text = \"\"\"\n",
"What started as oxford speech on whether Britain owns reparations to its former colonies has culminated into this seething yet persuasive compendium of arguments for the topic.\n",
"\n",
"The book refutes arguments of The Raj apologists that despite its despotic nature it bestowed India with seeds of modern liberal democratic ideals and built infrastructure that led to what India is today. British did this under the white man’s burden to give back, so they claim.\n",
"\n",
"British never cared for the interest of the Indian people. They resorted to tactical policies of divide & rule, discriminatory recruitment, xenophobic enforcement of laws and procedures, to prolong their stay in the country and ensure continued loot of its resources.\n",
"\n",
"Whether it was trade and agriculture policies, infrastructure development including railways and irrigation, criminal justice system or tax regime, at the heart of every policy was the imperial mindset aimed at enriching the coffers of the company or crown government later on.\n",
"\n",
"Unlike the previous despots in the country such as the Mughals or the Delhi Sultans, the British never assimilated in the Indian milieu and never had the intent to do so. This alienation was also reflected in their policies which never benefitted the Indians.\n",
"\n",
"While cricket and English language may be cherished by many Indians, they too were a byproduct of the British presence rather than a conscious effort of their percolation into the Indian society. So were the other legacies such as railways infrastructure or colonial laws.\n",
"\n",
"Finally, it is not important to arrive at the amount of reparation which the British owe to Indian people. More important is atonement and sincere apology that British owe to Indians and other former colonies.\n",
"\"\"\""
],
"metadata": {
"execution": {
"iopub.status.busy": "2022-02-28T17:56:09.303518Z",
"iopub.execute_input": "2022-02-28T17:56:09.304309Z",
"iopub.status.idle": "2022-02-28T17:56:09.310641Z",
"shell.execute_reply.started": "2022-02-28T17:56:09.304258Z",
"shell.execute_reply": "2022-02-28T17:56:09.309685Z"
},
"trusted": true,
"id": "nLJm_9eZcilr"
},
"execution_count": 24,
"outputs": []
},
{
"cell_type": "code",
"source": [
"def get_paraphrased_passage( input_text, model, tokenizer, num_return_sequences=5, num_beams=5):\n",
" \n",
" sentences = input_text.split('.')\n",
" \n",
" all_sentences = []\n",
" \n",
" for sentence in sentences:\n",
" # tokenize the text to be form of a list of token IDs\n",
" inputs = tokenizer([sentence], truncation=True, padding=\"longest\", return_tensors=\"pt\")\n",
" # generate the paraphrased sentences\n",
" outputs = model.generate(\n",
" **inputs,\n",
" num_beams=num_beams,\n",
" num_return_sequences=num_return_sequences,\n",
" )\n",
" # decode the generated sentences using the tokenizer to get them back to text\n",
" result = tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]\n",
" result = result[0].upper() + result[1:]\n",
" all_sentences.append(result)\n",
" \n",
" return \"\".join(all_sentences)"
],
"metadata": {
"id": "hV9Hl-Nkh-QY"
},
"execution_count": 25,
"outputs": []
},
{
"cell_type": "code",
"source": [
"def get_paraphrased_sentences(model, tokenizer, sentence, num_return_sequences=5, num_beams=5):\n",
" # tokenize the text to be form of a list of token IDs\n",
" inputs = tokenizer([sentence], truncation=True, padding=\"longest\", return_tensors=\"pt\")\n",
" # generate the paraphrased sentences\n",
" outputs = model.generate(\n",
" **inputs,\n",
" num_beams=num_beams,\n",
" num_return_sequences=num_return_sequences,\n",
" )\n",
" # decode the generated sentences using the tokenizer to get them back to text\n",
" return tokenizer.batch_decode(outputs, skip_special_tokens=True)"
],
"metadata": {
"id": "AcQh7y1yiHBI"
},
"execution_count": 26,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"# using Pegasus"
],
"metadata": {
"id": "accuHdVucilo"
}
},
{
"cell_type": "code",
"source": [
"model_pegasus = PegasusForConditionalGeneration.from_pretrained(\"tuner007/pegasus_paraphrase\")\n",
"tokenizer_pegasus = PegasusTokenizerFast.from_pretrained(\"tuner007/pegasus_paraphrase\")"
],
"metadata": {
"execution": {
"iopub.status.busy": "2022-02-28T17:55:18.724075Z",
"iopub.execute_input": "2022-02-28T17:55:18.72433Z",
"iopub.status.idle": "2022-02-28T17:55:44.938678Z",
"shell.execute_reply.started": "2022-02-28T17:55:18.724302Z",
"shell.execute_reply": "2022-02-28T17:55:44.937821Z"
},
"trusted": true,
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "1XgRD5t_cilp",
"outputId": "e4a29365-9dc2-4283-91f7-4262f734f98c"
},
"execution_count": 27,
"outputs": [
{
"output_type": "stream",
"name": "stderr",
"text": [
"loading configuration file config.json from cache at /root/.cache/huggingface/hub/models--tuner007--pegasus_paraphrase/snapshots/0159e2949ca73657a2f1329898f51b7bb53b9ab2/config.json\n",
"Model config PegasusConfig {\n",
" \"activation_dropout\": 0.1,\n",
" \"activation_function\": \"relu\",\n",
" \"add_bias_logits\": false,\n",
" \"add_final_layer_norm\": true,\n",
" \"architectures\": [\n",
" \"PegasusForConditionalGeneration\"\n",
" ],\n",
" \"attention_dropout\": 0.1,\n",
" \"bos_token_id\": 0,\n",
" \"classif_dropout\": 0.0,\n",
" \"d_model\": 1024,\n",
" \"decoder_attention_heads\": 16,\n",
" \"decoder_ffn_dim\": 4096,\n",
" \"decoder_layerdrop\": 0.0,\n",
" \"decoder_layers\": 16,\n",
" \"decoder_start_token_id\": 0,\n",
" \"dropout\": 0.1,\n",
" \"encoder_attention_heads\": 16,\n",
" \"encoder_ffn_dim\": 4096,\n",
" \"encoder_layerdrop\": 0.0,\n",
" \"encoder_layers\": 16,\n",
" \"eos_token_id\": 1,\n",
" \"extra_pos_embeddings\": 1,\n",
" \"force_bos_token_to_be_generated\": false,\n",
" \"forced_eos_token_id\": 1,\n",
" \"id2label\": {\n",
" \"0\": \"LABEL_0\",\n",
" \"1\": \"LABEL_1\",\n",
" \"2\": \"LABEL_2\"\n",
" },\n",
" \"init_std\": 0.02,\n",
" \"is_encoder_decoder\": true,\n",
" \"label2id\": {\n",
" \"LABEL_0\": 0,\n",
" \"LABEL_1\": 1,\n",
" \"LABEL_2\": 2\n",
" },\n",
" \"length_penalty\": 0.8,\n",
" \"max_length\": 60,\n",
" \"max_position_embeddings\": 60,\n",
" \"model_type\": \"pegasus\",\n",
" \"normalize_before\": true,\n",
" \"normalize_embedding\": false,\n",
" \"num_beams\": 8,\n",
" \"num_hidden_layers\": 16,\n",
" \"pad_token_id\": 0,\n",
" \"scale_embedding\": true,\n",
" \"static_position_embeddings\": true,\n",
" \"transformers_version\": \"4.29.2\",\n",
" \"use_cache\": true,\n",
" \"vocab_size\": 96103\n",
"}\n",
"\n",
"loading weights file pytorch_model.bin from cache at /root/.cache/huggingface/hub/models--tuner007--pegasus_paraphrase/snapshots/0159e2949ca73657a2f1329898f51b7bb53b9ab2/pytorch_model.bin\n",
"Generate config GenerationConfig {\n",
" \"_from_model_config\": true,\n",
" \"bos_token_id\": 0,\n",
" \"decoder_start_token_id\": 0,\n",
" \"eos_token_id\": 1,\n",
" \"forced_eos_token_id\": 1,\n",
" \"length_penalty\": 0.8,\n",
" \"max_length\": 60,\n",
" \"num_beams\": 8,\n",
" \"pad_token_id\": 0,\n",
" \"transformers_version\": \"4.29.2\"\n",
"}\n",
"\n",
"All model checkpoint weights were used when initializing PegasusForConditionalGeneration.\n",
"\n",
"All the weights of PegasusForConditionalGeneration were initialized from the model checkpoint at tuner007/pegasus_paraphrase.\n",
"If your task is similar to the task the model of the checkpoint was trained on, you can already use PegasusForConditionalGeneration for predictions without further training.\n",
"Generation config file not found, using a generation config created from the model config.\n",
"loading file spiece.model from cache at /root/.cache/huggingface/hub/models--tuner007--pegasus_paraphrase/snapshots/0159e2949ca73657a2f1329898f51b7bb53b9ab2/spiece.model\n",
"loading file tokenizer.json from cache at None\n",
"loading file added_tokens.json from cache at None\n",
"loading file special_tokens_map.json from cache at /root/.cache/huggingface/hub/models--tuner007--pegasus_paraphrase/snapshots/0159e2949ca73657a2f1329898f51b7bb53b9ab2/special_tokens_map.json\n",
"loading file tokenizer_config.json from cache at /root/.cache/huggingface/hub/models--tuner007--pegasus_paraphrase/snapshots/0159e2949ca73657a2f1329898f51b7bb53b9ab2/tokenizer_config.json\n",
"loading configuration file config.json from cache at /root/.cache/huggingface/hub/models--tuner007--pegasus_paraphrase/snapshots/0159e2949ca73657a2f1329898f51b7bb53b9ab2/config.json\n",
"Model config PegasusConfig {\n",
" \"_name_or_path\": \"tuner007/pegasus_paraphrase\",\n",
" \"activation_dropout\": 0.1,\n",
" \"activation_function\": \"relu\",\n",
" \"add_bias_logits\": false,\n",
" \"add_final_layer_norm\": true,\n",
" \"architectures\": [\n",
" \"PegasusForConditionalGeneration\"\n",
" ],\n",
" \"attention_dropout\": 0.1,\n",
" \"bos_token_id\": 0,\n",
" \"classif_dropout\": 0.0,\n",
" \"d_model\": 1024,\n",
" \"decoder_attention_heads\": 16,\n",
" \"decoder_ffn_dim\": 4096,\n",
" \"decoder_layerdrop\": 0.0,\n",
" \"decoder_layers\": 16,\n",
" \"decoder_start_token_id\": 0,\n",
" \"dropout\": 0.1,\n",
" \"encoder_attention_heads\": 16,\n",
" \"encoder_ffn_dim\": 4096,\n",
" \"encoder_layerdrop\": 0.0,\n",
" \"encoder_layers\": 16,\n",
" \"eos_token_id\": 1,\n",
" \"extra_pos_embeddings\": 1,\n",
" \"force_bos_token_to_be_generated\": false,\n",
" \"forced_eos_token_id\": 1,\n",
" \"id2label\": {\n",
" \"0\": \"LABEL_0\",\n",
" \"1\": \"LABEL_1\",\n",
" \"2\": \"LABEL_2\"\n",
" },\n",
" \"init_std\": 0.02,\n",
" \"is_encoder_decoder\": true,\n",
" \"label2id\": {\n",
" \"LABEL_0\": 0,\n",
" \"LABEL_1\": 1,\n",
" \"LABEL_2\": 2\n",
" },\n",
" \"length_penalty\": 0.8,\n",
" \"max_length\": 60,\n",
" \"max_position_embeddings\": 60,\n",
" \"model_type\": \"pegasus\",\n",
" \"normalize_before\": true,\n",
" \"normalize_embedding\": false,\n",
" \"num_beams\": 8,\n",
" \"num_hidden_layers\": 16,\n",
" \"pad_token_id\": 0,\n",
" \"scale_embedding\": true,\n",
" \"static_position_embeddings\": true,\n",
" \"transformers_version\": \"4.29.2\",\n",
" \"use_cache\": true,\n",
" \"vocab_size\": 96103\n",
"}\n",
"\n",
"loading configuration file config.json from cache at /root/.cache/huggingface/hub/models--tuner007--pegasus_paraphrase/snapshots/0159e2949ca73657a2f1329898f51b7bb53b9ab2/config.json\n",
"Model config PegasusConfig {\n",
" \"_name_or_path\": \"tuner007/pegasus_paraphrase\",\n",
" \"activation_dropout\": 0.1,\n",
" \"activation_function\": \"relu\",\n",
" \"add_bias_logits\": false,\n",
" \"add_final_layer_norm\": true,\n",
" \"architectures\": [\n",
" \"PegasusForConditionalGeneration\"\n",
" ],\n",
" \"attention_dropout\": 0.1,\n",
" \"bos_token_id\": 0,\n",
" \"classif_dropout\": 0.0,\n",
" \"d_model\": 1024,\n",
" \"decoder_attention_heads\": 16,\n",
" \"decoder_ffn_dim\": 4096,\n",
" \"decoder_layerdrop\": 0.0,\n",
" \"decoder_layers\": 16,\n",
" \"decoder_start_token_id\": 0,\n",
" \"dropout\": 0.1,\n",
" \"encoder_attention_heads\": 16,\n",
" \"encoder_ffn_dim\": 4096,\n",
" \"encoder_layerdrop\": 0.0,\n",
" \"encoder_layers\": 16,\n",
" \"eos_token_id\": 1,\n",
" \"extra_pos_embeddings\": 1,\n",
" \"force_bos_token_to_be_generated\": false,\n",
" \"forced_eos_token_id\": 1,\n",
" \"id2label\": {\n",
" \"0\": \"LABEL_0\",\n",
" \"1\": \"LABEL_1\",\n",
" \"2\": \"LABEL_2\"\n",
" },\n",
" \"init_std\": 0.02,\n",
" \"is_encoder_decoder\": true,\n",
" \"label2id\": {\n",
" \"LABEL_0\": 0,\n",
" \"LABEL_1\": 1,\n",
" \"LABEL_2\": 2\n",
" },\n",
" \"length_penalty\": 0.8,\n",
" \"max_length\": 60,\n",
" \"max_position_embeddings\": 60,\n",
" \"model_type\": \"pegasus\",\n",
" \"normalize_before\": true,\n",
" \"normalize_embedding\": false,\n",
" \"num_beams\": 8,\n",
" \"num_hidden_layers\": 16,\n",
" \"pad_token_id\": 0,\n",
" \"scale_embedding\": true,\n",
" \"static_position_embeddings\": true,\n",
" \"transformers_version\": \"4.29.2\",\n",
" \"use_cache\": true,\n",
" \"vocab_size\": 96103\n",
"}\n",
"\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"sentence = \"Learning is the process of acquiring new understanding, knowledge, behaviors, skills, values, attitudes, and preferences.\"\n",
"get_paraphrased_sentences(model_pegasus, tokenizer_pegasus, sentence, num_beams=5, num_return_sequences=5)"
],
"metadata": {
"execution": {
"iopub.status.busy": "2022-02-28T17:55:44.952351Z",
"iopub.execute_input": "2022-02-28T17:55:44.953001Z",
"iopub.status.idle": "2022-02-28T17:55:46.963919Z",
"shell.execute_reply.started": "2022-02-28T17:55:44.952947Z",
"shell.execute_reply": "2022-02-28T17:55:46.963303Z"
},
"trusted": true,
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "oDqt85Jecilp",
"outputId": "ec33b97f-8535-4e8b-bc3f-6b4a324ac901"
},
"execution_count": 28,
"outputs": [
{
"output_type": "stream",
"name": "stderr",
"text": [
"Generate config GenerationConfig {\n",
" \"_from_model_config\": true,\n",
" \"bos_token_id\": 0,\n",
" \"decoder_start_token_id\": 0,\n",
" \"eos_token_id\": 1,\n",
" \"forced_eos_token_id\": 1,\n",
" \"length_penalty\": 0.8,\n",
" \"max_length\": 60,\n",
" \"num_beams\": 8,\n",
" \"pad_token_id\": 0,\n",
" \"transformers_version\": \"4.29.2\"\n",
"}\n",
"\n"
]
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"['Learning is the acquisition of new understanding, knowledge, behaviors, skills, values, attitudes, and preferences.',\n",
" 'New understanding, knowledge, behaviors, skills, values, attitudes, and preferences are acquired through learning.',\n",
" 'Learning is the acquisition of new understanding, knowledge, behaviors, skills, values, attitudes and preferences.',\n",
" 'New understanding, knowledge, behaviors, skills, values, attitudes, and preferences are all acquired through learning.',\n",
" 'New understanding, knowledge, behaviors, skills, values, attitudes, and preferences are some of the things learning is about.']"
]
},
"metadata": {},
"execution_count": 28
}
]
},
{
"cell_type": "code",
"source": [
"get_paraphrased_passage(input_text, model_pegasus, tokenizer_pegasus)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 1000
},
"id": "xVhoEabAhm6w",
"outputId": "42655c13-e591-4af3-a04a-c0cd7cb25925"
},
"execution_count": 29,
"outputs": [
{
"output_type": "stream",
"name": "stderr",
"text": [
"Generate config GenerationConfig {\n",
" \"_from_model_config\": true,\n",
" \"bos_token_id\": 0,\n",
" \"decoder_start_token_id\": 0,\n",
" \"eos_token_id\": 1,\n",
" \"forced_eos_token_id\": 1,\n",
" \"length_penalty\": 0.8,\n",
" \"max_length\": 60,\n",
" \"num_beams\": 8,\n",
" \"pad_token_id\": 0,\n",
" \"transformers_version\": \"4.29.2\"\n",
"}\n",
"\n",
"Generate config GenerationConfig {\n",
" \"_from_model_config\": true,\n",
" \"bos_token_id\": 0,\n",
" \"decoder_start_token_id\": 0,\n",
" \"eos_token_id\": 1,\n",
" \"forced_eos_token_id\": 1,\n",
" \"length_penalty\": 0.8,\n",
" \"max_length\": 60,\n",
" \"num_beams\": 8,\n",
" \"pad_token_id\": 0,\n",
" \"transformers_version\": \"4.29.2\"\n",
"}\n",
"\n",
"Generate config GenerationConfig {\n",
" \"_from_model_config\": true,\n",
" \"bos_token_id\": 0,\n",
" \"decoder_start_token_id\": 0,\n",
" \"eos_token_id\": 1,\n",
" \"forced_eos_token_id\": 1,\n",
" \"length_penalty\": 0.8,\n",
" \"max_length\": 60,\n",
" \"num_beams\": 8,\n",
" \"pad_token_id\": 0,\n",
" \"transformers_version\": \"4.29.2\"\n",
"}\n",
"\n",
"Generate config GenerationConfig {\n",
" \"_from_model_config\": true,\n",
" \"bos_token_id\": 0,\n",
" \"decoder_start_token_id\": 0,\n",
" \"eos_token_id\": 1,\n",
" \"forced_eos_token_id\": 1,\n",
" \"length_penalty\": 0.8,\n",
" \"max_length\": 60,\n",
" \"num_beams\": 8,\n",
" \"pad_token_id\": 0,\n",
" \"transformers_version\": \"4.29.2\"\n",
"}\n",
"\n",
"Generate config GenerationConfig {\n",
" \"_from_model_config\": true,\n",
" \"bos_token_id\": 0,\n",
" \"decoder_start_token_id\": 0,\n",
" \"eos_token_id\": 1,\n",
" \"forced_eos_token_id\": 1,\n",
" \"length_penalty\": 0.8,\n",
" \"max_length\": 60,\n",
" \"num_beams\": 8,\n",
" \"pad_token_id\": 0,\n",
" \"transformers_version\": \"4.29.2\"\n",
"}\n",
"\n",
"Generate config GenerationConfig {\n",
" \"_from_model_config\": true,\n",
" \"bos_token_id\": 0,\n",
" \"decoder_start_token_id\": 0,\n",
" \"eos_token_id\": 1,\n",
" \"forced_eos_token_id\": 1,\n",
" \"length_penalty\": 0.8,\n",
" \"max_length\": 60,\n",
" \"num_beams\": 8,\n",
" \"pad_token_id\": 0,\n",
" \"transformers_version\": \"4.29.2\"\n",
"}\n",
"\n",
"Generate config GenerationConfig {\n",
" \"_from_model_config\": true,\n",
" \"bos_token_id\": 0,\n",
" \"decoder_start_token_id\": 0,\n",
" \"eos_token_id\": 1,\n",
" \"forced_eos_token_id\": 1,\n",
" \"length_penalty\": 0.8,\n",
" \"max_length\": 60,\n",
" \"num_beams\": 8,\n",
" \"pad_token_id\": 0,\n",
" \"transformers_version\": \"4.29.2\"\n",
"}\n",
"\n",
"Generate config GenerationConfig {\n",
" \"_from_model_config\": true,\n",
" \"bos_token_id\": 0,\n",
" \"decoder_start_token_id\": 0,\n",
" \"eos_token_id\": 1,\n",
" \"forced_eos_token_id\": 1,\n",
" \"length_penalty\": 0.8,\n",
" \"max_length\": 60,\n",
" \"num_beams\": 8,\n",
" \"pad_token_id\": 0,\n",
" \"transformers_version\": \"4.29.2\"\n",
"}\n",
"\n",
"Generate config GenerationConfig {\n",
" \"_from_model_config\": true,\n",
" \"bos_token_id\": 0,\n",
" \"decoder_start_token_id\": 0,\n",
" \"eos_token_id\": 1,\n",
" \"forced_eos_token_id\": 1,\n",
" \"length_penalty\": 0.8,\n",
" \"max_length\": 60,\n",
" \"num_beams\": 8,\n",
" \"pad_token_id\": 0,\n",
" \"transformers_version\": \"4.29.2\"\n",
"}\n",
"\n",
"Generate config GenerationConfig {\n",
" \"_from_model_config\": true,\n",
" \"bos_token_id\": 0,\n",
" \"decoder_start_token_id\": 0,\n",
" \"eos_token_id\": 1,\n",
" \"forced_eos_token_id\": 1,\n",
" \"length_penalty\": 0.8,\n",
" \"max_length\": 60,\n",
" \"num_beams\": 8,\n",
" \"pad_token_id\": 0,\n",
" \"transformers_version\": \"4.29.2\"\n",
"}\n",
"\n",
"Generate config GenerationConfig {\n",
" \"_from_model_config\": true,\n",
" \"bos_token_id\": 0,\n",
" \"decoder_start_token_id\": 0,\n",
" \"eos_token_id\": 1,\n",
" \"forced_eos_token_id\": 1,\n",
" \"length_penalty\": 0.8,\n",
" \"max_length\": 60,\n",
" \"num_beams\": 8,\n",
" \"pad_token_id\": 0,\n",
" \"transformers_version\": \"4.29.2\"\n",
"}\n",
"\n",
"Generate config GenerationConfig {\n",
" \"_from_model_config\": true,\n",
" \"bos_token_id\": 0,\n",
" \"decoder_start_token_id\": 0,\n",
" \"eos_token_id\": 1,\n",
" \"forced_eos_token_id\": 1,\n",
" \"length_penalty\": 0.8,\n",
" \"max_length\": 60,\n",
" \"num_beams\": 8,\n",
" \"pad_token_id\": 0,\n",
" \"transformers_version\": \"4.29.2\"\n",
"}\n",
"\n",
"Generate config GenerationConfig {\n",
" \"_from_model_config\": true,\n",
" \"bos_token_id\": 0,\n",
" \"decoder_start_token_id\": 0,\n",
" \"eos_token_id\": 1,\n",
" \"forced_eos_token_id\": 1,\n",
" \"length_penalty\": 0.8,\n",
" \"max_length\": 60,\n",
" \"num_beams\": 8,\n",
" \"pad_token_id\": 0,\n",
" \"transformers_version\": \"4.29.2\"\n",
"}\n",
"\n"
]
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"\"This seething yet persuasive compendium of arguments for the topic was the culmination of what started as an Oxford speech.The Raj apologists argue that India was given seeds of modern liberal democratic ideals and built infrastructure that led to what India is today.British did this under the burden of the white man.The interest of the Indian people was never cared for by the British.They use tactical policies to prolong their stay in the country and ensure continued loot of the country's resources.Whether it was trade and agriculture policies, infrastructure development including railways and irrigation, criminal justice system or tax regime, at the heart of every policy was the imperial mindset aimed at enriching the coffers of the company or crown government later on.The British never had the intent to integrate into the Indian culture, unlike the Mughals or the Delhi Sultans.Their policies never benefited the Indians.While cricket and English language may be cherished by many Indians, they too were a result of the British presence rather than a conscious effort of their percolation into the Indian society.Railways infrastructure or colonial laws were legacies.It's not important to arrive at the amount of reparation the British owe to Indian people.British owe an apology to Indians and other former colonies.There is a possibility that there is a possibility that there is a possibility that there is a possibility that there is a possibility that there is a possibility that there is\""
],
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "string"
}
},
"metadata": {},
"execution_count": 29
}
]
},
{
"cell_type": "markdown",
"source": [
"# Using PAWS"
],
"metadata": {
"id": "ByriReNRcilq"
}
},
{
"cell_type": "code",
"source": [
"paws_tokenizer = AutoTokenizer.from_pretrained(\"Vamsi/T5_Paraphrase_Paws\")\n",
"paws_model = AutoModelForSeq2SeqLM.from_pretrained(\"Vamsi/T5_Paraphrase_Paws\")"
],
"metadata": {
"execution": {
"iopub.status.busy": "2022-02-28T17:55:46.965078Z",
"iopub.execute_input": "2022-02-28T17:55:46.965455Z",
"iopub.status.idle": "2022-02-28T17:56:07.559035Z",
"shell.execute_reply.started": "2022-02-28T17:55:46.965424Z",
"shell.execute_reply": "2022-02-28T17:56:07.558327Z"
},
"trusted": true,
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "JBS0UMaZcilq",
"outputId": "34f4cc53-dbe8-4b44-e953-15ce0ab162f5"
},
"execution_count": 30,
"outputs": [
{
"output_type": "stream",
"name": "stderr",
"text": [
"loading configuration file config.json from cache at /root/.cache/huggingface/hub/models--Vamsi--T5_Paraphrase_Paws/snapshots/3bbf07dc42d5ddc9ca77c5589ce7239b0b731832/config.json\n",
"Model config T5Config {\n",
" \"_name_or_path\": \"Vamsi/T5_Paraphrase_Paws\",\n",
" \"architectures\": [\n",
" \"T5ForConditionalGeneration\"\n",
" ],\n",
" \"d_ff\": 3072,\n",
" \"d_kv\": 64,\n",
" \"d_model\": 768,\n",
" \"decoder_start_token_id\": 0,\n",
" \"dense_act_fn\": \"relu\",\n",
" \"dropout_rate\": 0.1,\n",
" \"eos_token_id\": 1,\n",
" \"feed_forward_proj\": \"relu\",\n",
" \"initializer_factor\": 1.0,\n",
" \"is_encoder_decoder\": true,\n",
" \"is_gated_act\": false,\n",
" \"layer_norm_epsilon\": 1e-06,\n",
" \"model_type\": \"t5\",\n",
" \"n_positions\": 512,\n",
" \"num_decoder_layers\": 12,\n",
" \"num_heads\": 12,\n",
" \"num_layers\": 12,\n",
" \"output_past\": true,\n",
" \"pad_token_id\": 0,\n",
" \"relative_attention_max_distance\": 128,\n",
" \"relative_attention_num_buckets\": 32,\n",
" \"task_specific_params\": {\n",
" \"summarization\": {\n",
" \"early_stopping\": true,\n",
" \"length_penalty\": 2.0,\n",
" \"max_length\": 200,\n",
" \"min_length\": 30,\n",
" \"no_repeat_ngram_size\": 3,\n",
" \"num_beams\": 4,\n",
" \"prefix\": \"summarize: \"\n",
" },\n",
" \"translation_en_to_de\": {\n",
" \"early_stopping\": true,\n",
" \"max_length\": 300,\n",
" \"num_beams\": 4,\n",
" \"prefix\": \"translate English to German: \"\n",
" },\n",
" \"translation_en_to_fr\": {\n",
" \"early_stopping\": true,\n",
" \"max_length\": 300,\n",
" \"num_beams\": 4,\n",
" \"prefix\": \"translate English to French: \"\n",
" },\n",
" \"translation_en_to_ro\": {\n",
" \"early_stopping\": true,\n",
" \"max_length\": 300,\n",
" \"num_beams\": 4,\n",
" \"prefix\": \"translate English to Romanian: \"\n",
" }\n",
" },\n",
" \"transformers_version\": \"4.29.2\",\n",
" \"use_cache\": true,\n",
" \"vocab_size\": 32128\n",
"}\n",
"\n",
"loading file spiece.model from cache at /root/.cache/huggingface/hub/models--Vamsi--T5_Paraphrase_Paws/snapshots/3bbf07dc42d5ddc9ca77c5589ce7239b0b731832/spiece.model\n",
"loading file tokenizer.json from cache at None\n",
"loading file added_tokens.json from cache at None\n",
"loading file special_tokens_map.json from cache at /root/.cache/huggingface/hub/models--Vamsi--T5_Paraphrase_Paws/snapshots/3bbf07dc42d5ddc9ca77c5589ce7239b0b731832/special_tokens_map.json\n",
"loading file tokenizer_config.json from cache at /root/.cache/huggingface/hub/models--Vamsi--T5_Paraphrase_Paws/snapshots/3bbf07dc42d5ddc9ca77c5589ce7239b0b731832/tokenizer_config.json\n",
"loading configuration file config.json from cache at /root/.cache/huggingface/hub/models--Vamsi--T5_Paraphrase_Paws/snapshots/3bbf07dc42d5ddc9ca77c5589ce7239b0b731832/config.json\n",
"Model config T5Config {\n",
" \"_name_or_path\": \"Vamsi/T5_Paraphrase_Paws\",\n",
" \"architectures\": [\n",
" \"T5ForConditionalGeneration\"\n",
" ],\n",
" \"d_ff\": 3072,\n",
" \"d_kv\": 64,\n",
" \"d_model\": 768,\n",
" \"decoder_start_token_id\": 0,\n",
" \"dense_act_fn\": \"relu\",\n",
" \"dropout_rate\": 0.1,\n",
" \"eos_token_id\": 1,\n",
" \"feed_forward_proj\": \"relu\",\n",
" \"initializer_factor\": 1.0,\n",
" \"is_encoder_decoder\": true,\n",
" \"is_gated_act\": false,\n",
" \"layer_norm_epsilon\": 1e-06,\n",
" \"model_type\": \"t5\",\n",
" \"n_positions\": 512,\n",
" \"num_decoder_layers\": 12,\n",
" \"num_heads\": 12,\n",
" \"num_layers\": 12,\n",
" \"output_past\": true,\n",
" \"pad_token_id\": 0,\n",
" \"relative_attention_max_distance\": 128,\n",
" \"relative_attention_num_buckets\": 32,\n",
" \"task_specific_params\": {\n",
" \"summarization\": {\n",
" \"early_stopping\": true,\n",
" \"length_penalty\": 2.0,\n",
" \"max_length\": 200,\n",
" \"min_length\": 30,\n",
" \"no_repeat_ngram_size\": 3,\n",
" \"num_beams\": 4,\n",
" \"prefix\": \"summarize: \"\n",
" },\n",
" \"translation_en_to_de\": {\n",
" \"early_stopping\": true,\n",
" \"max_length\": 300,\n",
" \"num_beams\": 4,\n",
" \"prefix\": \"translate English to German: \"\n",
" },\n",
" \"translation_en_to_fr\": {\n",
" \"early_stopping\": true,\n",
" \"max_length\": 300,\n",
" \"num_beams\": 4,\n",
" \"prefix\": \"translate English to French: \"\n",
" },\n",
" \"translation_en_to_ro\": {\n",
" \"early_stopping\": true,\n",
" \"max_length\": 300,\n",
" \"num_beams\": 4,\n",
" \"prefix\": \"translate English to Romanian: \"\n",
" }\n",
" },\n",
" \"transformers_version\": \"4.29.2\",\n",
" \"use_cache\": true,\n",
" \"vocab_size\": 32128\n",
"}\n",
"\n",
"loading configuration file config.json from cache at /root/.cache/huggingface/hub/models--Vamsi--T5_Paraphrase_Paws/snapshots/3bbf07dc42d5ddc9ca77c5589ce7239b0b731832/config.json\n",
"Model config T5Config {\n",
" \"_name_or_path\": \"Vamsi/T5_Paraphrase_Paws\",\n",
" \"architectures\": [\n",
" \"T5ForConditionalGeneration\"\n",
" ],\n",
" \"d_ff\": 3072,\n",
" \"d_kv\": 64,\n",
" \"d_model\": 768,\n",
" \"decoder_start_token_id\": 0,\n",
" \"dense_act_fn\": \"relu\",\n",
" \"dropout_rate\": 0.1,\n",
" \"eos_token_id\": 1,\n",
" \"feed_forward_proj\": \"relu\",\n",
" \"initializer_factor\": 1.0,\n",
" \"is_encoder_decoder\": true,\n",
" \"is_gated_act\": false,\n",
" \"layer_norm_epsilon\": 1e-06,\n",
" \"model_type\": \"t5\",\n",
" \"n_positions\": 512,\n",
" \"num_decoder_layers\": 12,\n",
" \"num_heads\": 12,\n",
" \"num_layers\": 12,\n",
" \"output_past\": true,\n",
" \"pad_token_id\": 0,\n",
" \"relative_attention_max_distance\": 128,\n",
" \"relative_attention_num_buckets\": 32,\n",
" \"task_specific_params\": {\n",
" \"summarization\": {\n",
" \"early_stopping\": true,\n",
" \"length_penalty\": 2.0,\n",
" \"max_length\": 200,\n",
" \"min_length\": 30,\n",
" \"no_repeat_ngram_size\": 3,\n",
" \"num_beams\": 4,\n",
" \"prefix\": \"summarize: \"\n",
" },\n",
" \"translation_en_to_de\": {\n",
" \"early_stopping\": true,\n",
" \"max_length\": 300,\n",
" \"num_beams\": 4,\n",
" \"prefix\": \"translate English to German: \"\n",
" },\n",
" \"translation_en_to_fr\": {\n",
" \"early_stopping\": true,\n",
" \"max_length\": 300,\n",
" \"num_beams\": 4,\n",
" \"prefix\": \"translate English to French: \"\n",
" },\n",
" \"translation_en_to_ro\": {\n",
" \"early_stopping\": true,\n",
" \"max_length\": 300,\n",
" \"num_beams\": 4,\n",
" \"prefix\": \"translate English to Romanian: \"\n",
" }\n",
" },\n",
" \"transformers_version\": \"4.29.2\",\n",
" \"use_cache\": true,\n",
" \"vocab_size\": 32128\n",
"}\n",
"\n",
"loading configuration file config.json from cache at /root/.cache/huggingface/hub/models--Vamsi--T5_Paraphrase_Paws/snapshots/3bbf07dc42d5ddc9ca77c5589ce7239b0b731832/config.json\n",
"Model config T5Config {\n",
" \"_name_or_path\": \"Vamsi/T5_Paraphrase_Paws\",\n",
" \"architectures\": [\n",
" \"T5ForConditionalGeneration\"\n",
" ],\n",
" \"d_ff\": 3072,\n",
" \"d_kv\": 64,\n",
" \"d_model\": 768,\n",
" \"decoder_start_token_id\": 0,\n",
" \"dense_act_fn\": \"relu\",\n",
" \"dropout_rate\": 0.1,\n",
" \"eos_token_id\": 1,\n",
" \"feed_forward_proj\": \"relu\",\n",
" \"initializer_factor\": 1.0,\n",
" \"is_encoder_decoder\": true,\n",
" \"is_gated_act\": false,\n",
" \"layer_norm_epsilon\": 1e-06,\n",
" \"model_type\": \"t5\",\n",
" \"n_positions\": 512,\n",
" \"num_decoder_layers\": 12,\n",
" \"num_heads\": 12,\n",
" \"num_layers\": 12,\n",
" \"output_past\": true,\n",
" \"pad_token_id\": 0,\n",
" \"relative_attention_max_distance\": 128,\n",
" \"relative_attention_num_buckets\": 32,\n",
" \"task_specific_params\": {\n",
" \"summarization\": {\n",
" \"early_stopping\": true,\n",
" \"length_penalty\": 2.0,\n",
" \"max_length\": 200,\n",
" \"min_length\": 30,\n",
" \"no_repeat_ngram_size\": 3,\n",
" \"num_beams\": 4,\n",
" \"prefix\": \"summarize: \"\n",
" },\n",
" \"translation_en_to_de\": {\n",
" \"early_stopping\": true,\n",
" \"max_length\": 300,\n",
" \"num_beams\": 4,\n",
" \"prefix\": \"translate English to German: \"\n",
" },\n",
" \"translation_en_to_fr\": {\n",
" \"early_stopping\": true,\n",
" \"max_length\": 300,\n",
" \"num_beams\": 4,\n",
" \"prefix\": \"translate English to French: \"\n",
" },\n",
" \"translation_en_to_ro\": {\n",
" \"early_stopping\": true,\n",
" \"max_length\": 300,\n",
" \"num_beams\": 4,\n",
" \"prefix\": \"translate English to Romanian: \"\n",
" }\n",
" },\n",
" \"transformers_version\": \"4.29.2\",\n",
" \"use_cache\": true,\n",
" \"vocab_size\": 32128\n",
"}\n",
"\n",
"loading weights file pytorch_model.bin from cache at /root/.cache/huggingface/hub/models--Vamsi--T5_Paraphrase_Paws/snapshots/3bbf07dc42d5ddc9ca77c5589ce7239b0b731832/pytorch_model.bin\n",
"Generate config GenerationConfig {\n",
" \"_from_model_config\": true,\n",
" \"decoder_start_token_id\": 0,\n",
" \"eos_token_id\": 1,\n",
" \"pad_token_id\": 0,\n",
" \"transformers_version\": \"4.29.2\"\n",
"}\n",
"\n",
"All model checkpoint weights were used when initializing T5ForConditionalGeneration.\n",
"\n",
"All the weights of T5ForConditionalGeneration were initialized from the model checkpoint at Vamsi/T5_Paraphrase_Paws.\n",
"If your task is similar to the task the model of the checkpoint was trained on, you can already use T5ForConditionalGeneration for predictions without further training.\n",
"Generation config file not found, using a generation config created from the model config.\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"sentence = \"One of the best ways to learn is to teach what you've already learned\"\n",
"get_paraphrased_sentences(paws_model, paws_tokenizer, sentence)"
],
"metadata": {
"execution": {
"iopub.status.busy": "2022-02-28T17:56:07.560328Z",
"iopub.execute_input": "2022-02-28T17:56:07.560679Z",
"iopub.status.idle": "2022-02-28T17:56:09.301928Z",
"shell.execute_reply.started": "2022-02-28T17:56:07.56065Z",
"shell.execute_reply": "2022-02-28T17:56:09.300685Z"
},
"trusted": true,
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "sBrdOUbicilq",
"outputId": "db9f2240-a31c-439d-d55e-d148ae40f4f5"
},
"execution_count": 31,
"outputs": [
{
"output_type": "stream",
"name": "stderr",
"text": [
"Generate config GenerationConfig {\n",
" \"_from_model_config\": true,\n",
" \"decoder_start_token_id\": 0,\n",
" \"eos_token_id\": 1,\n",
" \"pad_token_id\": 0,\n",
" \"transformers_version\": \"4.29.2\"\n",
"}\n",
"\n"
]
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"[\"One of the best ways to learn is to teach what you've already learned.\",\n",
" 'One of the best ways to learn is to teach what you have already learned.',\n",
" 'One of the best ways to learn is to teach what you already know.',\n",
" 'One of the best ways to learn is to teach what you already learned.',\n",
" \"One of the best ways to learn is to teach what you've already learned.\"]"
]
},
"metadata": {},
"execution_count": 31
}
]
},
{
"cell_type": "markdown",
"source": [
"# Using Bart Paraphraser"
],
"metadata": {
"id": "wbUALmxZe1Zd"
}
},
{
"cell_type": "code",
"source": [
"model_bart = BartForConditionalGeneration.from_pretrained('eugenesiow/bart-paraphrase')\n",
"tokenizer_bart = BartTokenizer.from_pretrained('eugenesiow/bart-paraphrase')"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "tFct6Nuge4DU",
"outputId": "41cdd3d3-c36c-46f3-d232-0b63092ae2d2"
},
"execution_count": 32,
"outputs": [
{
"output_type": "stream",
"name": "stderr",
"text": [
"loading configuration file config.json from cache at /root/.cache/huggingface/hub/models--eugenesiow--bart-paraphrase/snapshots/e0d09a8456792dec7ce5b2129798c32572842213/config.json\n",
"Model config BartConfig {\n",
" \"_name_or_path\": \"facebook/bart-large\",\n",
" \"activation_dropout\": 0.1,\n",
" \"activation_function\": \"gelu\",\n",
" \"add_bias_logits\": false,\n",
" \"add_final_layer_norm\": false,\n",
" \"architectures\": [\n",
" \"BartForConditionalGeneration\"\n",
" ],\n",
" \"attention_dropout\": 0.1,\n",
" \"bos_token_id\": 0,\n",
" \"classif_dropout\": 0.1,\n",
" \"classifier_dropout\": 0.0,\n",
" \"d_model\": 1024,\n",
" \"decoder_attention_heads\": 16,\n",
" \"decoder_ffn_dim\": 4096,\n",
" \"decoder_layerdrop\": 0.0,\n",
" \"decoder_layers\": 12,\n",
" \"decoder_start_token_id\": 2,\n",
" \"dropout\": 0.1,\n",
" \"early_stopping\": true,\n",
" \"encoder_attention_heads\": 16,\n",
" \"encoder_ffn_dim\": 4096,\n",
" \"encoder_layerdrop\": 0.0,\n",
" \"encoder_layers\": 12,\n",
" \"eos_token_id\": 2,\n",
" \"forced_eos_token_id\": 2,\n",
" \"gradient_checkpointing\": false,\n",
" \"id2label\": {\n",
" \"0\": \"LABEL_0\",\n",
" \"1\": \"LABEL_1\",\n",
" \"2\": \"LABEL_2\"\n",
" },\n",
" \"init_std\": 0.02,\n",
" \"is_encoder_decoder\": true,\n",
" \"label2id\": {\n",
" \"LABEL_0\": 0,\n",
" \"LABEL_1\": 1,\n",
" \"LABEL_2\": 2\n",
" },\n",
" \"max_position_embeddings\": 1024,\n",
" \"model_type\": \"bart\",\n",
" \"no_repeat_ngram_size\": 3,\n",
" \"normalize_before\": false,\n",
" \"num_beams\": 4,\n",
" \"num_hidden_layers\": 12,\n",
" \"pad_token_id\": 1,\n",
" \"scale_embedding\": false,\n",
" \"task_specific_params\": {\n",
" \"summarization\": {\n",
" \"length_penalty\": 1.0,\n",
" \"max_length\": 128,\n",
" \"min_length\": 12,\n",
" \"num_beams\": 4\n",
" },\n",
" \"summarization_cnn\": {\n",
" \"length_penalty\": 2.0,\n",
" \"max_length\": 142,\n",
" \"min_length\": 56,\n",
" \"num_beams\": 4\n",
" },\n",
" \"summarization_xsum\": {\n",
" \"length_penalty\": 1.0,\n",
" \"max_length\": 62,\n",
" \"min_length\": 11,\n",
" \"num_beams\": 6\n",
" }\n",
" },\n",
" \"torch_dtype\": \"float32\",\n",
" \"transformers_version\": \"4.29.2\",\n",
" \"use_cache\": true,\n",
" \"vocab_size\": 50265\n",
"}\n",
"\n",
"loading weights file pytorch_model.bin from cache at /root/.cache/huggingface/hub/models--eugenesiow--bart-paraphrase/snapshots/e0d09a8456792dec7ce5b2129798c32572842213/pytorch_model.bin\n",
"Generate config GenerationConfig {\n",
" \"_from_model_config\": true,\n",
" \"bos_token_id\": 0,\n",
" \"decoder_start_token_id\": 2,\n",
" \"early_stopping\": true,\n",
" \"eos_token_id\": 2,\n",
" \"forced_eos_token_id\": 2,\n",
" \"no_repeat_ngram_size\": 3,\n",
" \"num_beams\": 4,\n",
" \"pad_token_id\": 1,\n",
" \"transformers_version\": \"4.29.2\"\n",
"}\n",
"\n",
"All model checkpoint weights were used when initializing BartForConditionalGeneration.\n",
"\n",
"All the weights of BartForConditionalGeneration were initialized from the model checkpoint at eugenesiow/bart-paraphrase.\n",
"If your task is similar to the task the model of the checkpoint was trained on, you can already use BartForConditionalGeneration for predictions without further training.\n",
"Generation config file not found, using a generation config created from the model config.\n",
"loading file vocab.json from cache at /root/.cache/huggingface/hub/models--eugenesiow--bart-paraphrase/snapshots/e0d09a8456792dec7ce5b2129798c32572842213/vocab.json\n",
"loading file merges.txt from cache at /root/.cache/huggingface/hub/models--eugenesiow--bart-paraphrase/snapshots/e0d09a8456792dec7ce5b2129798c32572842213/merges.txt\n",
"loading file added_tokens.json from cache at None\n",
"loading file special_tokens_map.json from cache at /root/.cache/huggingface/hub/models--eugenesiow--bart-paraphrase/snapshots/e0d09a8456792dec7ce5b2129798c32572842213/special_tokens_map.json\n",
"loading file tokenizer_config.json from cache at /root/.cache/huggingface/hub/models--eugenesiow--bart-paraphrase/snapshots/e0d09a8456792dec7ce5b2129798c32572842213/tokenizer_config.json\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"sentence = \"One of the best ways to learn is to teach what you've already learned\"\n",
"get_paraphrased_sentences(model_bart, tokenizer_bart, sentence)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "u5VJg2e_fNUk",
"outputId": "6edd1632-b3fe-4655-b505-ef379a4d0b73"
},
"execution_count": 33,
"outputs": [
{
"output_type": "stream",
"name": "stderr",
"text": [
"Generate config GenerationConfig {\n",
" \"_from_model_config\": true,\n",
" \"bos_token_id\": 0,\n",
" \"decoder_start_token_id\": 2,\n",
" \"early_stopping\": true,\n",
" \"eos_token_id\": 2,\n",
" \"forced_eos_token_id\": 2,\n",
" \"no_repeat_ngram_size\": 3,\n",
" \"num_beams\": 4,\n",
" \"pad_token_id\": 1,\n",
" \"transformers_version\": \"4.29.2\"\n",
"}\n",
"\n"
]
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"['What is the best way to learn?',\n",
" \"How can I teach myself what I've already learned?\",\n",
" \"One of the best ways to learn is to teach what you've already learned\",\n",
" \"One of the best ways to learn is teach what you've already learned.\",\n",
" \"How can I teach what I've already learned?\"]"
]
},
"metadata": {},
"execution_count": 33
}
]
},
{
"cell_type": "code",
"source": [
"get_paraphrased_passage(input_text, model_bart, tokenizer_bart)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 1000
},
"id": "ybePdBB_gmwF",
"outputId": "29be741a-39f2-4748-b4ba-5a8b9d3cfe24"
},
"execution_count": 34,
"outputs": [
{
"output_type": "stream",
"name": "stderr",
"text": [
"Generate config GenerationConfig {\n",
" \"_from_model_config\": true,\n",
" \"bos_token_id\": 0,\n",
" \"decoder_start_token_id\": 2,\n",
" \"early_stopping\": true,\n",
" \"eos_token_id\": 2,\n",
" \"forced_eos_token_id\": 2,\n",
" \"no_repeat_ngram_size\": 3,\n",
" \"num_beams\": 4,\n",
" \"pad_token_id\": 1,\n",
" \"transformers_version\": \"4.29.2\"\n",
"}\n",
"\n",
"Generate config GenerationConfig {\n",
" \"_from_model_config\": true,\n",
" \"bos_token_id\": 0,\n",
" \"decoder_start_token_id\": 2,\n",
" \"early_stopping\": true,\n",
" \"eos_token_id\": 2,\n",
" \"forced_eos_token_id\": 2,\n",
" \"no_repeat_ngram_size\": 3,\n",
" \"num_beams\": 4,\n",
" \"pad_token_id\": 1,\n",
" \"transformers_version\": \"4.29.2\"\n",
"}\n",
"\n",
"Generate config GenerationConfig {\n",
" \"_from_model_config\": true,\n",
" \"bos_token_id\": 0,\n",
" \"decoder_start_token_id\": 2,\n",
" \"early_stopping\": true,\n",
" \"eos_token_id\": 2,\n",
" \"forced_eos_token_id\": 2,\n",
" \"no_repeat_ngram_size\": 3,\n",
" \"num_beams\": 4,\n",
" \"pad_token_id\": 1,\n",
" \"transformers_version\": \"4.29.2\"\n",
"}\n",
"\n",
"Generate config GenerationConfig {\n",
" \"_from_model_config\": true,\n",
" \"bos_token_id\": 0,\n",
" \"decoder_start_token_id\": 2,\n",
" \"early_stopping\": true,\n",
" \"eos_token_id\": 2,\n",
" \"forced_eos_token_id\": 2,\n",
" \"no_repeat_ngram_size\": 3,\n",
" \"num_beams\": 4,\n",
" \"pad_token_id\": 1,\n",
" \"transformers_version\": \"4.29.2\"\n",
"}\n",
"\n",
"Generate config GenerationConfig {\n",
" \"_from_model_config\": true,\n",
" \"bos_token_id\": 0,\n",
" \"decoder_start_token_id\": 2,\n",
" \"early_stopping\": true,\n",
" \"eos_token_id\": 2,\n",
" \"forced_eos_token_id\": 2,\n",
" \"no_repeat_ngram_size\": 3,\n",
" \"num_beams\": 4,\n",
" \"pad_token_id\": 1,\n",
" \"transformers_version\": \"4.29.2\"\n",
"}\n",
"\n",
"Generate config GenerationConfig {\n",
" \"_from_model_config\": true,\n",
" \"bos_token_id\": 0,\n",
" \"decoder_start_token_id\": 2,\n",
" \"early_stopping\": true,\n",
" \"eos_token_id\": 2,\n",
" \"forced_eos_token_id\": 2,\n",
" \"no_repeat_ngram_size\": 3,\n",
" \"num_beams\": 4,\n",
" \"pad_token_id\": 1,\n",
" \"transformers_version\": \"4.29.2\"\n",
"}\n",
"\n",
"Generate config GenerationConfig {\n",
" \"_from_model_config\": true,\n",
" \"bos_token_id\": 0,\n",
" \"decoder_start_token_id\": 2,\n",
" \"early_stopping\": true,\n",
" \"eos_token_id\": 2,\n",
" \"forced_eos_token_id\": 2,\n",
" \"no_repeat_ngram_size\": 3,\n",
" \"num_beams\": 4,\n",
" \"pad_token_id\": 1,\n",
" \"transformers_version\": \"4.29.2\"\n",
"}\n",
"\n",
"Generate config GenerationConfig {\n",
" \"_from_model_config\": true,\n",
" \"bos_token_id\": 0,\n",
" \"decoder_start_token_id\": 2,\n",
" \"early_stopping\": true,\n",
" \"eos_token_id\": 2,\n",
" \"forced_eos_token_id\": 2,\n",
" \"no_repeat_ngram_size\": 3,\n",
" \"num_beams\": 4,\n",
" \"pad_token_id\": 1,\n",
" \"transformers_version\": \"4.29.2\"\n",
"}\n",
"\n",
"Generate config GenerationConfig {\n",
" \"_from_model_config\": true,\n",
" \"bos_token_id\": 0,\n",
" \"decoder_start_token_id\": 2,\n",
" \"early_stopping\": true,\n",
" \"eos_token_id\": 2,\n",
" \"forced_eos_token_id\": 2,\n",
" \"no_repeat_ngram_size\": 3,\n",
" \"num_beams\": 4,\n",
" \"pad_token_id\": 1,\n",
" \"transformers_version\": \"4.29.2\"\n",
"}\n",
"\n",
"Generate config GenerationConfig {\n",
" \"_from_model_config\": true,\n",
" \"bos_token_id\": 0,\n",
" \"decoder_start_token_id\": 2,\n",
" \"early_stopping\": true,\n",
" \"eos_token_id\": 2,\n",
" \"forced_eos_token_id\": 2,\n",
" \"no_repeat_ngram_size\": 3,\n",
" \"num_beams\": 4,\n",
" \"pad_token_id\": 1,\n",
" \"transformers_version\": \"4.29.2\"\n",
"}\n",
"\n",
"Generate config GenerationConfig {\n",
" \"_from_model_config\": true,\n",
" \"bos_token_id\": 0,\n",
" \"decoder_start_token_id\": 2,\n",
" \"early_stopping\": true,\n",
" \"eos_token_id\": 2,\n",
" \"forced_eos_token_id\": 2,\n",
" \"no_repeat_ngram_size\": 3,\n",
" \"num_beams\": 4,\n",
" \"pad_token_id\": 1,\n",
" \"transformers_version\": \"4.29.2\"\n",
"}\n",
"\n",
"Generate config GenerationConfig {\n",
" \"_from_model_config\": true,\n",
" \"bos_token_id\": 0,\n",
" \"decoder_start_token_id\": 2,\n",
" \"early_stopping\": true,\n",
" \"eos_token_id\": 2,\n",
" \"forced_eos_token_id\": 2,\n",
" \"no_repeat_ngram_size\": 3,\n",
" \"num_beams\": 4,\n",
" \"pad_token_id\": 1,\n",
" \"transformers_version\": \"4.29.2\"\n",
"}\n",
"\n",
"Generate config GenerationConfig {\n",
" \"_from_model_config\": true,\n",
" \"bos_token_id\": 0,\n",
" \"decoder_start_token_id\": 2,\n",
" \"early_stopping\": true,\n",
" \"eos_token_id\": 2,\n",
" \"forced_eos_token_id\": 2,\n",
" \"no_repeat_ngram_size\": 3,\n",
" \"num_beams\": 4,\n",
" \"pad_token_id\": 1,\n",
" \"transformers_version\": \"4.29.2\"\n",
"}\n",
"\n"
]
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"\"What started as an oxford speech on whether Britain owns reparations to its former colonies hasThe book refutes arguments of The Raj apologists that despite its despotic nature, itThe British claim that they did this under the white man's burden to give back, soThe British never cared about the interests of the Indian people.They resorted to tactical policies of divide & rule, discriminatory recruitment, xenophobic enforcement of lawsWhether it was trade and agriculture policies, infrastructure development including railways and irrigation, criminal justice systemUnlike the previous despots in the country, such as the Mughals or the DelhiThis alienation was also reflected in their policies, which never benefited the Indians.While cricket and English language may be cherished by many Indians, they too were a byproductSo were the other legacies, such as railways infrastructure or colonial laws.Finally, it is not important to arrive at the amount of reparation which the British oweMore important is the atonement and sincere apology that British owe to Indians and other former coloniesWhat does it take to be a good person?\""
],
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "string"
}
},
"metadata": {},
"execution_count": 34
}
]
},
{
"cell_type": "code",
"source": [],
"metadata": {
"id": "Pr99f-_6mZDq"
},
"execution_count": 34,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment