{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "transformers_summarizer_pipeline.ipynb",
      "provenance": [],
      "authorship_tag": "ABX9TyMntllncmgnv9cwtyo9jpkF",
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "accelerator": "GPU"
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/mrm8488/shared_colab_notebooks/blob/master/transformers_summarizer_pipeline.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "2vNtiYck9SuX",
        "colab_type": "text"
      },
      "source": [
        "# [Hugginface/Transformers](https://github.com/huggingface/transformers) 🤗 new **summarizer** pipeline "
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "7kKAcx4W8jsI",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "!git clone https://github.com/huggingface/transformers \\\n",
        "&& cd transformers \\"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "0PfDjAzg8Nj5",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "!pip install -q ./transformers"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Yy-9zKYo8Q_k",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "from transformers import pipeline"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "CSc-qz7e-Pwf",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "summarizer = pipeline(\"summarization\")"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "2-loLKVL92dz",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "LONG_BORING_TENNIS_ARTICLE = \"\"\"\n",
        " Andy Murray  came close to giving himself some extra preparation time for his w\n",
        "edding next week before ensuring that he still has unfinished tennis business to\n",
        " attend to. The world No 4 is into the semi-finals of the Miami Open, but not be\n",
        "fore getting a scare from 21 year-old Austrian Dominic Thiem, who pushed him to \n",
        "4-4 in the second set before going down 3-6 6-4, 6-1 in an hour and three quarte\n",
        "rs. Murray was awaiting the winner from the last eight match between Tomas Berdy\n",
        "ch and Argentina's Juan Monaco. Prior to this tournament Thiem lost in the secon\n",
        "d round of a Challenger event to soon-to-be new Brit Aljaz Bedene. Andy Murray p\n",
        "umps his first after defeating Dominic Thiem to reach the Miami Open semi finals\n",
        " . Muray throws his sweatband into the crowd after completing a 3-6, 6-4, 6-1 vi\n",
        "ctory in Florida . Murray shakes hands with Thiem who he described as a 'strong \n",
        "guy' after the game . And Murray has a fairly simple message for any of his fell\n",
        "ow British tennis players who might be agitated about his imminent arrival into \n",
        "the home ranks: don't complain. Instead the British No 1 believes his colleagues\n",
        " should use the assimilation of the world number 83, originally from Slovenia, a\n",
        "s motivation to better themselves. At present any grumbles are happening in priv\n",
        "ate, and Bedene's present ineligibility for the Davis Cup team has made it less \n",
        "of an issue, although that could change if his appeal to play is allowed by the \n",
        "International Tennis Federation. Murray thinks anyone questioning the move, now \n",
        "it has become official, would be better working on getting their ranking closer \n",
        "to his. 'If he was 500 in the world they wouldn't be that fussed about it but ob\n",
        "viously he threatens their position a bit,' said the 27 year-old Scot. ' and he'\n",
        "s obviously the British number two, comfortably. 'So they can complain but the b\n",
        "est thing to do is use it in the right way and accept it for what it is, and try\n",
        " to use it as motivation whether they agree with it or not. He's British now so \n",
        "they've just got to deal with it. Murray stretches for a return after starting h\n",
        "is quarter final match slowly on the show court . Thiem held nothing back as he \n",
        "raced through the opening set, winning it 6-3 with a single break . The young Au\n",
        "strian is considered to be one of the hottest prospects on the ATP Tour . 'I wou\n",
        "ld hope that all the guys who are below him now like James (Ward) , Kyle (Edmund\n",
        ") , Liam (Broady) they will use it as motivation. If he becomes eligible for Dav\n",
        "is Cup then those guys are going to have to prove themselves. 'It can only be se\n",
        "en as a positive for those guys using it to try to get better. He's a good playe\n",
        "r but so are James and Kyle and Liam has improved. Aljaz is there, he's on the t\n",
        "our every week, the other guys aren't quite there yet.' For the first time Murra\n",
        "y, who has an encyclopaedic knowledge of the top 100, gave his opinion of Bedene\n",
        ": 'He's a good player with a very good serve. He's a legitimate top 100 player, \n",
        "when he plays Challengers he's there or thereabouts, when he plays on the main t\n",
        "our he wins matches, it's not like he turns up and always loses in the first rou\n",
        "nd. Murray's fiancee was once again watching from the stands shaded by a huge br\n",
        "immed hat . Kim Sears flashes her enormous diamond engagement ring while watchin\n",
        "g her beau on court . 'He had a bad injury last year (wrist) but has recovered w\n",
        "ell. I would imagine he would keep moving up the rankings although I don't know \n",
        "exactly how high he can go. I've practised with him a couple of times, I haven't\n",
        " seen him play loads, but when you serve as well as he does it helps. I would im\n",
        "agine he' s going to be comfortably in the top 70 or 80 in the world for a while\n",
        ".' It is understood the Lawn Tennis Association will give background support to \n",
        "his case regarding the Davis Cup but have made it clear that the onus is on him \n",
        "to lead the way. An official statement said: 'To have another player in the men'\n",
        "s top 100 is clearly a positive thing for British tennis and so we very much wel\n",
        "come Aljaz's change in citizenship.' The last comparable switch came twenty year\n",
        "s ago when Greg Rusedski arrived from Canada. It was by no means universally pop\n",
        "ular but, like Bedene, he pledged that he was in for the long haul and, in fairn\n",
        "ess to him, he proved true to his word. Loising the first set shocked Murray int\n",
        "o life as he raced to a commanding lead in the second . The No 3 seed sent over \n",
        "a few glaring looks towards his team before winning the second set . Murray had \n",
        "to put such matters aside as he tackled the unusually talented Thiem, a delight \n",
        "to watch. Coached by Boris Becker's veteran mentor Gunter Bresnik, he slightly r\n",
        "esembles Andy Roddick and hits with similar power but more elegance. His single \n",
        "handed backhand is a thing of rare beauty. However, he has had a mediocre season\n",
        " coming into this event and there was little to forewarn of his glorious shotmak\n",
        "ing that seemed to catch Murray unawares early on. The world No 4 looked to have\n",
        " worked him out in the second, but then suffered one of his periopdic mental lap\n",
        "ses and let him back in from 4-1 before closing it out with a break. After break\n",
        "ing him for 3-1 in the decider the Austrian whirlwind burnt itself out. 'He's a \n",
        "strong guy who hits the ball hard and it became a very physical match,' said Mur\n",
        "ray. Murray was presented with a celebratory cake after winning his 500th match \n",
        "in the previous round .\n",
        "\"\"\".replace('\\n','')"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "UBPXC2d38ZgB",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "summary = summarizer(LONG_BORING_TENNIS_ARTICLE)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "nnnVnKCW-8JQ",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "summary[0]['summary_text']"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "JHESa25N_fAK",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "with open('/content/summary.txt', 'w') as f:\n",
        "  f.write(summary[0]['summary_text'])\n",
        "  f.write('\\n')"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "b0zvdOQd_-0t",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "from google.colab import files\n",
        "files.download('/content/summary.txt')"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "E4qoDMt2-lcZ",
        "colab_type": "text"
      },
      "source": [
        "> Colab made by [Manuel Romero/mrm8488](https://twitter.com/mrm8488)"
      ]
    }
  ]
}