Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions notebooks/Classify_an_image_using_Gemini.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
21 changes: 10 additions & 11 deletions notebooks/Gemini_and_Stable_Diffusion.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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": {
Expand Down
22 changes: 10 additions & 12 deletions notebooks/Generate_images_with_Gemini_and_Vertex.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -103,17 +101,17 @@
"\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",
" Can you create a prompt I can use to generate an image of {item_selling} with Vertex?\n",
" 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"
],
Expand Down
21 changes: 11 additions & 10 deletions notebooks/Last_minute_Holiday_shopping_with_Gemini.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -82,15 +80,18 @@
"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",
"import textwrap\n",
"\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)"
Expand Down
22 changes: 10 additions & 12 deletions notebooks/Learning_with_Gemini_and_ChatGPT.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
27 changes: 18 additions & 9 deletions notebooks/Prepare_Christmas_cards_with_Gemini_and_Sheets.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
Loading
Loading