Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save asehmi/28c16f2aa178c4dfb1625bb28b9cde0c to your computer and use it in GitHub Desktop.
Save asehmi/28c16f2aa178c4dfb1625bb28b9cde0c to your computer and use it in GitHub Desktop.
example - t5-v1_1-L-semantic-analyzer
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/pszemraj/5760325192a37a7549cd666e473a1823/example-t5-v1_1-l-semantic-analyzer.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "auYqCTYi53jn"
},
"source": [
"# <center> example - `pszemraj/t5-v1_1-L-semantic-analyzer`\n",
"\n",
"- [model card](https://huggingface.co/pszemraj/t5_1_1-base-writing-analysis)\n",
"- [github](https://github.com/pszemraj)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "lIYdn1woOS1n"
},
"outputs": [],
"source": [
"!pip install -U -q transformers\n",
"!pip install -U -q sentencepiece"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cellView": "form",
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "oxLVW3ehkStT",
"outputId": "6da25d75-b66b-40af-c09a-92ab9d87d4c1"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Fri Feb 4 12:04:49 2022 \n",
"+-----------------------------------------------------------------------------+\n",
"| NVIDIA-SMI 460.32.03 Driver Version: 460.32.03 CUDA Version: 11.2 |\n",
"|-------------------------------+----------------------+----------------------+\n",
"| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |\n",
"| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |\n",
"| | | MIG M. |\n",
"|===============================+======================+======================|\n",
"| 0 Tesla V100-SXM2... Off | 00000000:00:04.0 Off | 0 |\n",
"| N/A 32C P0 22W / 300W | 0MiB / 16160MiB | 0% Default |\n",
"| | | N/A |\n",
"+-------------------------------+----------------------+----------------------+\n",
" \n",
"+-----------------------------------------------------------------------------+\n",
"| Processes: |\n",
"| GPU GI CI PID Type Process name GPU Memory |\n",
"| ID ID Usage |\n",
"|=============================================================================|\n",
"| No running processes found |\n",
"+-----------------------------------------------------------------------------+\n"
]
}
],
"source": [
"#@markdown check GPU allocation\n",
"!nvidia-smi"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cellView": "form",
"id": "7j7A3UDT0g6k"
},
"outputs": [],
"source": [
"hf_tag = \"pszemraj/t5-v1_1-L-semantic-analyzer\" #@param {type:\"string\"}\n",
"from transformers import pipeline\n",
"from transformers import T5ForConditionalGeneration, AutoTokenizer\n",
"import torch\n",
"model = T5ForConditionalGeneration.from_pretrained(hf_tag)\n",
"tokenizer = AutoTokenizer.from_pretrained(hf_tag)\n",
"# overwriting the default max_length of 20 \n",
"tokenizer.model_max_length=1024\n",
"model.config.max_length=1024\n",
"analyzer = pipeline(\"text2text-generation\", \n",
" model=model, tokenizer=tokenizer,\n",
" device=0 if torch.cuda.is_available() else -1,\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "DFUT5ou00trM",
"outputId": "4b75c4f0-17b2-4eeb-8d59-1d150b20efcc"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"('\\n'\n",
" 'Then suddenly he beheld his sister Eowyn as she lay, and he knew her. He '\n",
" 'stood a moment as a man who is pierced in the midst of a cry by an arrow '\n",
" 'through the heart; and then his face went deathly white, and a cold fury '\n",
" 'rose in him, so that all speech failed him for a while. A fey mood took '\n",
" 'him.\\n'\n",
" '\\n'\n",
" '“Begone, foul dwimmerlaik, lord of carrion! Leave the dead in peace!\"\\n'\n",
" '\\n'\n",
" \"A cold voice answered: 'Come not between the Nazgûl and his prey! Or he will \"\n",
" 'not slay thee in thy turn. He will bear thee away to the houses of '\n",
" 'lamentation, beyond all darkness, where thy flesh shall be devoured, and thy '\n",
" 'shrivelled mind be left naked to the Lidless Eye.\"\\n'\n",
" '\\n'\n",
" 'A sword rang as it was drawn. \"Do what you will; but I will hinder it, if I '\n",
" 'may.\"\\n'\n",
" '\\n'\n",
" '\"Hinder me? Thou fool. No living man may hinder me!\"\\n'\n",
" '\\n'\n",
" 'Then Merry heard of all sounds in that hour the strangest. It seemed that '\n",
" 'Dernhelm laughed, and the clear voice was like the ring of steel. \"But no '\n",
" 'living man am I!”\\n')\n"
]
}
],
"source": [
"text = \"\"\"\n",
"Then suddenly he beheld his sister Eowyn as she lay, and he knew her. He stood a moment as a man who is pierced in the midst of a cry by an arrow through the heart; and then his face went deathly white, and a cold fury rose in him, so that all speech failed him for a while. A fey mood took him.\n",
"\n",
"“Begone, foul dwimmerlaik, lord of carrion! Leave the dead in peace!\"\n",
"\n",
"A cold voice answered: 'Come not between the Nazgûl and his prey! Or he will not slay thee in thy turn. He will bear thee away to the houses of lamentation, beyond all darkness, where thy flesh shall be devoured, and thy shrivelled mind be left naked to the Lidless Eye.\"\n",
"\n",
"A sword rang as it was drawn. \"Do what you will; but I will hinder it, if I may.\"\n",
"\n",
"\"Hinder me? Thou fool. No living man may hinder me!\"\n",
"\n",
"Then Merry heard of all sounds in that hour the strangest. It seemed that Dernhelm laughed, and the clear voice was like the ring of steel. \"But no living man am I!”\n",
"\"\"\"\n",
"import pprint as pp\n",
"\n",
"pp.pprint(text)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "bnKLTjyq06pw",
"outputId": "53d13fe4-f62e-4633-846b-903bec1a7686"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 1min 35s, sys: 7.62 s, total: 1min 43s\n",
"Wall time: 1min 44s\n"
]
}
],
"source": [
"%%time\n",
"result = analyzer(\n",
" text,\n",
" min_length=int(len(text) * 0.7),\n",
" max_length=int(len(text) * 1.2),\n",
" no_repeat_ngram_size= 2,\n",
" repetition_penalty= 2.4,\n",
" length_penalty=0.8,\n",
" num_beams=4,\n",
" early_stopping= True,\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "Yx93nQOS1AJf",
"outputId": "03c4a68f-6cfa-4db6-fd37-252d123924a5"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(', in this chapter, the reader is given a glimpse of the horrors that are to '\n",
" 'come. It is as if there were no living man in the world who could not bear '\n",
" \"the sight of his sister Eowyn's corpse. Then he sees her dead body lying on \"\n",
" 'the ground, and it is clear that she has been pierced by an arrow through '\n",
" 'the heart. She is left naked to the Lidless Eye, where she will be eaten by '\n",
" 'the evil eye. This scene is one of those who have killed her; and then they '\n",
" 'shall be left alone in peace. In these words, Merry realizes what happened '\n",
" 'to him: \"Ithou artik, but I am I must die!\" He says, \"No living men may '\n",
" 'hinder me\" . And so far, we do not know anything about you, for I cannot '\n",
" 'speak with any other than my own mind, nor can ever say, or even when I hear '\n",
" 'the sound of steel, which is like the ring of irony. For now, all speech '\n",
" 'fails to speak. But rather than talk, thyretched out of life, let me go into '\n",
" 'darkness. There is nothing more than before me.\" A voice tells us that the '\n",
" 'sword rang as it echoes the cold voice from the blood of carrion, just as '\n",
" 'well as the cry of death. As the voice speaks, it seems to be uttered at the '\n",
" 'end of their conversation. His voice is also resoundingly, because it sounds '\n",
" 'like thunder--like the echoing of fire, such as though it was heard only '\n",
" 'after the killing of Carrion himself. Yet again, however, does it seem to '\n",
" 'take up the language of despair. When the voices begin to rise above the '\n",
" 'noises of battle, its silence becomes almost unintellowed upon them. These '\n",
" 'scenes are very different from each other. They are both silent and empty. '\n",
" 'Their voices are filled with fey moods, while the others are gone, they are '\n",
" 'still alive. Now they stand together in fearful of being shrivelled and '\n",
" 'devoid of human speech. At the same time, Dickens uses the word \"Honord,\" '\n",
" 'and the phrase \"Dwimmer lamentation\" -- \"Thenarrator calls forth the words '\n",
" '\"Oldder me?\" \"Alaiak, too, seemed to suggest that something had passed '\n",
" 'between the two noblemen and ghosts. One might have been spoken in terms of '\n",
" 'love and sorrow, yet another thing would have taken place in order to '\n",
" 'describe the nature of war and terror. Indeed, Merithorne describes the '\n",
" 'power of gods and hellishness. However, some people believed that God did '\n",
" 'not want to understand the true meaning of resurrection. Thus, many readers '\n",
" 'believe that Death is the most important part of our imagination. We wonder '\n",
" 'why? Why should we need not listen to your words? What is going to happen? '\n",
" 'Wherein lies the name of heaven? Or how long agonize the idea of salvation? '\n",
" 'That is it possible? Does it matter whether it be good or evil? Do you fool! '\n",
" 'My friend, my dear friend?')\n"
]
}
],
"source": [
"pp.pprint(result[0]['generated_text'])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "rCjW8MSA1C7D"
},
"outputs": [],
"source": [
"with open('example.txt', 'w') as f:\n",
" f.writelines(result[0]['generated_text'])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "KTq-8eC14TnR"
},
"outputs": [],
"source": [
"tests = [\n",
" \"But Arwen went forth from the House, and the light of her eyes was quenched, and it seemed to her people that she had become cold and grey as nightfall in winter that comes without a star. Then she said farewell to Eldarion, and to her daughters, and to all whom she had loved; and she went out from the city of Minas Tirith and passed away to the land of Lórien, and dwelt there alone under the fading trees until winter came. Galadriel had passed away and Celeborn also was gone, and the land was silent. There at last when the mallorn-leaves were falling, but spring had not yet come, she laid herself to rest upon Cerin Amroth; and there is her green grave, until the world is changed, and all the days of her life are utterly forgotten by men that come after, and elanor and niphredil bloom no more east of the Sea.\",\n",
" \"For myself, I would see the White Tree flower again in the courts of the kings, and the Silver Crown return, and Minas Tirith in peace: Minas Anor again as of old, full of light, high and fair, beautiful as a queen among other queens: not a mistress of many slaves, nay, not even a kind mistress of willing slaves [Galadriel!]. War must be, while we defend our lives against a destroyer who would devour us all; but I do not love the bright sword for its sharpness, nor the arrow for its swiftness, nor the warrior for his glory. I love only that which they defend: the city of the Men of Numenor; and I would have loved her for her memory, her ancientry, her beauty, and her present wisdom. Not feared, save as men may fear the dignity of a man, old and wise.\",\n",
" \"The road must be trod, but it will be very hard. And neither strength nor wisdom will carry us far upon it. This quest may be attempted by the weak with as much hope as the strong. Yet such is oft the course of deeds that move the wheels of the world: small hands do them because they must, while the eyes of the great are elsewhere.\",\n",
" \"It all depends on what you want. You can trust us to stick to you through thick and thin – to the bitter end. And you can trust us to keep any secret of yours – closer than you keep it yourself. But you cannot trust us to let you face trouble alone, and go off without a word. We are your friends, Frodo. Anyway: there it is. We know most of what Gandalf has told you. We know a good deal about the Ring. We are horribly afraid – but we are coming with you; or following you like hounds.\",\n",
" \"Beautiful she is, sir! Lovely! Sometimes like a great tree in flower, sometimes like a white daffadowndilly, small and slender like. Hard as di’monds, soft as moonlight. Warm as sunlight, cold as frost in the stars. Proud and far-off as a snow-mountain, and as merry as any lass I ever saw with daisies in her hair in springtime….\",\n",
" \"It strikes me that folk takes their peril with them into Lorien, and finds it there because they’ve brought it. But perhaps you could call her perilous, because she’s so strong in herself. You, you could dash yourself to pieces on her, like a ship on a rock; or drownd yourself, like a hobbit in a river. But neither rock nor river would be to blame.\"\n",
" \"A time may come soon, when none will return. Then there will be need of valour without renown, for none shall remember the deeds that are done in the last defence of your homes. Yet the deeds will not be less valiant because they are unpraised.\",\n",
" \"Far above the Ephel Duath in the West the night-sky was still dim and pale. There, peering among the cloud-wrack above a dark tor high up in the mountains, Sam saw a white star twinkle for a while. The beauty of it smote his heart, as he looked up out of the forsaken land, and hope returned to him. For like a shaft, clear and cold, the thought pierced him that in the end the Shadow was only a small and passing thing: there was light and high beauty for ever beyond its reach.\",\n",
" \"And we shouldn’t be here at all, if we’d known more about it before we started. But I suppose it’s often that way. The brave things in the old tales and songs, Mr. Frodo: adventures, as I used to call them. I used to think that they were things the wonderful folk of the stories went out and looked for, because they wanted them, because they were exciting and life was a bit dull, a kind of a sport, as you might say. But that’s not the way, as you put it. But I expect they had lots of chances, like us, of turning back, only they didn’t. And if they had, we shouldn’t know, because they’d have been forgotten. We hear about those as just went on – and not all to a good end, mind you; at least not to what folk inside a story and not outside it call a good end. You know, coming home, and finding things all right, though not quite the same – like old Mr. Bilbo. But those aren’t always the best tales to hear, though they may be the best tales to get landed in! I wonder what sort of a tale we’ve fallen into?\",\n",
" \"Beren now, he never thought he was going to get that Silmaril from the Iron Crown in Thangorodrim, and yet he did, and that was a worse place and a blacker danger than ours. But that’s a long tale, of course, and goes on past the happiness and into grief and beyond it – and the Silmaril went on and came to Eärendil. And why, sir, I never thought of that before! We’ve got – you’ve got some of the light of it in that star-glass that the Lady gave you! Why, to think of it, we’re in the same tale still! It’s going on. Don’t the great tales never end?\",\n",
"]"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "77ZI4E_XO9cN"
},
"source": [
"examples from [here](https://roadstainedfeet.wordpress.com/2019/03/24/top-ten-lord-of-the-rings-passages/)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"background_save": true,
"base_uri": "https://localhost:8080/"
},
"id": "qByDwMozKENt",
"outputId": "457e477d-f785-4363-fac3-73768f056b16"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
"\n",
"\n",
" prompt 0 \n",
"('But Arwen went forth from the House, and the light of her eyes was quenched, '\n",
" 'and it seemed to her people that she had become cold and grey as nightfall '\n",
" 'in winter that comes without a star. Then she said farewell to Eldarion, and '\n",
" 'to her daughters, and to all whom she had loved; and she went out from the '\n",
" 'city of Minas Tirith and passed away to the land of Lórien, and dwelt there '\n",
" 'alone under the fading trees until winter came. Galadriel had passed away '\n",
" 'and Celeborn also was gone, and the land was silent. There at last when the '\n",
" 'mallorn-leaves were falling, but spring had not yet come, she laid herself '\n",
" 'to rest upon Cerin Amroth; and there is her green grave, until the world is '\n",
" 'changed, and all the days of her life are utterly forgotten by men that come '\n",
" 'after, and elanor and niphredil bloom no more east of the Sea.')\n",
"--------------------------------------------------------\n",
"response\n",
"\n",
"('to the land of Lórien, where there is a green grave for Arwen and her '\n",
" 'daughters, and all who have loved her. Then she went out from Minas Tirith '\n",
" 'and passed away; and it was winter that came without starlight, which '\n",
" 'brought with it the darkness of winter. It was cold and grey as nightfall in '\n",
" 'winter, when the sun had not yet come. There was no light in her eyes, nor '\n",
" 'any light at all, but only darkness. And now the world has become dark and '\n",
" 'cold, even though they have lost all their loveliness. In this chapter, we '\n",
" 'see that the earth has been changed by the death of Elddrion and Celeborn '\n",
" 'are gone. This is also the last scene of the book, since the first time '\n",
" 'comes without stars: \"Ithou artith\\'s life--and I shall never be seen again\" '\n",
" '. But then the end of his story is an act of mourning over him, so that he '\n",
" 'says, \"Arwine did not know what will happen to me.\" He tells us that '\n",
" 'Galadirony is dead, too, because she has died, after the deaths of her '\n",
" 'children, or those whom she had loved. She said goodbye to them, saying '\n",
" 'farewell to each other. Now she goes forth from the House, leaving behind '\n",
" 'her family, she dies alone on Cerin Amrothredil, just as if you were your '\n",
" 'own father, my sons, do you say, O Lord Elornor and Eliothorne would have '\n",
" 'done nothing more than one day. As soon as well, Alphonion does not seem to '\n",
" 'have ever been told about the fate of these people, like the shadows of A '\n",
" 'Midsummer, let go down into the woods before. When the trees fall, the tree '\n",
" 'falls upon the ground, while the Trees were falling, its leaves fell, it '\n",
" 'seemed to be buried under the fallen trees. These words may be read aloudly, '\n",
" 'until the very same thing happened to take place in the house of '\n",
" 'Middle-Elanor, although the old trees were still alive. They were dying, '\n",
" 'however, could not be forgotten. Their names were unremembered, till the '\n",
" 'days of Winter began to fade away. For some years later, they were left to '\n",
" 'lie beneath the branches of Gableringstones and the red roses took place '\n",
" 'among the white flowers. At the beginning of Thyrin amrobbeth became black '\n",
" 'and gray. Even the blue sky was full of snow. All the birds flee through the '\n",
" 'sea, such as the grass grows brown and dried up. We must remember that our '\n",
" 'hearts were growing pale and dry. Our minds were bare and dull and dim. Yet '\n",
" 'the moon was bright and yellow and soft and silent. Glossary clouds grow '\n",
" 'darker than the silver-leaves were golden and beautiful. With the blood of '\n",
" 'men, gold and flowering hills were turning purple and orange-colored. Thus '\n",
" 'the forest was filled with tears and rusty faces were covered with dust and '\n",
" 'frostishness. Wherever seems to turn against the stone walls of heaven, '\n",
" 'gloomy and cloudless, deep and thick and heavy.')\n",
"--------------------------------------------------------\n",
"\n",
"\n",
"\n",
"\n",
" prompt 1 \n",
"('For myself, I would see the White Tree flower again in the courts of the '\n",
" 'kings, and the Silver Crown return, and Minas Tirith in peace: Minas Anor '\n",
" 'again as of old, full of light, high and fair, beautiful as a queen among '\n",
" 'other queens: not a mistress of many slaves, nay, not even a kind mistress '\n",
" 'of willing slaves [Galadriel!]. War must be, while we defend our lives '\n",
" 'against a destroyer who would devour us all; but I do not love the bright '\n",
" 'sword for its sharpness, nor the arrow for its swiftness, nor the warrior '\n",
" 'for his glory. I love only that which they defend: the city of the Men of '\n",
" 'Numenor; and I would have loved her for her memory, her ancientry, her '\n",
" 'beauty, and her present wisdom. Not feared, save as men may fear the dignity '\n",
" 'of a man, old and wise.')\n",
"--------------------------------------------------------\n",
"response\n",
"\n",
"('is a reference to the White Tree, and the Silver Crown return to Minas '\n",
" 'Tirith in peace. In this chapter, we are told that the white tree will '\n",
" 'flower again in the courts of the Men of Numenor; but I do not love the '\n",
" 'bright sword for its sharpness, nor the arrow for his swiftness. For myself, '\n",
" 'I would have loved the city of Anor again as of old, full of light, high and '\n",
" 'fair, beautiful as an ancient queen among other queens. This is my desire: '\n",
" 'to see her beauty once more than she has seen it now. Thenobility of all the '\n",
" \"men of Middle-Elvesedringhorne's wisdom, like the silver crown, which had \"\n",
" 'been destroyed by the destruction of A Midsummeronor, with its splendorous '\n",
" 'glory. And yet, there is no wonder at the power of those who defend us '\n",
" 'against the destroyer who would devour us. It is clear that these two things '\n",
" 'must be restored to their lives. But even when they defend our lives, they '\n",
" 'fight against him. They cannot help them, but only because they protect '\n",
" 'themselves from the enemy. Their defense is so great that they can never be '\n",
" 'able to live without war. There is also the contrast between the two '\n",
" 'kingdoms: one might seem to be both good and evil. One may say, \"I am not '\n",
" 'frighten me.\" Yet, however, does not want to know what you shall be\" . We '\n",
" 'should remember, then, how many slaves could be, indeed, were some sort of '\n",
" 'kings, or any kind of man, such as the nobles of others. Now, here, too, on '\n",
" 'the other hand, was the most important aspect of human beings,\" says '\n",
" 'Galadriel. He did not hate the warrior for the sake of God, since he was '\n",
" 'merely defending the people of Eyranor. Even though if they were willing to '\n",
" 'sacrifice their own life, rather than fighting for freedom. Indeed, '\n",
" 'Gandaladrolia, the Lord of Fanglord, whose name was unworthy of himself. His '\n",
" 'words were very different from each other. Thus, after all, Othello, '\n",
" 'Alphony, King Elvenge, let me think of him as thou artificently. If I knew '\n",
" 'nothing about him, neither did I know anything about the world. My thoughts '\n",
" 'were made out of course, just as long as I thought of ourselves. To me, '\n",
" 'therefore, while I believed that death would be done to me. When I saw the '\n",
" 'sun rise above the moon, mine eyes rose red, my eyes were dark and cold, '\n",
" 'golden, yellow, blue, green, black, gold, sweet, soft, warm, fragrant, pale, '\n",
" 'gentle, pure, calm, quiet, peaceful, happy, holy, glorious, radiant, '\n",
" 'brilliant, rich, strong, powerful, wise, proud, graceful, well-beauded upon '\n",
" 'the earth. Glossary, lovely, shining over the ground, filled with fire, '\n",
" 'blood, deep, sparkling, glowing, open, wide, broad, generous, cheerful, '\n",
" 'gracious, friendly, humble, brave, handsome, elegant, majestic, wild, '\n",
" 'delicate, tender, serene, joyful, loving, pleasant, charming, dewhelmelish, '\n",
" 'silent, restless, alive, free, young, innocent, intelligent, self-confident, '\n",
" 'tranquility, harmonious, ever-seeming, joyous, gladsome, precious, blessed, '\n",
" 'merciful, godlike, magnificent, exquisite, sacred, wonderful, mysterious, '\n",
" 'splendid, magical, marvellious, refined, passionate, faithful, courageous, '\n",
" 'confident, capable of courage, bold, clever, loyal, courteous, honorable, '\n",
" 'worthy of knowledge. These qualities serve to bring forth the images of '\n",
" 'nature. At the end of Chapter 18, we see the image of Lady Elizabethan '\n",
" 'womanhood, tall, large, luxurious, colorful, vibrant, vivid imagery.')\n",
"--------------------------------------------------------\n",
"\n",
"\n",
"\n",
"\n",
" prompt 2 \n",
"('The road must be trod, but it will be very hard. And neither strength nor '\n",
" 'wisdom will carry us far upon it. This quest may be attempted by the weak '\n",
" 'with as much hope as the strong. Yet such is oft the course of deeds that '\n",
" 'move the wheels of the world: small hands do them because they must, while '\n",
" 'the eyes of the great are elsewhere.')\n",
"--------------------------------------------------------\n",
"response\n",
"\n",
"('is a poem about the road that must be trod by the weak and the strong. In '\n",
" 'this chapter, we are given an insight into the course of deeds that move the '\n",
" 'wheels of the world: small hands do them because they must, while the eyes '\n",
" 'of great minds are elsewhere. And so it will be with us until the end of our '\n",
" 'journey. The road may be very hard, but not impossible. Yet such is the '\n",
" 'nature of human action; there is no hope for the strength or wisdom to carry '\n",
" \"us far upon it. This is one of those who believe in God's own life. It is \"\n",
" 'also true that all men have done nothing more than their own way. They do '\n",
" 'not know what they can do. But they cannot help us on the path, as well as '\n",
" 'they might. There is little chance at doing good things, and even when they '\n",
" 'fail to accomplish anything. For example, these words, \"Ithou art\" . I am '\n",
" 'sure, then, indeed, my hand does not seem to be any better. Indeed, it is '\n",
" 'difficult to try to make it possible. We shall never find out how easy it '\n",
" 'would be to do it.\" Thus, however, the roads are too hard to take us through '\n",
" 'the rough road--the road has already been traveled. As long as it had been, '\n",
" 'though it was only if we were to travel it without much effort. At the same '\n",
" 'time, many people did not want to go beyond the narrowness of mankind. To '\n",
" 'some extent, man could not reach the heights of his mind. He said, '\n",
" 'therefore, he meant to walk along the side of him. His desire to see the '\n",
" 'other hand, which seems to give us courage and courage. However, most of '\n",
" 'them seemed to meddle around the earth. One should say, yet again, let me '\n",
" 'think of myself. Even now, I thought of himself, nor of others. Now I knew '\n",
" 'that')\n",
"--------------------------------------------------------\n",
"\n",
"\n",
"\n",
"\n",
" prompt 3 \n",
"('It all depends on what you want. You can trust us to stick to you through '\n",
" 'thick and thin – to the bitter end. And you can trust us to keep any secret '\n",
" 'of yours – closer than you keep it yourself. But you cannot trust us to let '\n",
" 'you face trouble alone, and go off without a word. We are your friends, '\n",
" 'Frodo. Anyway: there it is. We know most of what Gandalf has told you. We '\n",
" 'know a good deal about the Ring. We are horribly afraid – but we are coming '\n",
" 'with you; or following you like hounds.')\n",
"--------------------------------------------------------\n",
"response\n",
"\n",
"(', Frodo. We are your friends, and we will follow you to the end of the '\n",
" 'journey. But we cannot trust us to keep any secret from you. And we can '\n",
" 'trust them to stay with you through thick and thin tyranny; but we do not '\n",
" 'know anything about the Ring. It is too late for him to tell you what it is. '\n",
" 'He knows everything about it. Then he says, \"There is no doubt in my mind '\n",
" 'that I am going to face trouble alone. There is only one thing left to me: '\n",
" 'there is nothing at all. This is something more than I have ever been told '\n",
" 'me. In other words, Gandalf has said, this is an important part of his '\n",
" 'story. That is another matter how far-aseshadows the fact that they are '\n",
" 'following you like hounds. They are very much more concerned about their own '\n",
" 'safety. Their friendship is so close to us. Even when we go off alone, we '\n",
" 'are coming together. Now we see ourselves as if we would be enough to leave '\n",
" 'behind. If we were on our way, then we must come along with each other. When '\n",
" \"we say, here, even though we don't know exactly what we know. What does it \"\n",
" 'mean? Do you really want to help you? Or do you need some help. Just as long '\n",
" 'as we did before. For example, let us walk away without saying anything '\n",
" 'else. As soon as possible, just as well. You should remember that we shall '\n",
" 'never get out of harm. Yet we believe that you might die. At last, these two '\n",
" 'things seem to be true. These chapters also suggest that evil will happen to '\n",
" 'you again. Here, however, both of course, may be right or wrong. To make '\n",
" 'sure, after all, it seems clearer now. One could be seen as good as bad as '\n",
" 'it was done. A great deal of danger comes into questionable. So many '\n",
" 'questions remain unanswraped by the others. No matter what happens to them. '\n",
" 'All of them lies in the same time. Some critics argue that those wholly '\n",
" 'agree to take care for themselves. Like the Hobbiters, they had little idea '\n",
" 'of what happened. Not only because they were willing to give up their best '\n",
" 'intentions. Perhaps they wanted to try to find out where they came from. '\n",
" 'After all three years later, the Lord of Middle-Eightenth Night, Thyllinith '\n",
" 'took place in between the Two Tower of Elves and Eliot thinks that the whole '\n",
" 'world was already gone. With such a thousand years ago. Glossary days, '\n",
" 'Obliviously believed in God. Indeed, most people seemed to feel guilty of '\n",
" 'being mistresses, but they knew better than anyone else...they were afraid '\n",
" 'of losing their lives')\n",
"--------------------------------------------------------\n",
"\n",
"\n",
"\n",
"\n",
" prompt 4 \n",
"('Beautiful she is, sir! Lovely! Sometimes like a great tree in flower, '\n",
" 'sometimes like a white daffadowndilly, small and slender like. Hard as '\n",
" 'di’monds, soft as moonlight. Warm as sunlight, cold as frost in the stars. '\n",
" 'Proud and far-off as a snow-mountain, and as merry as any lass I ever saw '\n",
" 'with daisies in her hair in springtime….')\n",
"--------------------------------------------------------\n",
"response\n",
"\n",
"(', sir! This is the first time I have seen a woman in my life. She is '\n",
" 'beautiful, and she is as merry as any lass I ever saw with daisies in her '\n",
" 'hair in springtime. It is not so much that she looks like an old tree in '\n",
" 'flower, but it is something more than that. In this chapter, we are '\n",
" 'introduced to another kind of girl, one who seems to be quite different from '\n",
" 'all others. Her eyes are bright and full of sunshine, warm as sunlight, cold '\n",
" 'as frost in the snow-mountain, soft as moonlight. The contrast between these '\n",
" 'two kinds of characters is very different. They are both hard as well as '\n",
" 'they are, too, for example, there is no difference between them. Their '\n",
" 'differences are also marked by their differences. These differ greatly from '\n",
" 'each other, however, are similar; they do not seem to fit together. And yet, '\n",
" 'even though they look almost identical. Both are small and smaller than '\n",
" 'those of them, which make them outwardly different: they can be described as '\n",
" \"either way or larger than they were. Hard as white as di'monds, while the \"\n",
" 'latter is just as lovely as the narrator says, \"Ithou artfully sweet as '\n",
" 'flowers in your hair\" . But what makes me feel? What does she say? That she '\n",
" 'has never been told me about her beauty, then, when I see her at the end of '\n",
" 'the novel. When I hear her voice, I am sure enough to think of her face, you '\n",
" 'know how good she will be. Just as long as she was once said, she always '\n",
" 'seemed to love her, Sir James tells me, Mr. Bronte describes her as being '\n",
" 'able to describe her character. A young man would call her into questioning '\n",
" 'whether she loves him or not. If she did not want to talk about his own '\n",
" 'mind,')\n",
"--------------------------------------------------------\n",
"\n",
"\n",
"\n",
"\n",
" prompt 5 \n",
"('It strikes me that folk takes their peril with them into Lorien, and finds '\n",
" 'it there because they’ve brought it. But perhaps you could call her '\n",
" 'perilous, because she’s so strong in herself. You, you could dash yourself '\n",
" 'to pieces on her, like a ship on a rock; or drownd yourself, like a hobbit '\n",
" 'in a river. But neither rock nor river would be to blame.A time may come '\n",
" 'soon, when none will return. Then there will be need of valour without '\n",
" 'renown, for none shall remember the deeds that are done in the last defence '\n",
" 'of your homes. Yet the deeds will not be less valiant because they are '\n",
" 'unpraised.')\n",
"--------------------------------------------------------\n",
"response\n",
"\n",
"('in this chapter is a reference to the perilous nature of Lorien. It seems as '\n",
" 'though it would be better to say that she is more dangerous than any other '\n",
" 'place in the world, because it is so strong in itself. Then again, there is '\n",
" 'something about her that makes her even stronger. She is very strong, but '\n",
" 'not too strong. But she does have some strength in herself, and she has no '\n",
" 'faults. In fact, she could call herself perishous, just as she was in '\n",
" 'Middlemarch. This is an interesting contrast between the two worlds: \"Ithou '\n",
" 'do not know what I shall be\" . There are many similarities between them, for '\n",
" 'example, one might think that they will be less valiant than others. And yet '\n",
" 'another way, we should note that when you can only see how far-frighted by '\n",
" 'your own perils, then, or else. They may also wonder at the difference '\n",
" 'between these two things. For instance, the first time may come soon, all '\n",
" 'the people who live on their homes. We must remember that those who were '\n",
" \"born into the last days of the Last Hobbiton's words, like the Lord of \"\n",
" 'Glossary, unnamed with his name. He says, \"The Perilothy did not die; but '\n",
" 'rather, they had been dead.\" That is true, however, that he knows that the '\n",
" 'deeland cannot be blamed for the death of Hobbithor, which means that none '\n",
" \"of them died. Even if they weren't enough to go back home, where they went \"\n",
" 'out of town, perhaps they came from the same place. If they left behind, '\n",
" 'after all, let them pass away, such as well as they passed away. Their '\n",
" 'deaths seem to be taken careless. When they leave, nothing remains alive, '\n",
" 'neither rock nor river nor rocks nor rivers. Yet both men and women take '\n",
" 'part in their lives. Thus, each person takes responsibility for their '\n",
" 'actions. However, every man is responsible for doing good work. One thing is '\n",
" 'important to consider the dangers of being killed. Indeed, everyone feels '\n",
" 'guilty of having done great acts of bravery. To me, I am sure that my life '\n",
" 'is worth fighting against the enemy. As long as much as mine. My friend '\n",
" 'tells me that evil is wrong. What is right? Whereas I find myself in '\n",
" 'trouble. A few years agoniously, Obliviously, although I don’t want to save '\n",
" 'us from harm. At least three hundred years later, Eliot uses the word '\n",
" '\"valour without remorseenity,\" explains why Elobeth used to describe the '\n",
" 'meaning of courage and courage. These words remind me of goodness and '\n",
" 'virtue. Of course, Liotheotes use the phrase \"Valour Without renowning our '\n",
" 'souls?\" Rather than saying that Valour lacks virtuousness and self-defiance. '\n",
" 'With respectable action, Virgil suggests that Voltaire refers to him as '\n",
" 'vengeance. His description of Van Hendlearrator speaks of love and honor. '\n",
" 'Although Byron writes that \"Ofoulds never forgetting the idea of loyalty and '\n",
" 'devotion to Alcott, Elves describes the importance of friendship.')\n",
"--------------------------------------------------------\n"
]
}
],
"source": [
"results = []\n",
"for i, intxt in enumerate(tests):\n",
" result = analyzer(\n",
" intxt,\n",
" max_length=int(len(intxt) * 1.2),\n",
" min_length=int(len(intxt) * 0.7),\n",
" no_repeat_ngram_size= 2,\n",
" length_penalty=0.8,\n",
" repetition_penalty= 2.4,\n",
" num_beams=4,\n",
" early_stopping= True,\n",
" )\n",
" \n",
" print(\"\\n\"*4, f\"prompt {i} \")\n",
" pp.pprint(intxt)\n",
" print(\"-------\"*8)\n",
" print(\"response\\n\")\n",
" pp.pprint(result[0]['generated_text'])\n",
"\n",
" print(\"-------\"*8)\n",
" results.append(result[0]['generated_text'])\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "XlBszTIcqase"
},
"source": [
"# Funny Analyses"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "WXldAJvSqcY8",
"outputId": "be0accf7-97cd-4d5b-9d00-b3e5f07666a4"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"('\\n'\n",
" 'To be fair, you have to have a very high IQ to understand Rick and Morty. '\n",
" 'The humour is extremely subtle, and without a solid grasp of theoretical '\n",
" \"physics most of the jokes will go over a typical viewer's head. There's also \"\n",
" \"Rick's nihilistic outlook, which is deftly woven into his characterisation- \"\n",
" 'his personal philosophy draws heavily from Narodnaya Volya literature, for '\n",
" 'instance. The fans understand this stuff; they have the intellectual '\n",
" 'capacity to truly appreciate the depths of these jokes, to realise that '\n",
" \"they're not just funny- they say something deep about LIFE. As a consequence \"\n",
" \"people who dislike Rick & Morty truly ARE idiots- of course they wouldn't \"\n",
" \"appreciate, for instance, the humour in Rick's existential catchphrase \"\n",
" '\"Wubba Lubba Dub Dub,\" which itself is a cryptic reference to Turgenev\\'s '\n",
" \"Russian epic Fathers and Sons. I'm smirking right now just imagining one of \"\n",
" 'those addlepated simpletons scratching their heads in confusion as Dan '\n",
" \"Harmon's genius wit unfolds itself on their television screens. What fools.. \"\n",
" 'how I pity them. 😂\\n'\n",
" 'And yes, by the way, i DO have a Rick & Morty tattoo. And no, you cannot see '\n",
" \"it. It's for the ladies' eyes only- and even then they have to demonstrate \"\n",
" \"that they're within 5 IQ points of my own (preferably lower) beforehand. \"\n",
" 'Nothin personnel kid 😎\\n')\n"
]
}
],
"source": [
"copypasta = \"\"\"\n",
"To be fair, you have to have a very high IQ to understand Rick and Morty. The humour is extremely subtle, and without a solid grasp of theoretical physics most of the jokes will go over a typical viewer's head. There's also Rick's nihilistic outlook, which is deftly woven into his characterisation- his personal philosophy draws heavily from Narodnaya Volya literature, for instance. The fans understand this stuff; they have the intellectual capacity to truly appreciate the depths of these jokes, to realise that they're not just funny- they say something deep about LIFE. As a consequence people who dislike Rick & Morty truly ARE idiots- of course they wouldn't appreciate, for instance, the humour in Rick's existential catchphrase \"Wubba Lubba Dub Dub,\" which itself is a cryptic reference to Turgenev's Russian epic Fathers and Sons. I'm smirking right now just imagining one of those addlepated simpletons scratching their heads in confusion as Dan Harmon's genius wit unfolds itself on their television screens. What fools.. how I pity them. 😂\n",
"And yes, by the way, i DO have a Rick & Morty tattoo. And no, you cannot see it. It's for the ladies' eyes only- and even then they have to demonstrate that they're within 5 IQ points of my own (preferably lower) beforehand. Nothin personnel kid 😎\n",
"\"\"\"\n",
"\n",
"pp.pprint(copypasta)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "EYFfE0Cash8v"
},
"outputs": [],
"source": [
"def get_analysis(prompt_text, max_L_ratio=1):\n",
"\n",
" result = analyzer(\n",
" prompt_text,\n",
" max_length=int(len(prompt_text) * max_L_ratio),\n",
" min_length=int(len(prompt_text) * 0.7),\n",
" no_repeat_ngram_size= 2,\n",
" length_penalty=0.8,\n",
" repetition_penalty= 2.4,\n",
" num_beams=4,\n",
" early_stopping= True,\n",
" )\n",
" output = result[0]['generated_text']\n",
" pp.pprint(output)\n",
"\n",
" return output"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "Vmz56ULxtVF3",
"outputId": "b4754608-ce2e-4bf1-a149-32e088a42f78"
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/usr/local/lib/python3.7/dist-packages/transformers/pipelines/base.py:978: UserWarning: You seem to be using the pipelines sequentially on GPU. In order to maximize efficiency please use a dataset\n",
" UserWarning,\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"('The narrator\\'s comment that \"the world is not\" to the people who dislike '\n",
" 'Rick and Morty are idiots. But they do not understand what they say about '\n",
" 'their characters, even though they have an extremely high IQ. They cannot '\n",
" 'appreciate the humor of the show itself- too much in the comedy. It makes '\n",
" 'them seem to be more intelligent than any other person. Even when they '\n",
" 'criticize the jokes are very clever, but they lack the intellectual capacity '\n",
" 'to comprehend the subtleties of these comics; it seems to make up with this '\n",
" 'point, there is also a great deal with Dan Harmon: \"I don\\'t know nothing.\" '\n",
" 'This is another example of his genius witted by him. In fact, he has no '\n",
" 'idealism. He does not just want to understand the humourness. And I '\n",
" 'amusement at least, as if you can see anything else. As for instance, '\n",
" 'however, especially those stupid fools because they believe that they will '\n",
" 'never really understand why they think they would rather than simply enjoy '\n",
" 'watching the entire episode. For example, although most of allusion to '\n",
" 'Dickenian attitude towards the audience. Indeed, we should take pleasure in '\n",
" 'terms of my own perspective on life. We might wonder why some people hate it '\n",
" 'so much of course, which means that everyone else knows something deeper '\n",
" 'than anyone else could care about things like me. Just as long as far as '\n",
" \"well as such satire comes from the same thing. There'll be little or less \"\n",
" \"interested in making fun of it. That'dobbery was written by someone who knew \"\n",
" 'how ridiculously it was meant to look at it, then, since it had been '\n",
" 'published in its first few years before it came out into existence. So many '\n",
" 'critics were writing about science and philosophy. Yet again, while others '\n",
" 'did not bother to try to explain the depths of reality. However, one must '\n",
" 'note that the fanbases themselves on the absurdity of modern society. One '\n",
" 'reason for rejecting the concept of self-delusions may be hard to justify '\n",
" 'their disapprovalism. Another example is the way in which Stevenson writes, '\n",
" '\"Why? Why do you go?\" . Perhaps it is possible that Richard Mowthorne wrote, '\n",
" 'perhaps because it contains deep philosophical insight into human nature. Of '\n",
" 'course Mr. Morphine uses language to describe himself as being able to '\n",
" 'express his own opinions about the worldview of humanity. His obsession with '\n",
" 'death is partly due to his desire to live in order to avoid having to suffer '\n",
" 'from desperation. What is going onwardly, therefore, is that despite his '\n",
" 'refusal to listen to Dr. Othello, whereby Rostanders refuse to accept the '\n",
" 'notion that humans are incapable of understanding the realities of manhood. '\n",
" 'If they fail to grasp the concepts of morality, they need not to pay '\n",
" 'attention to scientific truth. Their ignorance leads them to ridiculed by '\n",
" 'the readership of God, and thus they feel guilty of taking responsibility '\n",
" 'for their own actions. To put it aside, it becomes clear that Ricardiano '\n",
" 'Voly is merely attempting to convince us that men are capable of thinking '\n",
" 'about religion and spirituality. These two main themes are related to '\n",
" 'religious belief systems, both of which are associated with Christian '\n",
" 'doctrine. Both are concerned with Christianity and social justice. '\n",
" 'Furthermore, the rest of our faith in Jesus Christ and Satan are responsible '\n",
" 'for the destruction of mankind. Thus, Christians are often misguided against '\n",
" 'the gods whom they worship God. At the end of each episode, Death is '\n",
" 'actually based on biblical references to Russian epic literature. '\n",
" 'Aristocracy refers to classical Russian literature, particularly in Russia, '\n",
" 'are used to represent the idealistic view of evil. Like the Romantic '\n",
" 'philosophers, Doctor Defargenev describes the Greek mythology. Notes that '\n",
" 'Darwinists write novels, including the Book of Fathers and '\n",
" 'Sons/Fatheritevish poetry. Also, Stoker suggests that Babbitt represents the '\n",
" 'ultimate meaning behind the Bible. Although the word \"Godlepants\" reflects '\n",
" 'the fundamental principles of love and redemption. Though the book is full '\n",
" \"of divine revelation. The title of Chapter 3 is part of Narodnay volyavoks' \"\n",
" 'work. On the other hand, Crane draws heavily upon the story of Adam and Mary '\n",
" 'Shelley provides the basis for heroquy.')\n"
]
}
],
"source": [
"mortimer = get_analysis(copypasta)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "kA6zu9KRq0_g",
"outputId": "4cafcb30-535b-4870-b8af-cc98b2b4644e"
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/usr/local/lib/python3.7/dist-packages/transformers/pipelines/base.py:978: UserWarning: You seem to be using the pipelines sequentially on GPU. In order to maximize efficiency please use a dataset\n",
" UserWarning,\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"(\". Dr. Romero: We're kids, not monsters; we've been through a lot of trauma. \"\n",
" 'Then, we get to see the difference between human beings and children. This '\n",
" 'is an important question in this chapter as well as throughout the rest of '\n",
" \"the novel. Carmen: I'm not kidnapped by my parents. She's not evil, nor am \"\n",
" \"I? Dracula: You'll never be able to stop me from killing you. But I can't \"\n",
" 'help but wonder why do you want to kill them.')\n"
]
}
],
"source": [
"spy_kids = \"\"\"\n",
"Carmen: We're kids, not monsters.\n",
"Dr. Romero: What's the difference?\n",
"\"\"\"\n",
"\n",
"\n",
"interpretation = get_analysis(spy_kids, \n",
" max_L_ratio=8, # for short input text\n",
" )\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "OzdD0ZNyuuqL"
},
"outputs": [],
"source": [
""
]
}
],
"metadata": {
"accelerator": "GPU",
"colab": {
"collapsed_sections": [],
"name": "example - t5-v1_1-L-semantic-analyzer",
"provenance": [],
"include_colab_link": true
},
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment