Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
rossarmstrong committed Jan 30, 2023
1 parent 264c9f9 commit 02e90ec
Show file tree
Hide file tree
Showing 46 changed files with 499 additions and 22 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
.Rdata
.httr-oauth
.DS_Store
.Renviron
15 changes: 7 additions & 8 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ importFrom(golem,bundle_resources)
importFrom(golem,favicon)
importFrom(golem,with_golem_options)
importFrom(shiny,HTML)
importFrom(shiny,NS)
importFrom(shiny,column)
importFrom(shiny,shinyApp)
importFrom(shiny,tagAppendAttributes)
Expand Down
8 changes: 8 additions & 0 deletions R/app_server.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,12 @@
#' @noRd
app_server <- function(input, output, session) {
# Your application server logic

## START - MY CODE
mod_input_apikey_server("input_apikey_1")
mod_create_image_server("create_image_1")
mod_show_gallery_server("show_gallery_1")
mod_show_instructions_server("show_instructions_1")
## END - MY CODE

}
30 changes: 27 additions & 3 deletions R/app_ui.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,37 @@ app_ui <- function(request) {
tagList(
# Leave this function for adding external resources
golem_add_external_resources(),
# Your application UI logic
fluidPage(
h1("chatgptimages")


## START - MY CODE
shinydashboard::dashboardPage(
shinydashboard::dashboardHeader(title = "ChatGPT Images"),

shinydashboard::dashboardSidebar(
shinydashboard::sidebarMenu(
shinydashboard::menuItem("Instructions", tabName = "instructions", icon = icon("book-open-reader")),
shinydashboard::menuItem("Create Image", tabName = "create-image", icon = icon("plus")),
shinydashboard::menuItem("Image Gallery", tabName = "image-gallery", icon = icon("images")),
shinydashboard::menuItem("Settings", tabName = "settings", icon = icon("gear"))
)
),

shinydashboard::dashboardBody(
shinydashboard::tabItems(
shinydashboard::tabItem(tabName = "settings", mod_input_apikey_ui("input_apikey_1")),
shinydashboard::tabItem(tabName = "create-image", mod_create_image_ui("create_image_1")),
shinydashboard::tabItem(tabName = "image-gallery", mod_show_gallery_ui("show_gallery_1")),
shinydashboard::tabItem(tabName = "instructions", mod_show_instructions_ui("show_instructions_1"))
)
)

)
## END - MY CODE

)
}


#' Add external Resources to the Application
#'
#' This function is internally used to add external
Expand Down
12 changes: 12 additions & 0 deletions R/fct_get_filename.R
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]]
}
15 changes: 15 additions & 0 deletions R/fct_get_images.R
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)
})
}
14 changes: 14 additions & 0 deletions R/fct_get_url.R
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,]
}
19 changes: 19 additions & 0 deletions R/fct_set_apikey.R
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)
}
51 changes: 51 additions & 0 deletions R/mod_create_image.R
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")
52 changes: 52 additions & 0 deletions R/mod_input_apikey.R
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")
48 changes: 48 additions & 0 deletions R/mod_show_gallery.R
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")
61 changes: 61 additions & 0 deletions R/mod_show_instructions.R
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")
4 changes: 2 additions & 2 deletions dev/01_start.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ golem::set_golem_options()

## Create Common Files ----
## See ?usethis for more information
usethis::use_mit_license("Golem User") # You can set another license here
usethis::use_readme_rmd(open = FALSE)
#usethis::use_mit_license("Golem User") # You can set another license here
#usethis::use_readme_rmd(open = FALSE)
# Note that `contact` is required since usethis version 2.1.5
# If your {usethis} version is older, you can remove that param
usethis::use_code_of_conduct(contact = "Golem User")
Expand Down
Loading

0 comments on commit 02e90ec

Please sign in to comment.