-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ae93721
commit f225e24
Showing
1 changed file
with
270 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,270 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "sP8PQnz1QrcF" | ||
}, | ||
"source": [ | ||
"# Gemini API: Text Summarization\n", | ||
"\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "bxGr_x3MRA0z" | ||
}, | ||
"source": [ | ||
"<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/quickstarts/examples/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": [], | ||
"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": 4, | ||
"metadata": { | ||
"colab": { | ||
"base_uri": "https://localhost:8080/", | ||
"height": 999 | ||
}, | ||
"id": "8V5lHRmkNejR", | ||
"outputId": "f240c0d1-0afc-43be-c831-4c27b847bc61" | ||
}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/markdown": [ | ||
"The air hung heavy with the scent of pine and damp earth as Elara, a young woman with eyes the color of storm clouds and hair like spun moonlight, navigated the winding path through the Whispering Woods. Her boots, worn and patched, crunched on the fallen leaves, the only sound besides the rustling of the ancient trees. She carried a worn leather satchel, its contents a mystery, but its weight a familiar burden on her shoulder. Elara was on a mission, a quest that had taken her far from her home in the bustling city of Aethel, a place of cobblestone streets and towering spires.\n", | ||
"\n", | ||
"Her destination was the hidden village of Eldoria, nestled deep within the heart of the Whispering Woods. Legend spoke of a powerful artifact, the Heart of Eldoria, a crystal said to hold the very essence of the forest. It was said to be guarded by the ancient tree, the Whispering Elder, a towering oak that had stood for centuries, its branches reaching towards the heavens like gnarled fingers. Elara sought the Heart of Eldoria, not for power or glory, but for a chance to heal her ailing mother, who lay dying in Aethel.\n", | ||
"\n", | ||
"As she walked, the shadows of the trees grew longer, stretching out like grasping claws. The air grew colder, and a chill ran down Elara's spine. She quickened her pace, her heart pounding in her chest. The Whispering Woods was known for its dangers, home to creatures of myth and legend, from mischievous sprites to fearsome wolves. But Elara was not afraid. She had faced danger before, and she knew that the Heart of Eldoria was worth the risk.\n", | ||
"\n", | ||
"After what felt like hours, she emerged from the dense forest and into a clearing. In the center stood a small village, its houses built of wood and stone, nestled amongst the trees. The air here was different, filled with a sense of peace and tranquility. Elara felt a sense of relief wash over her. She had reached Eldoria.\n", | ||
"\n", | ||
"As she approached the village, she was greeted by a young man named Kael, his eyes as bright as the sun and his hair the color of burnished copper. He was a woodcarver, his hands calloused and strong, and he wore a smile that could light up the darkest night. He welcomed Elara to Eldoria, offering her food and shelter. He told her of the Whispering Elder and the Heart of Eldoria, and he warned her of the dangers that lurked in the forest.\n", | ||
"\n", | ||
"Elara listened intently, her heart filled with hope. She knew that the Heart of Eldoria was her only chance to save her mother. She thanked Kael for his hospitality and told him of her quest. Kael, touched by her story, offered to help her. He knew the forest well, and he promised to guide her to the Whispering Elder.\n", | ||
"\n", | ||
"Together, they set out into the forest, following a winding path that led them deeper into the woods. The trees grew taller and thicker, their branches intertwining overhead, creating a canopy of darkness. The air grew heavy with the scent of pine and damp earth, and the only sound was the rustling of leaves and the occasional chirp of a bird.\n", | ||
"\n", | ||
"As they walked, Kael told Elara stories of the forest, tales of ancient spirits and forgotten magic. He spoke of the Whispering Elder, a tree that was said to be wise beyond measure, its roots reaching deep into the earth, its branches touching the sky. He told her that the Heart of Eldoria was a powerful artifact, capable of healing the sick and restoring life to the dying.\n", | ||
"\n", | ||
"Elara listened intently, her heart filled with hope. She knew that the Heart of Eldoria was her only chance to save her mother. She thanked Kael for his stories and his guidance, and she vowed to find the Heart of Eldoria and bring it back to Aethel.\n", | ||
"\n", | ||
"Finally, they reached the Whispering Elder, a towering oak that stood in the center of a clearing. Its branches were gnarled and twisted, its bark rough and ancient. The tree seemed to hum with a life force, its leaves rustling softly in the breeze. Elara felt a sense of awe wash over her as she stood before the Whispering Elder.\n", | ||
"\n", | ||
"Kael stepped back, leaving Elara to face the ancient tree alone. He knew that the Heart of Eldoria was guarded by the Whispering Elder, and that only those who were pure of heart could approach it. Elara closed her eyes and took a deep breath. She thought of her mother, her face pale and drawn, her voice weak and raspy. She thought of the love she had for her mother, and the pain she felt at seeing her suffer.\n", | ||
"\n", | ||
"When she opened her eyes, she saw a faint glow emanating from the base of the Whispering Elder. It was the Heart of Eldoria, a crystal that pulsed with a soft, ethereal light. Elara reached out and touched the crystal, feeling a surge of energy flow through her. She knew that the Heart of Eldoria was real, and that it held the power to heal her mother.\n", | ||
"\n", | ||
"But as she reached for the crystal, a shadow fell over her. A creature emerged from the darkness, its eyes glowing red, its teeth bared in a snarl. It was a creature of the forest, a guardian of the Whispering Elder, and it was determined to prevent Elara from taking the Heart of Eldoria.\n", | ||
"\n", | ||
"Elara knew that she had to fight. She drew her sword, its blade gleaming in the dim light of the forest. She faced the creature, her heart pounding in her chest. She knew that she was outmatched, but she was determined to protect the Heart of Eldoria and save her mother.\n", | ||
"\n", | ||
"The battle was fierce, the creature's claws slashing at Elara, its teeth snapping at her throat. But Elara fought back with all her strength, her sword flashing in the dim light. She dodged and weaved, her movements swift and precise. She knew that she had to defeat the creature, or she would never be able to take the Heart of Eldoria.\n", | ||
"\n", | ||
"Finally, with a surge of adrenaline, Elara struck. Her sword pierced the creature's heart, and it let out a roar of pain before collapsing to the ground. Elara stood over the creature, her breath coming in ragged gasps. She had won, but the victory came at a cost. She was wounded, her body aching, her clothes torn.\n", | ||
"\n", | ||
"But she had the Heart of Eldoria. She picked up the crystal, its light warming her hand. She knew that she had to return to Aethel, to her mother, and to use the Heart of Eldoria to heal her. She turned and walked away from the Whispering Elder, her heart filled with hope and determination. She knew that she had faced danger and overcome it, and that she would do whatever it took to save her mother.\n", | ||
"\n", | ||
"As she walked back through the Whispering Woods, the shadows seemed to recede, the air grew warmer, and the forest seemed to sigh with relief. Elara knew that she had done something important, something that would change her life forever. She had faced her fears, she had overcome her doubts, and she had found the Heart of Eldoria. Now, she had to bring it back to Aethel and use its power to heal her mother. She knew that the journey would be long and difficult, but she was ready. She was ready to face whatever challenges lay ahead, for she had the Heart of Eldoria, and she had the love of her mother to guide her. \n" | ||
], | ||
"text/plain": [ | ||
"<IPython.core.display.Markdown object>" | ||
] | ||
}, | ||
"execution_count": 4, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"model = genai.GenerativeModel(model_name='gemini-1.5-flash-latest', generation_config={\"temperature\": 0})\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": 5, | ||
"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": 6, | ||
"metadata": { | ||
"id": "QGdJnd0AOKbu" | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"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": 7, | ||
"metadata": { | ||
"colab": { | ||
"base_uri": "https://localhost:8080/" | ||
}, | ||
"id": "d5tOgde6ONo3", | ||
"outputId": "0aa8ce30-c5d3-4177-8a0a-c2d8581685f3" | ||
}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"{\n", | ||
" \"summary\": \"Elara, a young woman from the city of Aethel, embarks on a perilous journey to the hidden village of Eldoria in the Whispering Woods. She seeks the Heart of Eldoria, a powerful artifact said to hold the essence of the forest, in hopes of healing her ailing mother. Guided by Kael, a kind woodcarver, Elara faces dangers and battles a guardian creature to obtain the Heart. Ultimately, she triumphs and returns to Aethel with the artifact, ready to face the challenges of healing her mother.\",\n", | ||
" \"genres\": [\n", | ||
" \"Fantasy\",\n", | ||
" \"Adventure\",\n", | ||
" \"Romance\"\n", | ||
" ],\n", | ||
" \"locations\": [\n", | ||
" \"Aethel\",\n", | ||
" \"Whispering Woods\",\n", | ||
" \"Eldoria\"\n", | ||
" ],\n", | ||
" \"characters\": [\n", | ||
" \"Elara\",\n", | ||
" \"Kael\",\n", | ||
" \"Whispering Elder\",\n", | ||
" \"Guardian Creature\"\n", | ||
" ]\n", | ||
"}\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"print(json.dumps(json.loads(response.text), indent=4))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"id": "FhFfzDMk36aI" | ||
}, | ||
"source": [ | ||
"## Summary\n", | ||
"We used Gemini API to extract various information from the story. In this case, we 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": { | ||
"provenance": [] | ||
}, | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"name": "python" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 0 | ||
} |