forked from analyticsinmotion/chatgpt-images-r-shiny
-
Notifications
You must be signed in to change notification settings - Fork 0
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
264c9f9
commit 02e90ec
Showing
46 changed files
with
499 additions
and
22 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ | |
.Rdata | ||
.httr-oauth | ||
.DS_Store | ||
.Renviron |
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 |
---|---|---|
|
@@ -2,21 +2,20 @@ Package: chatgptimages | |
Title: An Amazing Shiny App | ||
Version: 0.0.0.9000 | ||
Authors@R: | ||
person(given = "firstname", | ||
family = "lastname", | ||
role = c("aut", "cre"), | ||
email = "[email protected]") | ||
person("firstname", "lastname", , "[email protected]", role = c("aut", "cre")) | ||
Description: What the package does (one paragraph). | ||
License: What license is it under? | ||
Imports: | ||
config (>= 0.3.1), | ||
golem (>= 0.3.5), | ||
shiny (>= 1.7.4) | ||
Encoding: UTF-8 | ||
LazyData: true | ||
RoxygenNote: 7.2.3 | ||
openai (>= 0.3.0), | ||
shiny (>= 1.7.4), | ||
shinydashboard (>= 0.7.2) | ||
Suggests: | ||
spelling, | ||
testthat (>= 3.0.0) | ||
Config/testthat/edition: 3 | ||
Encoding: UTF-8 | ||
Language: en-US | ||
LazyData: true | ||
RoxygenNote: 7.2.3 |
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
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,12 @@ | ||
#' get_filename | ||
#' | ||
#' @description A fct function | ||
#' | ||
#' @return The return value, if any, from executing the function. | ||
#' | ||
#' @noRd | ||
get_filename <- function(url){ | ||
pattern <- "img-.*?\\.png" | ||
m <- gregexpr(pattern, url) | ||
regmatches(url, m)[[1]] | ||
} |
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,15 @@ | ||
#' get_images | ||
#' | ||
#' @description A fct function | ||
#' | ||
#' @return The return value, if any, from executing the function. | ||
#' | ||
#' @noRd | ||
get_images <- function(){ | ||
images <- list.files(path="inst/app/www", pattern=".png", all.files=FALSE, full.names=TRUE) | ||
lapply(images, | ||
function(x) { | ||
renderImage({ | ||
list(src = x, width = 256, height = 256, alt = x)}, deleteFile = F) | ||
}) | ||
} |
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,14 @@ | ||
#' get_url | ||
#' | ||
#' @description A fct function | ||
#' | ||
#' @return The return value, if any, from executing the function. | ||
#' | ||
#' @noRd | ||
get_url <- function(prompt, size){ | ||
openai::create_image(prompt = prompt, | ||
n = 1, | ||
size = size, | ||
openai_api_key = Sys.getenv('OPENAI_API_KEY') | ||
)[["data"]][1,] | ||
} |
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,19 @@ | ||
#' set_apikey | ||
#' | ||
#' @description A fct function | ||
#' | ||
#' @return The return value, if any, from executing the function. | ||
#' | ||
#' @noRd | ||
set_apikey <- function(apikey){ | ||
api_string <- paste("OPENAI_API_KEY=",apikey, sep = "") | ||
fileConn<-file(".Renviron") | ||
writeLines(api_string, fileConn) | ||
close(fileConn) | ||
# These 4 lines are required to update the environment variable input in settings tab | ||
# Otherwise it will be stuck using the one that was in the .renviron file at startup | ||
path <- gsub("/", "\\\\", golem::get_golem_wd()) | ||
filename <- "\\.Renviron" | ||
full_path <- paste(path,filename, sep = "") | ||
readRenviron(full_path) | ||
} |
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,51 @@ | ||
#' create_image UI Function | ||
#' | ||
#' @description A shiny Module. | ||
#' | ||
#' @param id,input,output,session Internal parameters for {shiny}. | ||
#' | ||
#' @noRd | ||
#' | ||
#' @importFrom shiny NS tagList | ||
mod_create_image_ui <- function(id){ | ||
ns <- NS(id) | ||
tagList( | ||
|
||
## START - MY CODE | ||
textAreaInput(ns("createimage"), label = "Enter the description to draw", rows = 3), | ||
selectInput(ns("imagesizes"), "Size of Images", choices = c("256x256", "512x512" , "1024x1024")), | ||
actionButton(ns("buttonCreateImage"), label = "Create Image", icon = icon("pencil")), | ||
br(),br(),br(), | ||
imageOutput(ns("openaiResponse")) | ||
## END - MY CODE | ||
|
||
) | ||
} | ||
|
||
#' create_image Server Functions | ||
#' | ||
#' @noRd | ||
mod_create_image_server <- function(id){ | ||
moduleServer( id, function(input, output, session){ | ||
ns <- session$ns | ||
|
||
## START - MY CODE | ||
output$openaiResponse <- renderImage({ | ||
url <- get_url(input$createimage, input$imagesizes) | ||
filename <- paste("inst/app/www",get_filename(url), sep = "/") | ||
download.file(url,filename, mode = 'wb') | ||
img_dimension <- as.numeric(sapply(strsplit(input$imagesizes, "x"), getElement, 1)) | ||
list(src = filename, contentType = 'image/png',width = img_dimension, height = img_dimension, | ||
alt = input$createimage) | ||
}, deleteFile = FALSE) |> | ||
bindEvent(input$buttonCreateImage) | ||
## END - MY CODE | ||
|
||
}) | ||
} | ||
|
||
## To be copied in the UI | ||
# mod_create_image_ui("create_image_1") | ||
|
||
## To be copied in the server | ||
# mod_create_image_server("create_image_1") |
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,52 @@ | ||
#' input_apikey UI Function | ||
#' | ||
#' @description A shiny Module. | ||
#' | ||
#' @param id,input,output,session Internal parameters for {shiny}. | ||
#' | ||
#' @noRd | ||
#' | ||
#' @importFrom shiny NS tagList | ||
mod_input_apikey_ui <- function(id){ | ||
ns <- NS(id) | ||
tagList( | ||
|
||
## START - MY CODE | ||
passwordInput(ns("apikey"), label = "Enter your OpenAI API Key"), | ||
actionButton(ns("buttonSaveApiKey"), label = "Save", icon = icon("upload")), | ||
br(),br(), | ||
textOutput(ns("messageApiKeySaved")) | ||
## END - MY CODE | ||
|
||
) | ||
} | ||
|
||
#' input_apikey Server Functions | ||
#' | ||
#' @noRd | ||
mod_input_apikey_server <- function(id){ | ||
moduleServer( id, function(input, output, session){ | ||
ns <- session$ns | ||
|
||
## START - MY CODE | ||
r <- reactive( | ||
set_apikey(input$apikey) | ||
) |> | ||
bindEvent(input$buttonSaveApiKey) | ||
|
||
output$messageApiKeySaved <- renderText({ | ||
r() | ||
"Your API Key has been saved." | ||
}) |> | ||
bindEvent(input$buttonSaveApiKey) | ||
## END - MY CODE | ||
|
||
|
||
}) | ||
} | ||
|
||
## To be copied in the UI | ||
# mod_input_apikey_ui("input_apikey_1") | ||
|
||
## To be copied in the server | ||
# mod_input_apikey_server("input_apikey_1") |
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,48 @@ | ||
#' show_gallery UI Function | ||
#' | ||
#' @description A shiny Module. | ||
#' | ||
#' @param id,input,output,session Internal parameters for {shiny}. | ||
#' | ||
#' @noRd | ||
#' | ||
#' @importFrom shiny NS tagList | ||
mod_show_gallery_ui <- function(id){ | ||
ns <- NS(id) | ||
tagList( | ||
|
||
## START - MY CODE | ||
uiOutput(ns("module_body")) | ||
## END - MY CODE | ||
) | ||
} | ||
|
||
#' show_gallery Server Functions | ||
#' | ||
#' @noRd | ||
mod_show_gallery_server <- function(id){ | ||
moduleServer( id, function(input, output, session){ | ||
ns <- session$ns | ||
|
||
## START - MY CODE | ||
output$module_body = renderUI({ | ||
number_images <- length(get_images()) | ||
image_splits <- split(get_images(), cut(seq_along(get_images()), 4, labels = FALSE)) | ||
fluidRow( | ||
column(width = 2, image_splits[1]), | ||
column(width = 2, image_splits[2]), | ||
column(width = 2, image_splits[3]), | ||
column(width = 2, image_splits[4]) | ||
) | ||
} | ||
) | ||
## END - MY CODE | ||
|
||
}) | ||
} | ||
|
||
## To be copied in the UI | ||
# mod_show_gallery_ui("show_gallery_1") | ||
|
||
## To be copied in the server | ||
# mod_show_gallery_server("show_gallery_1") |
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,61 @@ | ||
#' show_instructions UI Function | ||
#' | ||
#' @description A shiny Module. | ||
#' | ||
#' @param id,input,output,session Internal parameters for {shiny}. | ||
#' | ||
#' @noRd | ||
#' | ||
#' @importFrom shiny NS tagList | ||
mod_show_instructions_ui <- function(id){ | ||
ns <- NS(id) | ||
tagList( | ||
|
||
## START - MY CODE | ||
h2("Instructions"), | ||
hr(), | ||
h3("What is ChatGPT Images?"), | ||
p("ChatGPT Images is an application that utilizes OpenAI's ChatGPT model to dynamically create any type of image."), | ||
hr(), | ||
h3("How to use the app?"), | ||
p("Follow the steps below to generate new images and also review images you have created in the app previously."), | ||
hr(), | ||
h4("Step 1 - Enter your API Key"), | ||
p("The first step is to enter your OpenAI API Key in the ", em("Settings"), "tab."), | ||
p("You can create an OpenAI account and get the API Key by visiting ", a(href="https://chat.openai.com","https://chat.openai.com")), | ||
p("Once you have entered your API Key click the Save button."), | ||
hr(), | ||
h4("Step 2 - Create an Image"), | ||
p("After you have saved your API Key, click on the ", em("Create Image"), "tab."), | ||
p("In the text box, enter your description of the image that you would like produced."), | ||
p("Images can be generated in three sizes (in pixels):"), | ||
tags$ul(tags$li("256 x 256"),tags$li("512 x 512"),tags$li("1024 x 1024")), | ||
p("The images are created in a png format."), | ||
hr(), | ||
h4("Step 3 - View all images created"), | ||
p("All images created will be saved and available to view in the ", em("Image Gallery"), "tab."), | ||
p("Here images can be saved individually or download the complete collection.") | ||
## END - MY CODE | ||
|
||
) | ||
} | ||
|
||
#' show_instructions Server Functions | ||
#' | ||
#' @noRd | ||
mod_show_instructions_server <- function(id){ | ||
moduleServer( id, function(input, output, session){ | ||
ns <- session$ns | ||
|
||
## START - MY CODE | ||
|
||
## END - MY CODE | ||
|
||
}) | ||
} | ||
|
||
## To be copied in the UI | ||
# mod_show_instructions_ui("show_instructions_1") | ||
|
||
## To be copied in the server | ||
# mod_show_instructions_server("show_instructions_1") |
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
Oops, something went wrong.