Upload an image or file to GPT API using PowerShell and ask questions about it then Tag or Classify your file

Posted by SysAdmin Tools on

If you want to utilize the feature in Chat GPT4 that allows you to upload a file and ask questions about it but you don't have Chat GPT Plus, you can now do this via the API. 



This has quite a few use cases and one of these are to classify or tag your own images or files. You can always just use it to merely ask questions about files and get a response. 

This script allows you to upload your files to Chat GPT API.
The script then asks GPT to describe the files for tagging.

You can change this question to anything you want and utilize the $response output for your own purposes.
You do not need to use the rest of the script if you do not want to tag your files. 

Follow the instructions in this article to set up your account and get the API key. 
https://sysadmin-tools.com/blogs/news/simple-powershell-script-to-ask-questions-and-follow-up-questions-to-chat-gpt-4-api
This article also explains how to simply ask GPT API a normal question and get a response without a file upload. 

########################

# OpenAI API Key
$apiKey = "PasteYourAPIKeyHere"

#IMPORTANT - BEFORE RUNNING
#Loading TagLibSharp
#Download https://globalcdn.nuget.org/packages/taglibsharp.2.3.0.nupkg
#Unzip taglibsharp.2.3.0.nupkg and copy \taglibsharp.2.3.0\lib\netstandard2.0\TagLibSharp.dll to script directory

#Load
[System.Reflection.Assembly]::LoadFrom((Resolve-Path "TagLibSharp.dll"))

# Prepare the headers
$headers = @{
"Content-Type" = "application/json"
"Authorization" = "Bearer $apiKey"
}

# API endpoint
$apiEndpoint = "https://api.openai.com/v1/chat/completions"

# Function to encode the image in base64
function Encode-Image($imagePath) {
$imageBytes = [System.IO.File]::ReadAllBytes($imagePath)
$base64Image = [Convert]::ToBase64String($imageBytes)
return $base64Image
}

#change to your images directory and make sure only the images you want to process are in there.
$images = Get-ChildItem -Path "C:\Images" | where {$_.Name -ne "TagLibSharp.dll" }

foreach ($image in $images)
{
# Path to your image
$imagePath = $image.FullName

# Getting the base64 string
$base64Image = Encode-Image -imagePath $imagePath
$contenturl = "data:image/jpeg;base64,$base64Image"

# Prepare the payload
$payload = @{
model = "gpt-4-vision-preview"
messages = @(
@{
role = "user"
content = @(
@{
type = "text"
text = "Describe this image for tagging with single words seperated by semicolons?"
},
@{
type = "image_url"
image_url = @{
url = $contenturl
}
}
)
}
)
max_tokens = 300
} | ConvertTo-Json -Depth 5

# Send the request
$response = Invoke-RestMethod -Uri $apiEndpoint -Method Post -Headers $headers -Body $payload

# Output the response
$response | ConvertTo-Json -Depth 10
#$response.choices[0].message.content

$tags = $response.choices[0].message.content.Replace(" ","").Split(";")

#Load file
$photo = [TagLib.File]::Create((Resolve-Path $imagePath))
$currentTags = $photo.ImageTag.Keywords
$newTags = $photo.ImageTag.Keywords
if ($newTags.Count -eq 0)
{
$newTags = @()
}

foreach ($tag in $tags)
{
if (!$currentTags.Contains($tag)) {$newTags += $tag ; $tag}
}

$photo.ImageTag.Keywords = $newTags
$photo.Save()

}
###################





Copy the script and edit the API key and the C:\Images directory to your own details.
Remove the $tags to $photo.Save() section if you do not need tagging. 




Share this post



← Older Post Newer Post →


Leave a comment

Please note, comments must be approved before they are published.