Text Summarization
Copyright 2025 Google LLC.
You will use Gemini API's JSON capabilities to extract characters, locations, and summary of the plot from a story.
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 141.0/141.0 kB 3.4 MB/s eta 0:00:00
Configure your API key
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 for an example.
Example
{'characters': [{'alignment': 'Good',
'description': 'A cartographer and adventurer seeking the '
'lost city of Eldoria.',
'name': 'Elara'},
{'alignment': 'Neutral',
'description': 'A creature of stone and shadow tasked with '
'protecting the secrets of Eldoria.',
'name': 'Guardian of the Hidden Passage'}],
'genres': ['Fantasy', 'Adventure'],
'locations': [{'description': 'A treacherous mountain range with jagged '
'peaks, dangerous paths, and a hidden passage '
'to Eldoria.',
'name': "Dragon's Tooth Mountains"},
{'description': 'A legendary, vanished civilization rumored to '
'hold unimaginable riches and powerful magic.',
'name': 'Eldoria (Lost City)'},
{'description': 'A village where tales of Eldoria are whispered '
'in taverns.',
'name': 'Whisperwind Village'}],
'synopsis': 'Elara, a cartographer and adventurer, seeks the lost city of '
"Eldoria, following an ancient map through the Dragon's Tooth "
'mountains. She faces treacherous terrain and a guardian '
'creature, ultimately using music to connect with the guardian '
"and realizing that Eldoria's true value lies in its wisdom, not "
'material riches. She chooses to leave Eldoria undisturbed, '
'gaining newfound courage and respect for legends.'}
Summary
In this example, you used the Gemini API to extract key information from a story. This information could be fed into a structured database or used as a prompt for other writers to create their own versions.
This technique of converting large open-ended text to structured data works across other formats too, not just stories.
Please see the other notebooks in this directory to learn more about how you can use the Gemini API for other JSON related tasks.