-
-
Save codeas/331fcc75e0a23817168b2c87534a1aa1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const PROJECT_ID = "INSERT-HERE; | |
const REGION = "us-central1"; // https://cloud.google.com/vertex-ai/docs/generative-ai/model-reference/gemini#gemini-pro-vision | |
const GOOGLE_SLIDES_ID = "INSERT-HERE-GOOGLE-SLIDES-ID; | |
const CONFIG = { | |
newImageUrl: "https://upload.wikimedia.org/wikipedia/commons/thumb/7/7e/Gmail_icon_%282020%29.svg/2560px-Gmail_icon_%282020%29.svg.png", | |
oldImageUrl: "https://cdn-icons-png.flaticon.com/512/281/281769.png" | |
} | |
function getSlides() { | |
let newImageBlob = UrlFetchApp.fetch(CONFIG.newImageUrl).getBlob(); | |
let slides = SlidesApp.openById(GOOGLE_SLIDES_ID).getSlides(); | |
slides.forEach( slide => { | |
slide.getImages().forEach( image => { | |
console.log(image.getContentUrl()) | |
let blob = image.getBlob(); | |
let result = analyseImage(blob, CONFIG) | |
console.log(result) | |
if (result.toLowerCase().trim().indexOf("old") > -1) { | |
image.replace(newImageBlob) | |
} | |
}) | |
}) | |
} | |
function analyseImage(blob, config) { | |
const endpoint = `https://{REGION}-aiplatform.googleapis.com/v1/projects/{PROJECT_ID}/locations/{REGION}/publishers/google/models/gemini-pro-vision:generateContent` | |
let url = endpoint.replace("{PROJECT_ID}", PROJECT_ID).replace(/\{REGION\}/g, REGION); | |
let data = { | |
"contents": [ | |
{ | |
"role": "USER", | |
"parts": [ | |
{ "text": "You are helpful bot, which is able to recognize into categories: new, old or other"}, | |
{ "text": "If image is not either in category new or old returns category other"}, | |
{ "text": "Examples: "}, | |
{ "text": "Identify category for this image: "}, | |
{ "inlineData": loadFileUrl_(config.newImageUrl) }, | |
{ "text": "new"}, | |
{ "text": "\n\n"}, | |
{ "text": "Identify category for this image: "}, | |
{ "inlineData": loadFileUrl_(config.oldImageUrl) }, | |
{ "text": "old"}, | |
{ "text": "\n\n"}, | |
{ "text": "Identify category for this image: "}, | |
{ "inlineData": getInlineData_(blob) } | |
] | |
} | |
], | |
"generationConfig": { | |
"temperature": 0, | |
"topK": 1 | |
} | |
}; | |
let options = { | |
method:"POST", | |
contentType: "application/json", | |
payload: JSON.stringify(data), | |
muteHttpExceptions: true, | |
headers : { "Authorization" : "Bearer " + ScriptApp.getOAuthToken()} | |
} | |
let response = UrlFetchApp.fetch(url,options).getContentText(); | |
let result = JSON.parse(response) | |
let text = (result["candidates"][0]["content"]["parts"][0]["text"]) | |
return text.trim(); | |
} | |
function getInlineData_(blob) { | |
let bytes = blob.getBytes(); | |
let data = Utilities.base64Encode(bytes); | |
let result = { "mimeType": blob.getContentType(), "data": data }; | |
return result; | |
} | |
function loadFileUrl_(url) { | |
let blob = UrlFetchApp.fetch(url).getBlob(); | |
let result = getInlineData_(blob) | |
return result; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment