-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
179b06b
commit 886dc03
Showing
4 changed files
with
51 additions
and
1 deletion.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#' Generate an Image With DALL-E 3 | ||
#' | ||
#' @param prompt The prompt for image generation. | ||
#' @param out_file The path where to save the generated image. | ||
#' @param openai_api_key OpenAI's API key. | ||
#' | ||
#' @importFrom httr add_headers content content_type_json POST stop_for_status | ||
#' @importFrom jsonlite fromJSON toJSON | ||
#' @importFrom utils download.file | ||
#' | ||
#' @export | ||
#' | ||
generate_image <- function(prompt, out_file = tempfile(fileext = ".png"), | ||
openai_api_key = Sys.getenv("OPENAI_API_KEY")) { | ||
post_res <- POST( | ||
paste0(api_url, "/images/generations"), | ||
add_headers("Authorization" = paste("Bearer", openai_api_key)), | ||
content_type_json(), | ||
body = toJSON( | ||
list(model = "dall-e-3", prompt = prompt, n = 1, size = "1024x1024"), | ||
auto_unbox = TRUE | ||
) | ||
) | ||
stop_for_status(post_res) | ||
download.file(fromJSON(content(post_res, as = "text", encoding = "UTF-8"))$data$url, out_file) | ||
return(out_file) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.