{ "cells": [ { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "import os; os.chdir('..')" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "from keys import get_similarity_against" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "main_query= \"water intoxication\"\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "sentence1= '''Water intoxication, also known as water poisoning, hyperhydration, overhydration, or water toxemia, is a potentially fatal disturbance in brain functions that results when the normal balance of electrolytes in the body is pushed outside safe limits by excessive water intake.\n", "\n", "Under normal circumstances, accidentally consuming too much water is exceptionally rare. Nearly all deaths related to water intoxication in normal individuals have resulted either from water-drinking contests, in which individuals attempt to consume large amounts of water, or from long bouts of exercise during which excessive amounts of fluid were consumed.[1] In addition, water cure, a method of torture in which the victim is forced to consume excessive amounts of water, can cause water intoxication.[1]\n", "\n", "Water, like any other substance, can be considered a poison when over-consumed in a brief period of time. Water intoxication mostly occurs when water is being consumed in a high quantity without adequate electrolyte intake.[2]\n", "\n", "Excess of body water may also be a result of a medical condition or improper treatment; see \"hyponatremia\" for some examples. Water is considered one of the least toxic chemical compounds, with an LD50 exceeding 90 ml/kg in rats;[3] drinking six liters in three hours has caused the death of a human.[4]'''\n", "\n", "\n", "sentence2= '''Hyponatremia, colloquially termed aqua inebriation, or aqueous toxemia, represents a perilous derangement of cerebral functions. It ensues when the delicate equilibrium of bodily electrolytes is jolted beyond secure thresholds by an extravagant indulgence in aqueous libations.\n", "\n", "In ordinary circumstances, inadvertent indulgence in excessive aqueous elixir is exceedingly exceptional. Virtually all instances of aqueous inebriation-related demises in average individuals have stemmed either from aquatic imbiber duels, wherein contenders vie to imbibe copious volumes of water, or from prolonged stints of exertion accompanied by the immoderate ingestion of fluids. Additionally, aqua torment, a tormenting methodology in which the sufferer is coerced into partaking of profuse quantities of water, can precipitate aqueous inebriation.\n", "\n", "H2O, akin to any other substance, may be deemed venomous when extravagantly consumed within a concise temporal interval. Aqua intoxication predominantly manifests itself when an exorbitant quantum of water is ingested without commensurate electrolytic supplementation.\n", "\n", "A superabundance of corporeal aqueous content might also emerge as an outcome of a medical ailment or inept therapy; for illustrative instances, refer to \"hyponatremia.\" Water is reckoned as one of the least virulent chemical compounds, boasting an LD50 that surpasses 90 ml/kg in rodents. Consuming six liters in a mere three hours has led to the demise of a human.'''" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Water intoxication, also known as water poisoning, hyperhydration, overhydration, or water toxemia, is a potentially fatal disturbance in brain functions that results when the normal balance of electrolytes in the body is pushed outside safe limits by excessive water intake.\\n\\nUnder normal circumstances, accidentally consuming too much water is exceptionally rare. Nearly all deaths related to water intoxication in normal individuals have resulted either from water-drinking contests, in which individuals attempt to consume large amounts of water, or from long bouts of exercise during which excessive amounts of fluid were consumed.[1] In addition, water cure, a method of torture in which the victim is forced to consume excessive amounts of water, can cause water intoxication.[1]\\n\\nWater, like any other substance, can be considered a poison when over-consumed in a brief period of time. Water intoxication mostly occurs when water is being consumed in a high quantity without adequate electrolyte intake.[2]\\n\\nExcess of body water may also be a result of a medical condition or improper treatment; see \"hyponatremia\" for some examples. Water is considered one of the least toxic chemical compounds, with an LD50 exceeding 90 ml/kg in rats;[3] drinking six liters in three hours has caused the death of a human.[4]'" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sentence1" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Hyponatremia, colloquially termed aqua inebriation, or aqueous toxemia, represents a perilous derangement of cerebral functions. It ensues when the delicate equilibrium of bodily electrolytes is jolted beyond secure thresholds by an extravagant indulgence in aqueous libations.\\n\\nIn ordinary circumstances, inadvertent indulgence in excessive aqueous elixir is exceedingly exceptional. Virtually all instances of aqueous inebriation-related demises in average individuals have stemmed either from aquatic imbiber duels, wherein contenders vie to imbibe copious volumes of water, or from prolonged stints of exertion accompanied by the immoderate ingestion of fluids. Additionally, aqua torment, a tormenting methodology in which the sufferer is coerced into partaking of profuse quantities of water, can precipitate aqueous inebriation.\\n\\nH2O, akin to any other substance, may be deemed venomous when extravagantly consumed within a concise temporal interval. Aqua intoxication predominantly manifests itself when an exorbitant quantum of water is ingested without commensurate electrolytic supplementation.\\n\\nA superabundance of corporeal aqueous content might also emerge as an outcome of a medical ailment or inept therapy; for illustrative instances, refer to \"hyponatremia.\" Water is reckoned as one of the least virulent chemical compounds, boasting an LD50 that surpasses 90 ml/kg in rodents. Consuming six liters in a mere three hours has led to the demise of a human.'" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sentence2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Method 1: `Direct Similarity of Embeddings`" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "import requests\n", "import json" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "response= requests.post(url= \"https://embeddings.paperbot.ai/get-similarity-against\",\n", " json={\n", " \"main_entity\": main_query, \n", " \"compare_with\": [sentence1, sentence2]\n", " })" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0.94, 0.9]" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "json.loads(response.content.decode(\"utf-8\"))['similarity']\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Method 1: `BERT Question-Answering`\n" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "from transformers import AutoModelForQuestionAnswering, AutoTokenizer, pipeline\n", "\n", "model_name = \"deepset/roberta-base-squad2\"" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/home/ubuntu/SentenceStructureComparision/venv/lib/python3.10/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n", " from .autonotebook import tqdm as notebook_tqdm\n" ] } ], "source": [ "\n", "\n", "# a) Get predictions\n", "nlp = pipeline('question-answering', model=model_name, tokenizer=model_name)\n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'score': 0.001219886471517384,\n", " 'start': 252,\n", " 'end': 274,\n", " 'answer': 'excessive water intake'}" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "QA_input = {\n", " 'question': main_query,\n", " 'context': sentence1\n", "}\n", "res = nlp(QA_input)\n", "\n", "res" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'score': 2.8929189284099266e-05,\n", " 'start': 958,\n", " 'end': 975,\n", " 'answer': 'Aqua intoxication'}" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "QA_input = {\n", " 'question': main_query,\n", " 'context': sentence2\n", "}\n", "res = nlp(QA_input)\n", "\n", "res" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Perplexity" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Some weights of RobertaForMaskedLM were not initialized from the model checkpoint at deepset/roberta-base-squad2 and are newly initialized: ['lm_head.dense.weight', 'lm_head.layer_norm.bias', 'lm_head.dense.bias', 'lm_head.layer_norm.weight', 'lm_head.decoder.bias', 'lm_head.bias']\n", "You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference.\n" ] } ], "source": [ "import torch\n", "from transformers import AutoTokenizer, AutoModelForMaskedLM\n", "tokenizer = AutoTokenizer.from_pretrained(model_name)\n", "model = AutoModelForMaskedLM.from_pretrained(model_name).to(\"cuda\")\n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "RobertaConfig {\n", " \"_name_or_path\": \"deepset/roberta-base-squad2\",\n", " \"architectures\": [\n", " \"RobertaForQuestionAnswering\"\n", " ],\n", " \"attention_probs_dropout_prob\": 0.1,\n", " \"bos_token_id\": 0,\n", " \"classifier_dropout\": null,\n", " \"eos_token_id\": 2,\n", " \"gradient_checkpointing\": false,\n", " \"hidden_act\": \"gelu\",\n", " \"hidden_dropout_prob\": 0.1,\n", " \"hidden_size\": 768,\n", " \"initializer_range\": 0.02,\n", " \"intermediate_size\": 3072,\n", " \"language\": \"english\",\n", " \"layer_norm_eps\": 1e-05,\n", " \"max_position_embeddings\": 514,\n", " \"model_type\": \"roberta\",\n", " \"name\": \"Roberta\",\n", " \"num_attention_heads\": 12,\n", " \"num_hidden_layers\": 12,\n", " \"pad_token_id\": 1,\n", " \"position_embedding_type\": \"absolute\",\n", " \"transformers_version\": \"4.34.0\",\n", " \"type_vocab_size\": 1,\n", " \"use_cache\": true,\n", " \"vocab_size\": 50265\n", "}" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "model.config" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "def calculate_perplexity(sentence):\n", " inputs = tokenizer(sentence, return_tensors='pt').to(\"cuda\")\n", " with torch.no_grad():\n", " outputs = model(**inputs, labels=inputs['input_ids'])\n", " print(outputs)\n", " loss = outputs.loss # cross entropy loss ----> assuming our model gives us probabilities of different words\n", " perplexity = torch.exp(loss)\n", " return perplexity.item()" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "MaskedLMOutput(loss=tensor(17.8039, device='cuda:0'), logits=tensor([[[ 0.3795, 0.3104, -0.5476, ..., 0.6206, 1.2048, 0.2867],\n", " [ 6.1099, 0.2996, -3.4115, ..., 0.1186, -2.1274, -0.5033],\n", " [ 3.1611, 0.4855, -1.8712, ..., 1.0915, -1.1092, 0.9038],\n", " ...,\n", " [ 4.7616, 0.5287, -4.6328, ..., 0.6480, -2.6257, 0.2898],\n", " [-1.5800, 0.9459, -4.8465, ..., -1.5705, -3.4567, -1.7680],\n", " [-2.2582, 0.7316, -5.1463, ..., -2.0331, -4.5188, -0.8688]]],\n", " device='cuda:0'), hidden_states=None, attentions=None)\n", "Perplexity of the sentence1: 53969640.0\n", "MaskedLMOutput(loss=tensor(17.1493, device='cuda:0'), logits=tensor([[[ 0.2888, 0.1490, -0.1353, ..., 0.9108, 1.6271, 0.4736],\n", " [ 7.6726, 0.6179, -2.9211, ..., 1.1359, -1.1669, 0.7527],\n", " [ 3.2592, 0.6674, -4.1829, ..., -1.3289, -2.9414, 0.0205],\n", " ...,\n", " [ 1.4004, 0.5725, -3.3266, ..., -0.8717, -3.7206, -0.4705],\n", " [-1.4701, 0.8628, -5.1591, ..., -1.6357, -3.4620, -1.6146],\n", " [-2.7571, 0.6047, -5.1016, ..., -1.9740, -4.2890, -1.0212]]],\n", " device='cuda:0'), hidden_states=None, attentions=None)\n", "Perplexity of the sentence2: 28044616.0\n" ] } ], "source": [ "print(f'Perplexity of the sentence1: {calculate_perplexity(sentence1)}')\n", "print(f'Perplexity of the sentence2: {calculate_perplexity(sentence2)}')" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'input_ids': tensor([[ 0, 25589, 34205, 6, 67, 684, 25, 514, 15000, 6,\n", " 8944, 30420, 8475, 6, 81, 30420, 8475, 6, 50, 514,\n", " 46334, 23249, 6, 16, 10, 2905, 6484, 20771, 11, 2900,\n", " 8047, 14, 775, 77, 5, 2340, 2394, 9, 39875, 12782,\n", " 11, 5, 809, 16, 3148, 751, 1522, 4971, 30, 10079,\n", " 514, 14797, 4, 50118, 50118, 17245, 2340, 4215, 6, 13636,\n", " 16997, 350, 203, 514, 16, 20135, 3159, 4, 9221, 70,\n", " 3257, 1330, 7, 514, 34205, 11, 2340, 2172, 33, 4596,\n", " 1169, 31, 514, 12, 10232, 18957, 11997, 6, 11, 61,\n", " 2172, 2120, 7, 14623, 739, 5353, 9, 514, 6, 50,\n", " 31, 251, 24750, 9, 3325, 148, 61, 10079, 5353, 9,\n", " 12293, 58, 13056, 31274, 134, 742, 96, 1285, 6, 514,\n", " 13306, 6, 10, 5448, 9, 11809, 11, 61, 5, 1802,\n", " 16, 1654, 7, 14623, 10079, 5353, 9, 514, 6, 64,\n", " 1303, 514, 34205, 31274, 134, 742, 50118, 50118, 25589, 6,\n", " 101, 143, 97, 6572, 6, 64, 28, 1687, 10, 17712,\n", " 77, 81, 12, 10998, 28817, 11, 10, 4315, 675, 9,\n", " 86, 4, 3201, 34205, 2260, 11493, 77, 514, 16, 145,\n", " 13056, 11, 10, 239, 16363, 396, 9077, 39875, 859, 14797,\n", " 31274, 176, 742, 50118, 50118, 9089, 19348, 9, 809, 514,\n", " 189, 67, 28, 10, 898, 9, 10, 1131, 1881, 50,\n", " 18418, 1416, 131, 192, 22, 33027, 261, 415, 5593, 493,\n", " 113, 13, 103, 7721, 4, 3201, 16, 1687, 65, 9,\n", " 5, 513, 8422, 4747, 18291, 6, 19, 41, 34744, 1096,\n", " 17976, 1814, 36769, 73, 9043, 11, 24162, 131, 10975, 246,\n", " 742, 4835, 411, 6474, 268, 11, 130, 722, 34, 1726,\n", " 5, 744, 9, 10, 1050, 31274, 306, 742, 2]]), 'attention_mask': tensor([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n", " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n", " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n", " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n", " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n", " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n", " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n", " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n", " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n", " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n", " 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n", " 1, 1, 1, 1, 1]])}" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "encodings_sentence1= tokenizer(sentence1, return_tensors=\"pt\")\n", "encodings_sentence1" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "seq_len = 269\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ " 0%| | 0/269 [00:0010:\n", "# x.append(i)\n", " \n", "list_of_sentences1" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['Political stability is the ability of a government to maintain order and authority within its borders',\n", " ' It is essential for economic growth, as it provides a foundation for investment and trade',\n", " '\\nThere are many factors that contribute to political stability, including:\\nA strong rule of law: The rule of law is the principle that everyone is subject to the same laws, regardless of their social status or political affiliation',\n", " ' A strong rule of law helps to prevent corruption and ensures that everyone has equal opportunity to succeed',\n", " '\\nA well-functioning government: A well-functioning government is one that is able to provide essential services, such as security, education, and healthcare',\n", " ' It is also able to manage the economy effectively and to respond to crises',\n", " '\\nA vibrant civil society: A vibrant civil society is one that is made up of active and engaged citizens',\n", " ' Civil society organizations can help to hold the government accountable and to promote democracy and good governance',\n", " '\\nPolitical stability is not always easy to achieve, but it is essential for economic growth',\n", " ' By investing in political stability, we can create a foundation for long-term prosperity',\n", " '\\nHere are some of the benefits of political stability:\\nIncreased investment: Investors are more likely to invest in countries that are politically stable',\n", " ' This can lead to increased economic growth and job creation',\n", " '\\nImproved trade: Trade between countries is easier and more efficient when there is political stability',\n", " ' This can lead to lower prices for consumers and increased profits for businesses',\n", " '\\nReduced poverty: Political stability can help to reduce poverty by creating a more conducive environment for economic growth',\n", " '\\nImproved quality of life: Political stability can lead to improved quality of life by providing a safer and more secure environment',\n", " '\\nPolitical stability is a key ingredient for a prosperous and successful society',\n", " ' By investing in political stability, we can create a better future for ourselves and our children',\n", " '\\nHere are some of the challenges to political stability:\\nEconomic inequality: Economic inequality can lead to social unrest and instability',\n", " ' This is because it can create a sense of injustice and resentment among those who are not benefiting from economic growth',\n", " '\\nCorruption: Corruption can undermine the rule of law and erode public trust in government',\n", " ' This can lead to instability and violence',\n", " '\\nEthnic and religious conflict: Ethnic and religious conflict can be a major source of instability',\n", " ' This is because it can lead to violence, displacement, and economic disruption',\n", " '\\nNatural disasters: Natural disasters can also be a source of instability',\n", " ' This is because they can displace people, damage infrastructure, and disrupt economic activity',\n", " '\\nDespite the challenges, there are many things that can be done to promote political stability',\n", " ' These include:\\nInvesting in education and healthcare: Education and healthcare can help to reduce poverty and inequality, which are two of the main causes of instability',\n", " '\\nPromoting good governance: Good governance is essential for building trust between the government and the people',\n", " ' It can be promoted by strengthening the rule of law, fighting corruption, and ensuring transparency and accountability',\n", " '\\nResolving conflict peacefully: Conflict can be resolved peacefully through negotiation, mediation, and other means',\n", " ' This can help to prevent violence and instability',\n", " '\\nBuilding resilience: Building resilience is essential for coping with shocks and stresses, such as economic downturns and natural disasters',\n", " ' It can be done by investing in infrastructure, social safety nets, and disaster preparedness',\n", " '\\nPolitical stability is a complex issue, but it is essential for economic growth and prosperity',\n", " ' By investing in political stability, we can create a better future for ourselves and our children',\n", " '']" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# list_of_sentences2= sentence2.split('.')\n", "# s2= s2.replace('\\n', ' ')\n", "\n", "list_of_sentences2= s2.split('.')\n", "list_of_sentences2" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "def calculate_burst(list_of_sentences):\n", " arr= []\n", " for i in list_of_sentences:\n", " ei= tokenizer(i, return_tensors=\"pt\")\n", " arr.append(ei.input_ids.size(1))\n", " print(f\"arr= {(arr)}\")\n", " return np.var(np.array(arr))\n", " " ] }, { "cell_type": "code", "execution_count": 78, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "arr= [41, 27, 24, 37, 24, 28, 42, 35, 24, 11, 23, 21, 29, 40]\n" ] }, { "data": { "text/plain": [ "74.14285714285714" ] }, "execution_count": 78, "metadata": {}, "output_type": "execute_result" } ], "source": [ "calculate_burst(list_of_sentences1[:-1])" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "arr= [16, 16, 44, 18, 32, 14, 21, 17, 17, 16, 27, 10, 17, 13, 21, 23, 13, 17, 24, 21, 18, 7, 18, 14, 13, 16, 17, 31, 19, 19, 19, 8, 23, 17, 17, 17]\n" ] }, { "data": { "text/plain": [ "45.57098765432099" ] }, "execution_count": 63, "metadata": {}, "output_type": "execute_result" } ], "source": [ "calculate_burst(list_of_sentences2[:-1])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Method 3: `Summarization via Language Model`\n" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [], "source": [ "# Use a pipeline as a high-level helper\n", "from transformers import pipeline\n", "\n", "pipe = pipeline(\"summarization\", model=\"DunnBC22/flan-t5-base-text_summarization_data\", device=\"cuda\")" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[{'summary_text': 'Understand water intoxication. Recognize water poisoning. Understand the dangers of excessive water intake. Understand how water can be considered a poison. Understand that water is considered one of the least toxic chemical compounds, with an LD50 exceeding 90 ml/kg in rats.'},\n", " {'summary_text': 'Understand hyponatremia. Recognize the dangers of aqueous intoxication. Understand the causes of aqua inebriation. Identify the cause of venomous aquious content. Describe the effects of water on human health.'}]" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pipe([sentence1, sentence2], min_length=50, max_length=200)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "# # Use a pipeline as a high-level helper\n", "# from transformers import pipeline\n", "\n", "# pipe = pipeline(\"summarization\", model=\"google/pegasus-cnn_dailymail\", device=\"cuda\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[{'summary_text': 'Cat ear headphones are popular among otakus, streamers, gamers, and anyone who wants a uniquely cute look .Design is the priority, but sound quality should also be considered .'},\n", " {'summary_text': 'Feline-themed audio headgear enjoys favor among aficionados of anime and gaming, as well as content creators .Seek out headphones delivering crystal-clear and precise audio, and assess their suitability for both mature users and youngsters'}]" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# pipe([sentence1, sentence2], min_length=50, max_length=200)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "venv", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.12" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }