-
Notifications
You must be signed in to change notification settings - Fork 107
/
Get-DalleImage.ps1
51 lines (42 loc) · 1.33 KB
/
Get-DalleImage.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
function Get-DalleImage {
<#
.SYNOPSIS
Get a DALL-E image from the OpenAI API
.DESCRIPTION
Given a description, the model will return an image
.PARAMETER Description
The description to generate an image for
.PARAMETER Size
The size of the image to generate. Defaults to 256
.PARAMETER Raw
If set, the raw response will be returned. Otherwise, the image will be saved to a temporary file and the path to that file will be returned
.EXAMPLE
Get-DalleImage -Description "A cat sitting on a table"
#>
[CmdletBinding()]
param(
[Parameter(Mandatory)]
$Description,
[ValidateSet('256', '512', '1024')]
$Size = 256,
[Switch]$Raw
)
$targetSize = switch ($Size) {
256 { '256x256' }
512 { '512x512' }
1024 { '1024x1024' }
}
$body = [ordered]@{
prompt = $Description
size = $targetSize
} | ConvertTo-Json
$result = Invoke-OpenAIAPI -Uri (Get-OpenAIImagesGenerationsURI) -Body $body -Method Post
if ($Raw) {
return $result
}
else {
$DestinationPath = [IO.Path]::GetTempFileName() -replace ".tmp", ".png"
Invoke-RestMethod $result.data.url -OutFile $DestinationPath
$DestinationPath
}
}