Skip to content

Commit

Permalink
Add text summarization JSON example (#177)
Browse files Browse the repository at this point in the history
* Add example for adding context information for prompting

* delete

* Add text summarization JSON example

* Reformat notebook

* Replace we with you

* Add licensing

* update colab button link

* Reformat notebook

* Reformat notebook

* Reformat notebook

* Use default temperature, use pro

---------

Co-authored-by: Mark Daoust <markdaoust@google.com>
  • Loading branch information
shilpakancharla and MarkDaoust authored May 30, 2024
1 parent c231f19 commit cae597c
Showing 1 changed file with 289 additions and 0 deletions.
289 changes: 289 additions & 0 deletions examples/json_capabilities/Text_Summarization.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,289 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "sP8PQnz1QrcF"
},
"source": [
"##### Copyright 2024 Google LLC."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cellView": "form",
"id": "ca23c3f523a7"
},
"outputs": [],
"source": [
"# @title Licensed under the Apache License, Version 2.0 (the \"License\");\n",
"# you may not use this file except in compliance with the License.\n",
"# You may obtain a copy of the License at\n",
"#\n",
"# https://www.apache.org/licenses/LICENSE-2.0\n",
"#\n",
"# Unless required by applicable law or agreed to in writing, software\n",
"# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
"# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
"# See the License for the specific language governing permissions and\n",
"# limitations under the License."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "bxGr_x3MRA0z"
},
"source": [
"# Gemini API: Text Summarization\n",
"\n",
"<table class=\"tfo-notebook-buttons\" align=\"left\">\n",
" <td>\n",
" <a target=\"_blank\" href=\"https://colab.research.google.com/github/google-gemini/cookbook/blob/main/examples/json_capabilities/Text_Summarization.ipynb\"><img src = \"https://www.tensorflow.org/images/colab_logo_32px.png\"/>Run in Google Colab</a>\n",
" </td>\n",
"</table>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "ysy--KfNRrCq"
},
"source": [
"You will use Gemini API's JSON capabilities to extract characters, locations, and summary of the plot from a story."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"id": "Ne-3gnXqR0hI"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[33mWARNING: There was an error checking the latest version of pip.\u001b[0m\u001b[33m\n",
"\u001b[0m"
]
}
],
"source": [
"!pip install -U -q google-generativeai"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"id": "EconMHePQHGw"
},
"outputs": [],
"source": [
"import google.generativeai as genai\n",
"\n",
"import json\n",
"from IPython.display import Markdown\n",
"from typing_extensions import TypedDict # in python 3.12 replace typing_extensions with typing"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "eomJzCa6lb90"
},
"source": [
"## Configure your API key\n",
"\n",
"To run the following cell, your API key must be stored it in a Colab Secret named `GOOGLE_API_KEY`. If you don't already have an API key, or you're not sure how to create a Colab Secret, see [Authentication](https://github.com/google-gemini/cookbook/blob/main/quickstarts/Authentication.ipynb) for an example."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"id": "v-JZzORUpVR2"
},
"outputs": [],
"source": [
"from google.colab import userdata\n",
"GOOGLE_API_KEY=userdata.get('GOOGLE_API_KEY')\n",
"\n",
"genai.configure(api_key=GOOGLE_API_KEY)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "kNE5XslpPpT0"
},
"source": [
"## Example"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"id": "8V5lHRmkNejR"
},
"outputs": [
{
"data": {
"text/markdown": [
"Anya gripped the worn leather strap of her satchel, her knuckles white as pearls against the tanned hide. Wind whipped strands of her raven hair across her face, obscuring the treacherous path that snaked its way down the cliff face towards the Whisperwood below. Legend claimed the forest whispered secrets to those who dared to listen, secrets both wondrous and terrible. Anya, however, only heard the insistent shriek of the wind, a chilling reminder of the urgency of her mission. \n",
"\n",
"The fate of Eldoria, her homeland, rested on her slender shoulders. She carried within her satchel a vial containing the last shimmering drop of Moonsilver, a mystical elixir capable of healing the Blight that was slowly strangling the life from their world. The Blight, a creeping darkness that withered everything it touched, had originated in the Shadowlands, a desolate realm ruled by the tyrannical sorcerer, Malkor. \n",
"\n",
"Anya's destination lay hidden deep within the Whisperwood: the Grove of Whispers, a sanctuary guarded by the enigmatic Whisperers, an ancient order sworn to protect the balance of nature. Legend claimed they held the knowledge to activate the Moonsilver, knowledge lost to the rest of Eldoria. Anya prayed the legends held true. She had witnessed firsthand the devastating effects of the Blight: the once vibrant fields now barren wastelands, the vibrant forests reduced to skeletal husks. \n",
"\n",
"Reaching the forest floor, she pushed through a tangle of thorny vines, their leaves the color of dried blood. An oppressive silence descended, the wind's mournful song swallowed by the dense canopy overhead. Sunlight struggled to penetrate the thick foliage, casting the forest floor in an eerie, perpetual twilight. Anya shivered, pulling her cloak tighter around her against the unnatural chill.\n",
"\n",
"The forest pulsed with an unseen energy, a living entity that observed her every move with unseen eyes. She could almost feel the whispers brushing against her skin, whispers that seemed to slither into her mind, whispering doubts, fears, temptations. She pressed on, clutching the vial in her satchel like a talisman, her resolve hardening with each step. \n",
"\n",
"Days bled into nights and still Anya journeyed deeper into the Whisperwood, her supplies dwindling, her body weary. She subsisted on the meager berries she could find, the sweet juice tinged with a strange bitterness that lingered on her tongue. Sleep offered little respite, haunted by nightmares of the Blight consuming everything she held dear, the chilling laughter of Malkor echoing in her ears. \n",
"\n",
"One evening, as the last rays of sunlight painted the forest in hues of gold and crimson, Anya stumbled upon a clearing. In the center stood a majestic oak, its branches reaching skyward like gnarled fingers, its leaves rustling softly despite the absence of wind. At the base of the tree, a lone figure sat cross-legged, seemingly carved from the very wood itself. He wore a robe of woven leaves and bark, his face obscured by a deep hood. \n",
"\n",
"\"You seek the Whisperers,\" the figure rasped, his voice like the rustling of dry leaves.\n",
"\n",
"Anya's heart leaped. \"I do,\" she breathed, her voice hoarse from exhaustion. \"I am Anya, and I bring with me the last of the Moonsilver. Eldoria… my people… they are dying. We need your help.\"\n",
"\n",
"The figure slowly raised his head, and Anya found herself staring into eyes that glowed with an otherworldly light, eyes as ancient as the forest itself. \"The Grove of Whispers is not a place you find, child,\" he intoned. \"It is a place that finds you.\"\n",
"\n",
"And as the figure, whose name was Silvanus, spoke these words, the forest around them shimmered and shifted. The trees surrounding the clearing parted as if alive, revealing a hidden path, bathed in an ethereal, silver light. Anya's breath caught in her throat, hope blooming in her chest like a fragile flower pushing through the barren earth. This was it. This was her chance to save her world. \n"
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"model = genai.GenerativeModel(model_name='gemini-1.5-pro-latest')\n",
"prompt = \"Generate a 10 paragraph fantasy story. Include at least 2 named characters and 2 named locations. Give as much detail in the story as possible.\"\n",
"response = model.generate_content(prompt)\n",
"story = response.text\n",
"Markdown(story)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"id": "sShzxm3JNm6M"
},
"outputs": [],
"source": [
"class Character(TypedDict):\n",
" name: str\n",
" description: str\n",
" alignment: str\n",
"\n",
"class Location(TypedDict):\n",
" name: str\n",
" description: str\n",
"\n",
"class TextSummary(TypedDict):\n",
" synopsis: str\n",
" genres: list[str]\n",
" locations: list[Location]\n",
" characters: list[Character]"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"id": "QGdJnd0AOKbu"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 10.4 ms, sys: 5.78 ms, total: 16.2 ms\n",
"Wall time: 6.02 s\n"
]
}
],
"source": [
"%%time\n",
"prompt = f\"\"\"\n",
"Generate summary of the story. With a list of genres locations and characters.\n",
"\n",
"{story}\"\"\"\n",
"response = model.generate_content(prompt, generation_config={\"response_mime_type\": \"application/json\", \"response_schema\": TextSummary})"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"id": "d5tOgde6ONo3"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{\n",
" \"genres\": [\n",
" \"fantasy\",\n",
" \"adventure\"\n",
" ],\n",
" \"locations\": [\n",
" {\n",
" \"name\": \"Eldoria\"\n",
" },\n",
" {\n",
" \"name\": \"Whisperwood\"\n",
" },\n",
" {\n",
" \"name\": \"Shadowlands\"\n",
" },\n",
" {\n",
" \"name\": \"Grove of Whispers\"\n",
" }\n",
" ],\n",
" \"synopsis\": \"Anya, a young woman from Eldoria, embarks on a perilous journey to the Whisperwood, a mystical forest, to find the Grove of Whispers and seek help from the Whisperers, an ancient order. Her homeland is being consumed by the Blight, a creeping darkness originating from the Shadowlands ruled by the evil sorcerer Malkor. Anya carries the last hope for Eldoria, a vial of Moonsilver, which needs the Whisperers' knowledge to activate. Facing perilous paths, strange whispers, and her own fears, Anya's determination leads her to Silvanus, a figure seemingly a part of the forest itself. Silvanus guides her towards the hidden Grove of Whispers, offering a glimmer of hope for Anya's mission to save her people and her world.\"\n",
"}\n"
]
}
],
"source": [
"print(json.dumps(json.loads(response.text), indent=4))"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "FhFfzDMk36aI"
},
"source": [
"## Summary\n",
"In this example, you used Gemini API to extract various information from the story. In this case, you could have done it without the help of the model in no time, but imagine how much time it would save if the text would be 10 times as long or even longer.\n",
"\n",
"Please see the other notebooks in this directory to learn more about how you can use the Gemini API for other JSON related tasks."
]
}
],
"metadata": {
"colab": {
"name": "Text_Summarization.ipynb",
"toc_visible": true
},
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

0 comments on commit cae597c

Please sign in to comment.