Skip to content

Instantly share code, notes, and snippets.

@itsuncheng
Created January 17, 2021 07:45
Show Gist options
  • Save itsuncheng/f3c4dde81ac4651383c4480958da4f8e to your computer and use it in GitHub Desktop.
Save itsuncheng/f3c4dde81ac4651383c4480958da4f8e to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Import Library"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from transformers import pipeline\n",
"import os"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Setting GPU and Model to Use"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"## Setting to use the 0th GPU\n",
"os.environ[\"CUDA_VISIBLE_DEVICES\"] = \"0\"\n",
"\n",
"## Setting to use the bart-large-cnn model for summarization\n",
"summarizer = pipeline(\"summarization\")\n",
"\n",
"## To use the t5-base model for summarization:\n",
"## summarizer = pipeline(\"summarization\", model=\"t5-base\", tokenizer=\"t5-base\", framework=\"tf\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Input the Text to Summarize"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"text = \"\"\"One month after the United States began what has become a troubled rollout of a national COVID vaccination campaign, the effort is finally gathering real steam.\n",
"Close to a million doses -- over 951,000, to be more exact -- made their way into the arms of Americans in the past 24 hours, the U.S. Centers for Disease Control and Prevention reported Wednesday. That's the largest number of shots given in one day since the rollout began and a big jump from the previous day, when just under 340,000 doses were given, CBS News reported.\n",
"That number is likely to jump quickly after the federal government on Tuesday gave states the OK to vaccinate anyone over 65 and said it would release all the doses of vaccine it has available for distribution. Meanwhile, a number of states have now opened mass vaccination sites in an effort to get larger numbers of people inoculated, CBS News reported.\"\"\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Summarize"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" Over 951,000 doses of vaccine given in one day in the past 24 hours, CDC says . That's the largest number of shots given in a month since the rollout began . The federal government gave states the OK to vaccinate anyone over 65 on Tuesday . A number of states have now opened mass vaccination sites in an effort to get more people inoculated, CBS News reports .\n"
]
}
],
"source": [
"summary_text = summarizer(text, max_length=100, min_length=5, do_sample=False)[0]['summary_text']\n",
"print(summary_text)"
]
}
],
"metadata": {
"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.6.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment