Text Classification
json_capabilitiesgemini-cookbookgemini-apiexamplesgemini
Export
Copyright 2025 Google LLC.
[ ]
Gemini API: Text Classification
You will use the Gemini API to classify what topics are relevant in the text.
Install dependencies
[ ]
Set up 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 the Authentication quickstart for an example.
[ ]
Example
[ ]
MODEL_ID
[ ]
[ ]
[{'relevance': <Relevance.STRONG: 'strong'>, 'topic': 'Sports'},
{'relevance': <Relevance.STRONG: 'strong'>, 'topic': 'Health'},
{'relevance': <Relevance.WEAK: 'weak'>, 'topic': 'Economics'},
{'relevance': <Relevance.STRONG: 'strong'>, 'topic': 'Social Issues'},
{'relevance': <Relevance.WEAK: 'weak'>, 'topic': 'Art'}]
Summary
Now, you know how to classify text into different categories. Feel free to experiment with other texts, or provide a specific set of possible topics.
Please see the other notebooks in this directory to learn more about how you can use the Gemini API for other JSON related tasks.