From 0a8758df904c87c1c5e4779ea3c686944dd23f99 Mon Sep 17 00:00:00 2001 From: Google Colaboratory Team Date: Fri, 18 Sep 2026 15:53:18 -0700 Subject: [PATCH] Migrate built-in example notebooks to google-genai SDK and gemini-2.5-flash-lite. PiperOrigin-RevId: 984111740 --- .../Classify_an_image_using_Gemini.ipynb | 24 ++++++++++------- notebooks/Gemini_and_Stable_Diffusion.ipynb | 21 +++++++-------- ...nerate_images_with_Gemini_and_Vertex.ipynb | 22 +++++++-------- ..._minute_Holiday_shopping_with_Gemini.ipynb | 21 ++++++++------- .../Learning_with_Gemini_and_ChatGPT.ipynb | 22 +++++++-------- ...ristmas_cards_with_Gemini_and_Sheets.ipynb | 27 ++++++++++++------- ...Sell_lemonade_with_Gemini_and_Sheets.ipynb | 19 ++++++------- ...ini_with_Google's_Speech_to_Text_API.ipynb | 19 ++++++------- 8 files changed, 93 insertions(+), 82 deletions(-) diff --git a/notebooks/Classify_an_image_using_Gemini.ipynb b/notebooks/Classify_an_image_using_Gemini.ipynb index d196c793..2d721cba 100644 --- a/notebooks/Classify_an_image_using_Gemini.ipynb +++ b/notebooks/Classify_an_image_using_Gemini.ipynb @@ -32,26 +32,24 @@ "\n", "#Access your Gemini API key\n", "\n", - "import google.generativeai as genai\n", + "from google import genai\n", "from google.colab import userdata\n", "\n", "gemini_api_secret_name = 'GOOGLE_API_KEY' # @param {type: \"string\"}\n", "\n", "try:\n", - " GOOGLE_API_KEY=userdata.get(gemini_api_secret_name)\n", - " genai.configure(api_key=GOOGLE_API_KEY)\n", + " GOOGLE_API_KEY = userdata.get(gemini_api_secret_name)\n", + " client = genai.Client(api_key=GOOGLE_API_KEY)\n", "except userdata.SecretNotFoundError as e:\n", - " print(f'Secret not found\\n\\nThis expects you to create a secret named {gemini_api_secret_name} in Colab\\n\\nVisit https://makersuite.google.com/app/apikey to create an API key\\n\\nStore that in the secrets section on the left side of the notebook (key icon)\\n\\nName the secret {gemini_api_secret_name}')\n", + " print(f'Secret not found\\n\\nThis expects you to create a secret named {gemini_api_secret_name} in Colab\\n\\nVisit https://aistudio.google.com/app/apikey to create an API key\\n\\nStore that in the secrets section on the left side of the notebook (key icon)\\n\\nName the secret {gemini_api_secret_name}')\n", " raise e\n", "except userdata.NotebookAccessError as e:\n", " print(f'You need to grant this notebook access to the {gemini_api_secret_name} secret in order for the notebook to access Gemini on your behalf.')\n", " raise e\n", "except Exception as e:\n", " # unknown error\n", - " print(f\"There was an unknown error. Ensure you have a secret {gemini_api_secret_name} stored in Colab and it's a valid key from https://makersuite.google.com/app/apikey\")\n", - " raise e\n", - "\n", - "model = genai.GenerativeModel('gemini-pro-vision')" + " print(f\"There was an unknown error. Ensure you have a secret {gemini_api_secret_name} stored in Colab and it's a valid key from https://aistudio.google.com/app/apikey\")\n", + " raise e" ], "metadata": { "id": "yFv1abRcv2P2", @@ -72,7 +70,10 @@ "!curl -o image.jpg https://t0.gstatic.com/licensed-image?q=tbn:ANd9GcQ_Kevbk21QBRy-PgB4kQpS79brbmmEG7m3VOTShAn4PecDU5H5UxrJxE3Dw1JiaG17V88QIol19-3TM2wCHw\n", "\n", "img = PIL.Image.open('image.jpg')\n", - "response = model.generate_content(img)\n", + "response = client.models.generate_content(\n", + " model='gemini-2.5-flash-lite',\n", + " contents=img,\n", + ")\n", "\n", "# convert the image to Base64 for embedding in HTML\n", "buffered = io.BytesIO()\n", @@ -364,7 +365,10 @@ "#@title Ask Gemini about your image\n", "\n", "img = PIL.Image.open(list(uploaded.keys())[0])\n", - "response = model.generate_content(img)\n", + "response = client.models.generate_content(\n", + " model='gemini-2.5-flash-lite',\n", + " contents=img,\n", + ")\n", "\n", "# convert the image to Base64 for embedding in HTML\n", "buffered = io.BytesIO()\n", diff --git a/notebooks/Gemini_and_Stable_Diffusion.ipynb b/notebooks/Gemini_and_Stable_Diffusion.ipynb index 16ad5712..90b895b5 100644 --- a/notebooks/Gemini_and_Stable_Diffusion.ipynb +++ b/notebooks/Gemini_and_Stable_Diffusion.ipynb @@ -400,26 +400,24 @@ "\n", "#Access your Gemini API key\n", "\n", - "import google.generativeai as genai\n", + "from google import genai\n", "from google.colab import userdata\n", "\n", "gemini_api_secret_name = 'GOOGLE_API_KEY' # @param {type: \"string\"}\n", "\n", "try:\n", - " GOOGLE_API_KEY=userdata.get(gemini_api_secret_name)\n", - " genai.configure(api_key=GOOGLE_API_KEY)\n", + " GOOGLE_API_KEY = userdata.get(gemini_api_secret_name)\n", + " client = genai.Client(api_key=GOOGLE_API_KEY)\n", "except userdata.SecretNotFoundError as e:\n", - " print(f'''Secret not found\\n\\nThis expects you to create a secret named {gemini_api_secret_name} in Colab\\n\\nVisit https://makersuite.google.com/app/apikey to create an API key\\n\\nStore that in the secrets section on the left side of the notebook (key icon)\\n\\nName the secret {gemini_api_secret_name}''')\n", + " print(f'''Secret not found\\n\\nThis expects you to create a secret named {gemini_api_secret_name} in Colab\\n\\nVisit https://aistudio.google.com/app/apikey to create an API key\\n\\nStore that in the secrets section on the left side of the notebook (key icon)\\n\\nName the secret {gemini_api_secret_name}''')\n", " raise e\n", "except userdata.NotebookAccessError as e:\n", " print(f'''You need to grant this notebook access to the {gemini_api_secret_name} secret in order for the notebook to access Gemini on your behalf.''')\n", " raise e\n", "except Exception as e:\n", " # unknown error\n", - " print(f\"There was an unknown error. Ensure you have a secret {gemini_api_secret_name} stored in Colab and it's a valid key from https://makersuite.google.com/app/apikey\")\n", - " raise e\n", - "\n", - "model = genai.GenerativeModel('gemini-pro')" + " print(f\"There was an unknown error. Ensure you have a secret {gemini_api_secret_name} stored in Colab and it's a valid key from https://aistudio.google.com/app/apikey\")\n", + " raise e" ], "metadata": { "id": "yFv1abRcv2P2", @@ -459,12 +457,13 @@ "source": [ "#@title Use Gemini to create a text prompt with to feed to Stable Diffusion\n", "\n", - "model = genai.GenerativeModel('gemini-pro')\n", - "\n", "text = 'Draw a kitty cat typing on a computer' # @param {type:\"string\"}\n", "\n", "prompt = \"You are creating a prompt for Stable Diffusion to generate an image. Please generate a text prompt for %s. Only respond with the prompt itself, but embellish it as needed but keep it under 80 tokens.\" % text\n", - "response = model.generate_content(prompt)\n", + "response = client.models.generate_content(\n", + " model='gemini-2.5-flash-lite',\n", + " contents=prompt,\n", + ")\n", "response.text" ], "metadata": { diff --git a/notebooks/Generate_images_with_Gemini_and_Vertex.ipynb b/notebooks/Generate_images_with_Gemini_and_Vertex.ipynb index 6f027e00..21326f05 100644 --- a/notebooks/Generate_images_with_Gemini_and_Vertex.ipynb +++ b/notebooks/Generate_images_with_Gemini_and_Vertex.ipynb @@ -68,26 +68,24 @@ "\n", "#Access your Gemini API key\n", "\n", - "import google.generativeai as genai\n", + "from google import genai\n", "from google.colab import userdata\n", "\n", "gemini_api_secret_name = 'GOOGLE_API_KEY' # @param {type: \"string\"}\n", "\n", "try:\n", - " GOOGLE_API_KEY=userdata.get(gemini_api_secret_name)\n", - " genai.configure(api_key=GOOGLE_API_KEY)\n", + " GOOGLE_API_KEY = userdata.get(gemini_api_secret_name)\n", + " client = genai.Client(api_key=GOOGLE_API_KEY)\n", "except userdata.SecretNotFoundError as e:\n", - " print(f'Secret not found\\n\\nThis expects you to create a secret named {gemini_api_secret_name} in Colab\\n\\nVisit https://makersuite.google.com/app/apikey to create an API key\\n\\nStore that in the secrets section on the left side of the notebook (key icon)\\n\\nName the secret {gemini_api_secret_name}')\n", + " print(f'Secret not found\\n\\nThis expects you to create a secret named {gemini_api_secret_name} in Colab\\n\\nVisit https://aistudio.google.com/app/apikey to create an API key\\n\\nStore that in the secrets section on the left side of the notebook (key icon)\\n\\nName the secret {gemini_api_secret_name}')\n", " raise e\n", "except userdata.NotebookAccessError as e:\n", " print(f'You need to grant this notebook access to the {gemini_api_secret_name} secret in order for the notebook to access Gemini on your behalf.')\n", " raise e\n", "except Exception as e:\n", " # unknown error\n", - " print(f\"There was an unknown error. Ensure you have a secret {gemini_api_secret_name} stored in Colab and it's a valid key from https://makersuite.google.com/app/apikey\")\n", - " raise e\n", - "\n", - "model = genai.GenerativeModel('gemini-pro')" + " print(f\"There was an unknown error. Ensure you have a secret {gemini_api_secret_name} stored in Colab and it's a valid key from https://aistudio.google.com/app/apikey\")\n", + " raise e" ], "metadata": { "id": "yFv1abRcv2P2", @@ -103,9 +101,6 @@ "\n", "item_selling = 'lemonade' #@param {type: \"string\"}\n", "\n", - "model = genai.GenerativeModel('gemini-pro')\n", - "chat = model.start_chat(history=[])\n", - "\n", "prompttext = f\"\"\"\n", " I'm selling {item_selling} online, and I need to generate an image of it.\n", " I need the image to be compelling and interesting to convince people to buy.\n", @@ -113,7 +108,10 @@ " Respond with only the prompt, no other text. Be as verbose as possible.\n", " \"\"\"\n", "\n", - "response = chat.send_message(prompttext)\n", + "response = client.models.generate_content(\n", + " model='gemini-2.5-flash-lite',\n", + " contents=prompttext,\n", + ")\n", "\n", "response.text" ], diff --git a/notebooks/Last_minute_Holiday_shopping_with_Gemini.ipynb b/notebooks/Last_minute_Holiday_shopping_with_Gemini.ipynb index 9e7e6f01..3bab1cc7 100644 --- a/notebooks/Last_minute_Holiday_shopping_with_Gemini.ipynb +++ b/notebooks/Last_minute_Holiday_shopping_with_Gemini.ipynb @@ -32,26 +32,24 @@ "\n", "#Access your Gemini API key\n", "\n", - "import google.generativeai as genai\n", + "from google import genai\n", "from google.colab import userdata\n", "\n", "gemini_api_secret_name = 'GOOGLE_API_KEY' # @param {type: \"string\"}\n", "\n", "try:\n", - " GOOGLE_API_KEY=userdata.get(gemini_api_secret_name)\n", - " genai.configure(api_key=GOOGLE_API_KEY)\n", + " GOOGLE_API_KEY = userdata.get(gemini_api_secret_name)\n", + " client = genai.Client(api_key=GOOGLE_API_KEY)\n", "except userdata.SecretNotFoundError as e:\n", - " print(f'Secret not found\\n\\nThis expects you to create a secret named {gemini_api_secret_name} in Colab\\n\\nVisit https://makersuite.google.com/app/apikey to create an API key\\n\\nStore that in the secrets section on the left side of the notebook (key icon)\\n\\nName the secret {gemini_api_secret_name}')\n", + " print(f'Secret not found\\n\\nThis expects you to create a secret named {gemini_api_secret_name} in Colab\\n\\nVisit https://aistudio.google.com/app/apikey to create an API key\\n\\nStore that in the secrets section on the left side of the notebook (key icon)\\n\\nName the secret {gemini_api_secret_name}')\n", " raise e\n", "except userdata.NotebookAccessError as e:\n", " print(f'You need to grant this notebook access to the {gemini_api_secret_name} secret in order for the notebook to access Gemini on your behalf.')\n", " raise e\n", "except Exception as e:\n", " # unknown error\n", - " print(f\"There was an unknown error. Ensure you have a secret {gemini_api_secret_name} stored in Colab and it's a valid key from https://makersuite.google.com/app/apikey\")\n", - " raise e\n", - "\n", - "model = genai.GenerativeModel('gemini-pro')" + " print(f\"There was an unknown error. Ensure you have a secret {gemini_api_secret_name} stored in Colab and it's a valid key from https://aistudio.google.com/app/apikey\")\n", + " raise e" ], "metadata": { "id": "yFv1abRcv2P2", @@ -82,7 +80,10 @@ "prompt += \"Could you suggest three good holiday gift ideas that I could buy them?\"\n", "prompt += \"Try to be as thoughtful and delightful as possible.\"\n", "\n", - "response = model.generate_content(prompt)\n", + "response = client.models.generate_content(\n", + " model='gemini-2.5-flash-lite',\n", + " contents=prompt,\n", + ")\n", "\n", "#print out the results nicely\n", "from IPython.display import Markdown\n", @@ -90,7 +91,7 @@ "\n", "def to_markdown(text):\n", " text = text.replace('•', '*')\n", - " text = text.replace('$','\\$')\n", + " text = text.replace('$', r'\\$')\n", " return Markdown(textwrap.indent(text, '> ', predicate=lambda _: True))\n", "\n", "to_markdown(response.text)" diff --git a/notebooks/Learning_with_Gemini_and_ChatGPT.ipynb b/notebooks/Learning_with_Gemini_and_ChatGPT.ipynb index ccb3a44f..689e4d79 100644 --- a/notebooks/Learning_with_Gemini_and_ChatGPT.ipynb +++ b/notebooks/Learning_with_Gemini_and_ChatGPT.ipynb @@ -34,26 +34,24 @@ "\n", "# access your Gemini API key\n", "\n", - "import google.generativeai as genai\n", + "from google import genai\n", "from google.colab import userdata\n", "\n", "gemini_api_secret_name = 'GOOGLE_API_KEY' # @param {type: \"string\"}\n", "\n", "try:\n", - " GOOGLE_API_KEY=userdata.get(gemini_api_secret_name)\n", - " genai.configure(api_key=GOOGLE_API_KEY)\n", + " GOOGLE_API_KEY = userdata.get(gemini_api_secret_name)\n", + " gemini_client = genai.Client(api_key=GOOGLE_API_KEY)\n", "except userdata.SecretNotFoundError as e:\n", - " print(f'''Secret not found\\n\\nThis expects you to create a secret named {gemini_api_secret_name} in Colab\\n\\nVisit https://makersuite.google.com/app/apikey to create an API key\\n\\nStore that in the secrets section on the left side of the notebook (key icon)\\n\\nName the secret {gemini_api_secret_name}''')\n", + " print(f'''Secret not found\\n\\nThis expects you to create a secret named {gemini_api_secret_name} in Colab\\n\\nVisit https://aistudio.google.com/app/apikey to create an API key\\n\\nStore that in the secrets section on the left side of the notebook (key icon)\\n\\nName the secret {gemini_api_secret_name}''')\n", " raise e\n", "except userdata.NotebookAccessError as e:\n", " print(f'''You need to grant this notebook access to the {gemini_api_secret_name} secret in order for the notebook to access Gemini on your behalf.''')\n", " raise e\n", "except Exception as e:\n", " # unknown error\n", - " print(f\"There was an unknown error. Ensure you have a secret {gemini_api_secret_name} stored in Colab and it's a valid key from https://makersuite.google.com/app/apikey\")\n", - " raise e\n", - "\n", - "model = genai.GenerativeModel('gemini-pro')" + " print(f\"There was an unknown error. Ensure you have a secret {gemini_api_secret_name} stored in Colab and it's a valid key from https://aistudio.google.com/app/apikey\")\n", + " raise e" ], "metadata": { "cellView": "form", @@ -107,10 +105,10 @@ "text = 'Write a python function that calculates the distance between any two latitudes and longitudes on earth' # @param {type:\"string\"}\n", "\n", "# ask Gemini\n", - "model = genai.GenerativeModel('gemini-pro')\n", - "chat = model.start_chat(history=[])\n", - "\n", - "response = chat.send_message('%s -- Please answer as concisely as you can, avoiding any extra conversation or text' % text)\n", + "response = gemini_client.models.generate_content(\n", + " model='gemini-2.5-flash-lite',\n", + " contents='%s -- Please answer as concisely as you can, avoiding any extra conversation or text' % text,\n", + ")\n", "gemini_response = response.text\n", "\n", "# ask ChatGPT\n", diff --git a/notebooks/Prepare_Christmas_cards_with_Gemini_and_Sheets.ipynb b/notebooks/Prepare_Christmas_cards_with_Gemini_and_Sheets.ipynb index 8b6cb43f..9be4fa7d 100644 --- a/notebooks/Prepare_Christmas_cards_with_Gemini_and_Sheets.ipynb +++ b/notebooks/Prepare_Christmas_cards_with_Gemini_and_Sheets.ipynb @@ -53,26 +53,32 @@ "\n", "#Access your Gemini API key\n", "\n", - "import google.generativeai as genai\n", + "from google import genai\n", "from google.colab import userdata\n", "\n", "gemini_api_secret_name = 'GOOGLE_API_KEY' # @param {type: \"string\"}\n", "\n", "try:\n", - " GOOGLE_API_KEY=userdata.get(gemini_api_secret_name)\n", - " genai.configure(api_key=GOOGLE_API_KEY)\n", + " GOOGLE_API_KEY = userdata.get(gemini_api_secret_name)\n", + " client = genai.Client(api_key=GOOGLE_API_KEY)\n", "except userdata.SecretNotFoundError as e:\n", - " print(f'''Secret not found\n\nThis expects you to create a secret named {gemini_api_secret_name} in Colab\n\nVisit https://makersuite.google.com/app/apikey to create an API key\n\nStore that in the secrets section on the left side of the notebook (key icon)\n\nName the secret {gemini_api_secret_name}''')\n", + " print(f'''Secret not found\n", + "\n", + "This expects you to create a secret named {gemini_api_secret_name} in Colab\n", + "\n", + "Visit https://aistudio.google.com/app/apikey to create an API key\n", + "\n", + "Store that in the secrets section on the left side of the notebook (key icon)\n", + "\n", + "Name the secret {gemini_api_secret_name}''')\n", " raise e\n", "except userdata.NotebookAccessError as e:\n", " print(f'''You need to grant this notebook access to the {gemini_api_secret_name} secret in order for the notebook to access Gemini on your behalf.''')\n", " raise e\n", "except Exception as e:\n", " # unknown error\n", - " print(f\"There was an unknown error. Ensure you have a secret {gemini_api_secret_name} stored in Colab and it's a valid key from https://makersuite.google.com/app/apikey\")\n", - " raise e\n", - "\n", - "model = genai.GenerativeModel('gemini-pro')" + " print(f\"There was an unknown error. Ensure you have a secret {gemini_api_secret_name} stored in Colab and it's a valid key from https://aistudio.google.com/app/apikey\")\n", + " raise e" ], "metadata": { "id": "yFv1abRcv2P2", @@ -137,7 +143,10 @@ "for row in rows[1:]:\n", " prompt = \"I'm writing Christmas cards. Can you suggest a short meaningful paragraph of text for a Christmas note? I'm writing a note to %s my %s and I'd like the style to be %s.\" % (row[0], row[1], row[2])\n", "\n", - " response = model.generate_content(prompt)\n", + " response = client.models.generate_content(\n", + " model='gemini-2.5-flash-lite',\n", + " contents=prompt,\n", + " )\n", " row.append(response.text)\n", "\n", "#print out the results nicely\n", diff --git a/notebooks/Sell_lemonade_with_Gemini_and_Sheets.ipynb b/notebooks/Sell_lemonade_with_Gemini_and_Sheets.ipynb index fa349969..b0fae444 100644 --- a/notebooks/Sell_lemonade_with_Gemini_and_Sheets.ipynb +++ b/notebooks/Sell_lemonade_with_Gemini_and_Sheets.ipynb @@ -53,26 +53,24 @@ "\n", "#Access your Gemini API key\n", "\n", - "import google.generativeai as genai\n", + "from google import genai\n", "from google.colab import userdata\n", "\n", "gemini_api_secret_name = 'GOOGLE_API_KEY' # @param {type: \"string\"}\n", "\n", "try:\n", - " GOOGLE_API_KEY=userdata.get(gemini_api_secret_name)\n", - " genai.configure(api_key=GOOGLE_API_KEY)\n", + " GOOGLE_API_KEY = userdata.get(gemini_api_secret_name)\n", + " client = genai.Client(api_key=GOOGLE_API_KEY)\n", "except userdata.SecretNotFoundError as e:\n", - " print(f'Secret not found\\n\\nThis expects you to create a secret named {gemini_api_secret_name} in Colab\\n\\nVisit https://makersuite.google.com/app/apikey to create an API key\\n\\nStore that in the secrets section on the left side of the notebook (key icon)\\n\\nName the secret {gemini_api_secret_name}')\n", + " print(f'Secret not found\\n\\nThis expects you to create a secret named {gemini_api_secret_name} in Colab\\n\\nVisit https://aistudio.google.com/app/apikey to create an API key\\n\\nStore that in the secrets section on the left side of the notebook (key icon)\\n\\nName the secret {gemini_api_secret_name}')\n", " raise e\n", "except userdata.NotebookAccessError as e:\n", " print(f'You need to grant this notebook access to the {gemini_api_secret_name} secret in order for the notebook to access Gemini on your behalf.')\n", " raise e\n", "except Exception as e:\n", " # unknown error\n", - " print(f\"There was an unknown error. Ensure you have a secret {gemini_api_secret_name} stored in Colab and it's a valid key from https://makersuite.google.com/app/apikey\")\n", - " raise e\n", - "\n", - "model = genai.GenerativeModel('gemini-pro')" + " print(f\"There was an unknown error. Ensure you have a secret {gemini_api_secret_name} stored in Colab and it's a valid key from https://aistudio.google.com/app/apikey\")\n", + " raise e" ], "metadata": { "id": "yFv1abRcv2P2", @@ -136,7 +134,10 @@ "for row in rows[1:]:\n", " prompt = \"I'm selling %s. Can you suggest a short paragraph of text for how I might best pitch them on buying? I'm writing a note to %s my %s. Here's some context about them: %s.\" % (sales_topic, row[0], row[1], row[2])\n", "\n", - " response = model.generate_content(prompt)\n", + " response = client.models.generate_content(\n", + " model='gemini-2.5-flash-lite',\n", + " contents=prompt,\n", + " )\n", " row.append(response.text)\n", "\n", "#print out the results nicely\n", diff --git a/notebooks/Talk_to_Gemini_with_Google's_Speech_to_Text_API.ipynb b/notebooks/Talk_to_Gemini_with_Google's_Speech_to_Text_API.ipynb index 592d9780..a4d967fa 100644 --- a/notebooks/Talk_to_Gemini_with_Google's_Speech_to_Text_API.ipynb +++ b/notebooks/Talk_to_Gemini_with_Google's_Speech_to_Text_API.ipynb @@ -329,26 +329,24 @@ "\n", "#Access your Gemini API key\n", "\n", - "import google.generativeai as genai\n", + "from google import genai\n", "from google.colab import userdata\n", "\n", "gemini_api_secret_name = 'GOOGLE_API_KEY' # @param {type: \"string\"}\n", "\n", "try:\n", - " GOOGLE_API_KEY=userdata.get(gemini_api_secret_name)\n", - " genai.configure(api_key=GOOGLE_API_KEY)\n", + " GOOGLE_API_KEY = userdata.get(gemini_api_secret_name)\n", + " client = genai.Client(api_key=GOOGLE_API_KEY)\n", "except userdata.SecretNotFoundError as e:\n", - " print(f'Secret not found\\n\\nThis expects you to create a secret named {gemini_api_secret_name} in Colab\\n\\nVisit https://makersuite.google.com/app/apikey to create an API key\\n\\nStore that in the secrets section on the left side of the notebook (key icon)\\n\\nName the secret {gemini_api_secret_name}')\n", + " print(f'Secret not found\\n\\nThis expects you to create a secret named {gemini_api_secret_name} in Colab\\n\\nVisit https://aistudio.google.com/app/apikey to create an API key\\n\\nStore that in the secrets section on the left side of the notebook (key icon)\\n\\nName the secret {gemini_api_secret_name}')\n", " raise e\n", "except userdata.NotebookAccessError as e:\n", " print(f'You need to grant this notebook access to the {gemini_api_secret_name} secret in order for the notebook to access Gemini on your behalf.')\n", " raise e\n", "except Exception as e:\n", " # unknown error\n", - " print(f\"There was an unknown error. Ensure you have a secret {gemini_api_secret_name} stored in Colab and it's a valid key from https://makersuite.google.com/app/apikey\")\n", - " raise e\n", - "\n", - "model = genai.GenerativeModel('gemini-pro')" + " print(f\"There was an unknown error. Ensure you have a secret {gemini_api_secret_name} stored in Colab and it's a valid key from https://aistudio.google.com/app/apikey\")\n", + " raise e" ], "metadata": { "id": "yFv1abRcv2P2", @@ -526,7 +524,10 @@ "processing_results = speech_to_text(config, audio)\n", "audio_text = processing_results.results[0].alternatives[0].transcript\n", "\n", - "response = model.generate_content(audio_text)\n", + "response = client.models.generate_content(\n", + " model='gemini-2.5-flash-lite',\n", + " contents=audio_text,\n", + ")\n", "\n", "to_markdown(f'**You**: {audio_text}\\n\\n**Gemini**:\\n{response.text}')" ],