Skip to content

Commit

Permalink
Text classification JSON example (#194)
Browse files Browse the repository at this point in the history
* Reformat notebook

* Reformat notebook

* Update notebook with shorter prompt

* Adding text classification JSON example

* Reformat basic code generation

* Update colab link

* Update colab link

* Update text classification notebook

* Format notebooks

* Fix link.

---------

Co-authored-by: Mark Daoust <markdaoust@google.com>
  • Loading branch information
shilpakancharla and MarkDaoust committed Jun 11, 2024
1 parent d21f205 commit 94cbc27
Showing 1 changed file with 252 additions and 0 deletions.
252 changes: 252 additions & 0 deletions examples/json_capabilities/Text_Classification.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,252 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "KLHiTPXNTf2a"
},
"source": [
"##### Copyright 2024 Google LLC."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"cellView": "form",
"id": "oTuT5CsaTigz"
},
"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": "sP8PQnz1QrcF"
},
"source": [
"# Gemini API: Text Classification"
]
},
{
"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/examples/json_capabilities/Text_Classification.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 the Gemini API to classify what topics are relevant in the text."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"id": "Ne-3gnXqR0hI"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m158.8/158.8 kB\u001b[0m \u001b[31m1.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25h"
]
}
],
"source": [
"!pip install -U -q google-generativeai"
]
},
{
"cell_type": "code",
"execution_count": 3,
"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": 4,
"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": 5,
"metadata": {
"id": "d7vfQVmt9uUT"
},
"outputs": [
{
"data": {
"text/markdown": "## The Power of Sport: More Than Just a Game\n\nThe roar of the crowd, the thrill of victory, the agony of defeat – sports have a captivating power that transcends the boundaries of the playing field. From the meticulously choreographed dances of figure skating to the raw intensity of a football game, sports evoke emotions that run deep, uniting people from all walks of life. But beyond the entertainment value, sport plays a crucial role in shaping our society, promoting physical and mental well-being, and fostering a sense of community. \n\nThe undeniable physical benefits of sport are widely recognized. Regular participation in physical activities strengthens the body, improves cardiovascular health, and reduces the risk of chronic diseases. However, the benefits extend far beyond the physical. Studies have shown that sport can have a profound impact on mental health, reducing stress, anxiety, and depression. By encouraging teamwork, discipline, and perseverance, sports instill valuable life lessons that extend beyond the playing field.\n\nThe unifying power of sport transcends language, culture, and even political divides. Shared passions for a team or an athlete create a sense of belonging, fostering friendships and strengthening bonds within communities. The global appeal of major sporting events, such as the Olympics or the World Cup, demonstrates the universal language of sportsmanship and the ability to bring people together in a shared celebration of human achievement. \n\nThis unifying power of sport can be particularly important in addressing pressing societal issues. The \"Peace and Sport\" organization, for example, leverages the power of sport to promote peace and reconciliation in conflict zones. By providing access to sporting opportunities, the organization empowers individuals and communities to rebuild their lives and foster a sense of hope for the future.\n\nSports hold the potential to empower, inspire, and transform individuals and communities. From promoting physical and mental well-being to fostering social cohesion and addressing societal challenges, the power of sport extends far beyond the playing field. As we navigate an increasingly complex and interconnected world, the unifying and transformative power of sports will continue to play a vital role in shaping our future. \n",
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"model = genai.GenerativeModel(model_name='gemini-1.5-flash-latest')\n",
"response = model.generate_content(\"Generate a 5 paragraph article about Sports, include one other topic\")\n",
"article = response.text\n",
"Markdown(article)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"id": "704lXZcS9uUT"
},
"outputs": [],
"source": [
"prompt = f\"\"\"\n",
"Generate topics from text, the topics should be general e.g. Health:\n",
"Return a Topics.\n",
"\n",
"Follow this schema to generate JSON:\n",
"\n",
"class Topic(TypedDict):\n",
" topic: str # should be general\n",
" relevance: float\n",
"\n",
"class Topics(TypedDict):\n",
" topics: list[Topic]\n",
"\n",
"{article}\"\"\"\n",
"response = model.generate_content(prompt, generation_config={\"response_mime_type\": \"application/json\"})"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"id": "LMTjb4Gs9uUU"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{\n",
" \"topics\": [\n",
" {\n",
" \"topic\": \"Sports\",\n",
" \"relevance\": 0.8\n",
" },\n",
" {\n",
" \"topic\": \"Health\",\n",
" \"relevance\": 0.6\n",
" },\n",
" {\n",
" \"topic\": \"Community\",\n",
" \"relevance\": 0.5\n",
" },\n",
" {\n",
" \"topic\": \"Social Impact\",\n",
" \"relevance\": 0.4\n",
" },\n",
" {\n",
" \"topic\": \"Peace and Conflict\",\n",
" \"relevance\": 0.3\n",
" }\n",
" ]\n",
"}\n"
]
}
],
"source": [
"print(json.dumps(json.loads(response.text), indent=4))"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "l3XJbxtv1Htk"
},
"source": [
"## Summary\n",
"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.\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_Classification.ipynb",
"toc_visible": true
},
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

0 comments on commit 94cbc27

Please sign in to comment.