Sunday, March 31, 2024

Smart replacing images in Google Slides with Gemini Pro API and Vertex AI

Surely, you have also experienced having a presentation in which you needed to replace old content with new. Replacing text is very simple because you just need to use the Replace function, and you can either do it in the Google Slides user interface.

The problem arises when you need to replace one image with another, for example, if your corporate logo is updated to a new graphic design or if one of your favorite cloud services updates its icons (Gmail, blink blink ;-) It's still somewhat bearable with one presentation, but what do you do when, like me, you have thousands of Google Slides files on your Google Drive?


Fortunately, there are large language models and, specifically, multimodal models that allow input prompts to include images in addition to text. Specifically, with Gemini Pro, you can have up to 16 such images as input. And then the old saying applies that one picture is worth a thousand words :)

I used Gemini Pro for exactly this use case in the Vertex AI service with integration into Google Apps Script, which could connect to my presentation, go through all the slides, and if there was an image containing the old logo, it replaced it with the new logo. I will show you how you can replicate such a procedure yourself, and all you need for it is just a Google Cloud account."


1. Create a new Google Apps Script project https://script.new/


2) Go to Project settings -> and click the checkbox Show "appsscript.json" manifest file in editor


3) Copy the manifest.json below


4) Prepare a Google Cloud project and if you don't have one, create one here: https://console.cloud.google.com/projectcreate 


Then enable the Vertex AI API https://console.cloud.google.com/marketplace/product/google/aiplatform.googleapis.com



The Gemini Pro Vision API takes as input data that consists of parts (parts as an array), where each item can be either text or binary data (either embedded or embedded via a URL link)

https://cloud.google.com/vertex-ai/generative-ai/docs/model-reference/gemini


We compose our prompt as you might be used to, only with the difference that we also load two images and tell it which is the old and which is the new logo. We will use the a-few-shot learning technique for examples.




Finally, all that's left is to create a function that can load all the slides in a presentation, load all the images in each slide, and then send each image to the Gemini Pro API to see if it's an old or new image. If it's an old image, then replace it directly in the presentation with the new image.
And that's all. Now you just need to run the getSlides() function, which will replace all the old Gmail logos with the new ones. Of course, the script can be modified to go through all your files. Or better yet, to go through all the files in the company through domain-wide delegation

Google Cloud credits are provided for this project
#GeminiSprint

Tuesday, August 23, 2022

List all GCP regions with Google (unofficial) API endpoint

I have several scenarios where I needed to list all GCP regions. (e.g. Cloud Billing API https://cloud.google.com/billing/docs/reference/rest).




I was surprised that there is no API for that. 

When you try to search "list of GCP regions" you will end with one of the top results to documentation e.g. https://cloud.google.com/compute/docs/regions-zones or https://cloud.google.com/about/locations. It is not suitable for programmatic access.

I have found recently an endpoint (not API!) with the list of IP ranges for each GCP regions

https://www.gstatic.com/ipranges/cloud.json


That was the last piece of the puzzle to create my desired function.

Here is a snippet for Google Apps Script:


Friday, February 25, 2022

Get filtered rows in Google Sheets with Google Apps Script

Google Sheets allows you to filter data in grid.

Sometimes you need to filter and use data with API.
https://developers.google.com/sheets/api

 Google Sheets API has endpoint with array rowMetadata.

You can iterate over the all rows and check if property hiddenByFilter is setup.



Here is a snippet how to get filtered rows with Google Apps Script


Monday, December 7, 2020

16 secrets tips, tricks and features for new Google Apps Script Editor (v2020)

Google Apps Script has a new editor, which is better, nicer, and completed ready for future new features.
Today, I would like to introduce you to several dirty & secret tricks, what you can do.
(All shortcuts have been tested on MacOS)

#1 Change order of your files in Apps Script project


#2 When you click save, all project's files are saved


#3 Show / hide not used code in function(s) with small arrow


#4 Exchange two lines with [ALT] + [UP] or [ALT] + [DOWN] 

#5 Copy line below or above with [ALT] + [SHIFT]+ [UP] or [ALT] + [SHIFT] + [DOWN] 



#6 Expand or shrink block selection with [CTRL] + [SHIFT]+[CMD]+ [LEFT] or [CTRL] + [SHIFT]+[CMD]+ [RIGHT] 


#7 Multi-cursor for editing more lines together [ALT] + click to position


#8 Better code refactoring with Rename symbol, which replaces all findings


#9 Peek definition is best quick insight about function definition


#10 Code suggestion for native JavaScript objects (e.g. Date) is available


#11 Always visible panel with execution logs



#12 Different colors console.warn() and console.error()




#13 Hyperlink in logging console leads to code line 

#14 Better debugging to deeper inspect of function callstack and variables (local and global scope)


#15 Change editor into dark-mode with command 

 

#16 Inline documentation with example in code suggestion



If you have your own favourite tip for new Apps Script editor, ping me on Twitter @ivankutil