diff --git a/NAMESPACE b/NAMESPACE index 731d529..3d16cf8 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -8,6 +8,7 @@ export(create_variable_name) export(document_code) export(explain_code) export(find_issues_in_code) +export(generate_image) export(list_models) export(optimize_code) export(refactor_code) @@ -37,5 +38,6 @@ importFrom(shiny,stopApp) importFrom(shiny,textAreaInput) importFrom(shiny,updateTextAreaInput) importFrom(shiny,wellPanel) +importFrom(utils,download.file) importFrom(utils,getFromNamespace) importFrom(xfun,base64_encode) diff --git a/R/build_prompt_content.R b/R/build_prompt_content.R index 00b9522..1f70836 100644 --- a/R/build_prompt_content.R +++ b/R/build_prompt_content.R @@ -19,4 +19,3 @@ build_prompt_content <- function(question, images) { list(type = "image_url", image_url = list(url = image_url)) })) } - diff --git a/R/generate_image.R b/R/generate_image.R new file mode 100644 index 0000000..85fd2f4 --- /dev/null +++ b/R/generate_image.R @@ -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) +} diff --git a/man/generate_image.Rd b/man/generate_image.Rd new file mode 100644 index 0000000..ebe5c7f --- /dev/null +++ b/man/generate_image.Rd @@ -0,0 +1,22 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/generate_image.R +\name{generate_image} +\alias{generate_image} +\title{Generate an Image With DALL-E 3} +\usage{ +generate_image( + prompt, + out_file = tempfile(fileext = ".png"), + openai_api_key = Sys.getenv("OPENAI_API_KEY") +) +} +\arguments{ +\item{prompt}{The prompt for image generation.} + +\item{out_file}{The path where to save the generated image.} + +\item{openai_api_key}{OpenAI's API key.} +} +\description{ +Generate an Image With DALL-E 3 +}