Skip to content

Commit

Permalink
lint cleanup and a start on vision #222
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesHWade committed Aug 24, 2024
1 parent 3380ee8 commit 9eb110e
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 30 deletions.
6 changes: 3 additions & 3 deletions R/mod_chat.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ mod_chat_server <- function(id,
# UI outputs ----
output$welcome <-
renderWelcomeMessage({
welcomeMessage(ide_colors)
}) %>% bindEvent(rv$reset_welcome_message)
welcomeMessage(ide_colors)
}) %>% bindEvent(rv$reset_welcome_message)

output$history <- renderUI({
history$chat_history %>% style_chat_history(ide_colors = ide_colors)
Expand Down Expand Up @@ -95,7 +95,7 @@ mod_chat_server <- function(id,

append_to_conversation_history(
id = history$selected_conversation$id %||% ids::random_id(),
title = history$selected_conversation$title %||% find_placeholder_title(history$chat_history),
title = history$selected_conversation$title %||% find_placeholder_title(history$chat_history), #nolint
messages = history$chat_history
)

Expand Down
26 changes: 13 additions & 13 deletions R/record-audio.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#' An audio clip input control that records short audio clips from the
#' microphone
#'
#' @param inputId The input slot that will be used to access the value.
#' @param id The input slot that will be used to access the value.
#' @param record_label Display label for the "record" control, or NULL for no
#' label. Default is 'Record'.
#' @param stop_label Display label for the "stop" control, or NULL for no label.
Expand All @@ -27,14 +27,14 @@
#' @importFrom htmltools tag tags tagList div
#' @importFrom shiny icon
input_audio_clip <- function(
inputId,
record_label = "Record",
stop_label = "Stop",
reset_on_record = TRUE,
mime_type = NULL,
audio_bits_per_second = NULL,
show_mic_settings = TRUE,
...
id,
record_label = "Record",
stop_label = "Stop",
reset_on_record = TRUE,
mime_type = NULL,
audio_bits_per_second = NULL,
show_mic_settings = TRUE,
...
) {

# Create the settings menu
Expand Down Expand Up @@ -86,7 +86,7 @@ input_audio_clip <- function(
style = "display: block;",
tagList(
div(
style = "display: inline-block; background-color: red; width: 1rem; height: 1rem; border-radius: 100%; position: relative; top: 0.175rem; margin-right: 0.3rem;"
style = "display: inline-block; background-color: red; width: 1rem; height: 1rem; border-radius: 100%; position: relative; top: 0.175rem; margin-right: 0.3rem;" #nolint
),
record_label
)
Expand All @@ -96,7 +96,7 @@ input_audio_clip <- function(
style = "display: block;",
tagList(
div(
style = "display: inline-block; background-color: currentColor; width: 1rem; height: 1rem; position: relative; top: 0.175rem; margin-right: 0.3rem;"
style = "display: inline-block; background-color: currentColor; width: 1rem; height: 1rem; position: relative; top: 0.175rem; margin-right: 0.3rem;" #nolint
),
stop_label
)
Expand All @@ -105,9 +105,9 @@ input_audio_clip <- function(

# Create the main audio-clipper tag
tag("audio-clipper", list(
id = inputId,
id = id,
class = "shiny-audio-clip",
`data-reset-on-record` = if(reset_on_record) "true" else "false",
`data-reset-on-record` = if (reset_on_record) "true" else "false",
`data-mime-type` = mime_type,
`data-audio-bits-per-second` = audio_bits_per_second,
multimodal_dep(),
Expand Down
4 changes: 2 additions & 2 deletions R/run_chatgpt_app.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
#' @return Nothing.
#' @export
gptstudio_run_chat_app <- function(ide_colors = get_ide_theme_info(),
host = getOption("shiny.host", "127.0.0.1"),
port = getOption("shiny.port")) {
host = getOption("shiny.host", "127.0.0.1"),
port = getOption("shiny.port")) {
ui <- mod_app_ui("app", ide_colors)

server <- function(input, output, session) {
Expand Down
51 changes: 44 additions & 7 deletions R/service-openai_api_calls.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,6 @@ openai_create_chat_completion <-
model = getOption("gptstudio.model"),
openai_api_key = Sys.getenv("OPENAI_API_KEY"),
task = "chat/completions") {
assert_that(
is.string(model),
is.string(openai_api_key)
)

if (is.string(prompt)) {
prompt <- list(
list(
Expand All @@ -63,7 +58,7 @@ openai_create_chat_completion <-
messages = prompt
)

query_openai_api(task = task, request_body = body, openai_api_key = openai_api_key)
query_api_openai(task = task, request_body = body, openai_api_key = openai_api_key)
}


Expand All @@ -76,7 +71,7 @@ openai_create_chat_completion <-
#'
#' @return The response from the API.
#'
query_openai_api <- function(task, request_body, openai_api_key = Sys.getenv("OPENAI_API_KEY")) {
query_api_openai <- function(task, request_body, openai_api_key = Sys.getenv("OPENAI_API_KEY")) {
response <- request_base(task, token = openai_api_key) %>%
req_body_json(data = request_body) %>%
req_retry(max_tries = 3) %>%
Expand Down Expand Up @@ -113,3 +108,45 @@ query_openai_api <- function(task, request_body, openai_api_key = Sys.getenv("OP
get_available_endpoints <- function() {
c("completions", "chat/completions", "edits", "embeddings", "models")
}

#' Encode an image file to base64
#'
#' @param image_path String containing the path to the image file
#' @return A base64 encoded string of the image
encode_image <- function(image_path) {
image_file <- file(image_path, "rb")
image_data <- readBin(image_file, "raw", file.info(image_path)$size)
close(image_file)
base64_image <- jsonlite::base64_enc(image_data)
paste0("data:image/jpeg;base64,", base64_image)
}

create_image_chat_openai <- function(image_path,
prompt = "What is this image?",
model = getOption("gptstudio.model"),
openai_api_key = Sys.getenv("OPENAI_API_KEY"),
task = "chat/completions") {
image_data <- encode_image(image_path)
body <- list(
model = model,
messages =
list(
list(
role = "user",
content = list(
list(
type = "text",
text = prompt
),
list(
type = "image_url",
image_url = list(url = image_data)
)
)
)
)
)
query_api_openai(task = task,
request_body = body,
openai_api_key = openai_api_key)
}
16 changes: 11 additions & 5 deletions R/transcribe-audio.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ parse_data_uri <- function(data_uri) {

#' Transcribe Audio from Data URI Using OpenAI's Whisper Model
#'
#' This function takes an audio file in data URI format, converts it to WAV,
#' and sends it to OpenAI's transcription API to get the transcribed text.
#' This function takes an audio file in data URI format, converts it to WAV, and
#' sends it to OpenAI's transcription API to get the transcribed text.
#'
#' @param audio_input A string. The audio data in data URI format.
#' @param api_key A string. Your OpenAI API key. Defaults to the OPENAI_API_KEY environment variable.
#' @param api_key A string. Your OpenAI API key. Defaults to the OPENAI_API_KEY
#' environment variable.
#'
#' @return A string containing the transcribed text.
#'
Expand All @@ -41,7 +42,8 @@ parse_data_uri <- function(data_uri) {
#' print(transcription)
#' }
#'
#' @importFrom httr2 request req_auth_bearer_token req_body_multipart req_perform resp_is_error resp_status_desc resp_body_json
#' @importFrom httr2 request req_auth_bearer_token req_body_multipart
#' req_perform resp_is_error resp_status_desc resp_body_json
#' @importFrom jsonlite fromJSON
transcribe_audio <- function(audio_input, api_key = Sys.getenv("OPENAI_API_KEY")) {
# Parse the data URI
Expand All @@ -51,7 +53,11 @@ transcribe_audio <- function(audio_input, api_key = Sys.getenv("OPENAI_API_KEY")
temp_webm <- tempfile(fileext = ".webm")
temp_wav <- tempfile(fileext = ".wav")
writeBin(parsed$data, temp_webm)
system_result <- system2("ffmpeg", args = c("-i", temp_webm, "-acodec", "pcm_s16le", "-ar", "44100", temp_wav), stdout = TRUE, stderr = TRUE)
system_result <-
system2("ffmpeg",
args = c("-i", temp_webm, "-acodec", "pcm_s16le", "-ar", "44100", temp_wav), #nolint
stdout = TRUE,
stderr = TRUE)

if (!file.exists(temp_wav)) {
stop("Failed to convert audio: ", paste(system_result, collapse = "\n"))
Expand Down
17 changes: 17 additions & 0 deletions man/encode_image.Rd

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

0 comments on commit 9eb110e

Please sign in to comment.