{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/LAION-AI/Open-Assistant/blob/main/notebooks/data-augmentation/essay-instructions/essay-instructions.ipynb)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "8zsmJ96eaL2w"
   },
   "outputs": [],
   "source": [
    "!pip install transformers"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "Pt6qbTsjW7Kp"
   },
   "source": [
    "Put your essay here, [source of the essay used ](https://https://www.thewisdompost.com/essay/technology-essay/3387#essay-on-technology-for-college-and-university-students-essay-2-750-words)\n",
    "\n",
    "Separate paragraphs with one blank line\n",
    "(this step is annoying but important)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "id": "d_5_BDFNWneB"
   },
   "outputs": [],
   "source": [
    "essay = \"\"\"\n",
    "We live in a world driven by technology — hardly anyone would argue with you if you said this. \n",
    "Technology, literally meaning the “science of craft”, refers to the collection of techniques, \n",
    "skills, methods, and processes used to produce goods or services or for accomplishing objectives \n",
    "such as scientific investigation. Technology can be embedded in machines enabling them to be \n",
    "used by people even without a detailed knowledge of their inner workings. Technological growth \n",
    "is closely linked to the expansion of scientific research and knowledge. In the last 50 years, \n",
    "thanks to the exponential increases in computing power and microchip design and manufacture, \n",
    "there has been unprecedented innovation and technological growth in nearly every field of human \n",
    "endeavour from health and transport to industrial production and education.\n",
    "\n",
    "It is automotive technology that drives today’s electric and hybrid cars, and which will drive \n",
    "tomorrow’s driverless cars, hover-taxis and space cabs. It is technology that drives the \n",
    "ubiquitous mobile phones that you will now find in the hands of even the poorest of the world’s \n",
    "poor. It is technology that creates hybrid seeds that resist inhospitable climatic conditions \n",
    "and difficult terrain, giving high yields in shorter times. It is advancing medical technology \n",
    "that makes remote surgery, minimally invasive surgery and life-saving cures using stem cell \n",
    "transplants. Technology puts spacecrafts on asteroids and distant planets and lets us see \n",
    "new worlds. Technology splits atoms, revealing their secrets, and gives us ways to exploit \n",
    "them to create energy, quantum storage for data, and virtual reality games.\n",
    "\n",
    "There are people who strongly oppose technology and claim that it spells the death of \n",
    "‘humanity’, and that we are approaching the day when machines will rule everything. They refer \n",
    "to fans of technology as ‘techies’ or sometimes ‘geeks’. On the other hand, proponents of \n",
    "technology call these people Luddites, a derogatory name for someone who is opposed to \n",
    "industrialisation, automation, computerisation and new technologies in general.\n",
    "Is this true? Is technology really a curse disguised as a blessing? Many believe that the \n",
    "convergence of biotechnology and AI might be the most consequential development of all.\n",
    "\n",
    "In the last five decades, two areas in particular have grown faster than the rest, powered \n",
    "by research and advances in computing power. One is artificial intelligence, or AI; the other \n",
    "is biotechnology. Huge benefits have emerged from each of them for human beings in general, \n",
    "such as self-driving cars — which will dramatically reduce the death rate from road accidents \n",
    "— and robotic surgery, which enables precise, highly efficient and targeted surgical \n",
    "interventions. Yet, visionaries like Yuval Noah Harari, author of the best-selling \"Homo \n",
    "Sapiens\" and \"Deus\", are now warning that the convergence of biotechnology and AI will \n",
    "irreversibly and unpredictably change both the quality of human life and its challenges in \n",
    "the next few decades. A good example of this is the facial recognition technology that is \n",
    "now present in all photo management programs. The AI in the software is capable of not \n",
    "only spotting the faces in every photograph but also recognising the person by name.\n",
    "This technology has now expanded so that photo apps can recognise cats, dogs, beaches, \n",
    "mountains and cars too. Computers with AI are already correctly identifying human emotions \n",
    "through observing facial expressions and body movements. Some robots are able to mimic \n",
    "human emotions. This is called affective computing, sometimes called artificial emotional \n",
    "intelligence, and refers to the study and development of systems and devices that can \n",
    "recognize, interpret, process, and simulate human affects.\n",
    "\n",
    "How could this be a negative?\n",
    "The ability to read human emotions is just a step away from predicting human emotions. For \n",
    "example, if a computer attached to a video camera could identify which products a consumer \n",
    "is showing greater interest in or which ones he is really keen to buy, various tactics \n",
    "could be used to influence her to buy it. Activists worry that computers that can understand \n",
    "and anticipate human wishes and desires by scanning their irises and analysing their \n",
    "micro-expressions could also be programmed to exploit and manipulate them. Another very real \n",
    "fear is that humanoid computers with human-like skin, speech, and expressions could jeopardise \n",
    "and dehumanise relationship and create emotional vacuums.\n",
    "\n",
    "An enduring fear of Luddites has always been that computers will rob humans of their \n",
    "livelihood by taking their jobs and doing them more efficiently at lower cost. However, in \n",
    "reality the exact opposite has happened. As computerised machines began taking over mechanical \n",
    "and repetitive human activities, new jobs for people opened up that needs thinking and \n",
    "analytical skills and judgement, or human interpersonal skills. A good example is the \n",
    "worldwide proliferation of call centres. When drones were invented many feared that pilots \n",
    "would soon be redundant. However, few people know that it takes almost 30 people to fly \n",
    "one military drone, and an additional 50 people to analyze and make sense of the data being \n",
    "streamed back by the drone. The US army suffers from a serious shortage of trained, high \n",
    "quality drone pilots; anyone who masters this skill will have a job. But a social scientist \n",
    "warns that in 10 years, it is certain that computers will be flying that drone and humans \n",
    "will be redundant. Equally sure is that some brand new skill requirement will have opened \n",
    "up with advancing technology, calling for new talents.\n",
    "\n",
    "In the 20th century, a young man was supposed to choose a skill, vocation or profession, \n",
    "master it through education and practice, and then earn a living from it till he or she \n",
    "retired. However, the fast-changing nature of technology is making skills obsolete at a \n",
    "higher rate than ever before. To survive, tomorrow young man must keep re-inventing himself \n",
    "and updating his skills continuously. Life could be difficult if every new skill has a shelf \n",
    "life of only a decade or so. Or perhaps one could look at it the other way — and say that \n",
    "changing technology will keep human beings on their toes throughout their life.\n",
    "\n",
    "Technology is the result of human inventiveness. It reflects our evolutionary heritage. We \n",
    "are neither strong like gorillas or tigers, nor fast like cheetahs and hawks, but our \n",
    "brains and thinking powers have given us the greatest edge of any species on the planet. \n",
    "Technology is a result. Technology is either inherently good or bad; it is how we use it \n",
    "that makes it so. The splitting of a hydrogen atom is technology at work. As history has \n",
    "shown us, technology can equally be used to make a nuclear bomb that kills millions — or \n",
    "generate electricity that lights up a million homes.\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "id": "JESY8Y10W6hQ"
   },
   "outputs": [],
   "source": [
    "essay_paragraphs = essay.split(\"\\n\\n\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "t1G-ZiHbZZ-Y"
   },
   "outputs": [],
   "source": [
    "model_name = \"snrspeaks/t5-one-line-summary\"\n",
    "\n",
    "from transformers import AutoModelForSeq2SeqLM, AutoTokenizer\n",
    "\n",
    "model = AutoModelForSeq2SeqLM.from_pretrained(model_name)\n",
    "tokenizer = AutoTokenizer.from_pretrained(model_name)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "8BARyupEemZ-"
   },
   "source": [
    "## Results\n",
    "Please at least check what is generated here, it's usually good but sometimes it's bs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "eyR58KFRae7n",
    "outputId": "b8e4bc29-be89-43c3-d1bc-7e90525c0e09"
   },
   "outputs": [],
   "source": [
    "preds = []\n",
    "\n",
    "for para in essay_paragraphs:\n",
    "    input_ids = tokenizer.encode(para, return_tensors=\"pt\", add_special_tokens=True)\n",
    "    generated_ids = model.generate(\n",
    "        input_ids=input_ids,\n",
    "        num_beams=5,\n",
    "        max_length=35,\n",
    "        repetition_penalty=4.5,\n",
    "        length_penalty=1.5,\n",
    "        early_stopping=True,\n",
    "        num_return_sequences=1,\n",
    "    )\n",
    "    preds.append(tokenizer.decode(generated_ids[0], skip_special_tokens=True, clean_up_tokenization_spaces=True))\n",
    "\n",
    "prompts = (\n",
    "    [\"Write an intro paragraph to an essay called\"]\n",
    "    + [\"Write a paragraph to an essay about\"] * len(preds[1:-1])\n",
    "    + [\"Write a concluding paragraph about\"]\n",
    ")\n",
    "\n",
    "assert len(preds) == len(prompts)\n",
    "\n",
    "for prompt, pred in zip(prompts, preds):\n",
    "    print(prompt, pred.lower())"
   ]
  }
 ],
 "metadata": {
  "colab": {
   "provenance": []
  },
  "kernelspec": {
   "display_name": "Python 3",
   "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.7.4 (tags/v3.7.4:e09359112e, Jul  8 2019, 20:34:20) [MSC v.1916 64 bit (AMD64)]"
  },
  "vscode": {
   "interpreter": {
    "hash": "25d5c2324055587ceaeef27650c79ce8358ea61d7689f2e0b8ada5d53f85bce4"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
