Skip to content

Commit

Permalink
Generate image
Browse files Browse the repository at this point in the history
  • Loading branch information
jcrodriguez1989 committed Aug 8, 2024
1 parent 179b06b commit 886dc03
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
1 change: 0 additions & 1 deletion R/build_prompt_content.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,3 @@ build_prompt_content <- function(question, images) {
list(type = "image_url", image_url = list(url = image_url))
}))
}

27 changes: 27 additions & 0 deletions R/generate_image.R
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)
}
22 changes: 22 additions & 0 deletions man/generate_image.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 886dc03

Please sign in to comment.