Skip to content

Commit

Permalink
0.1.0.9000
Browse files Browse the repository at this point in the history
  • Loading branch information
choonghyunryu committed Mar 4, 2023
1 parent 7b287c0 commit c225d0b
Show file tree
Hide file tree
Showing 9 changed files with 164 additions and 3 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: bitGPT
Title: Tools to use chatGPT for Koreans
Version: 0.0.2.9000
Version: 0.1.0.9000
Date: 2023-03-04
Authors@R: c(
person("Choonghyun", "Ryu",, "[email protected]", role = c("aut", "cre"))
Expand All @@ -9,6 +9,7 @@ Description: A collection of tools that support using chatGPT for Korean.
License: GPL (>= 3)
Encoding: UTF-8
Imports:
assertthat,
base64enc,
glue,
httr,
Expand Down
9 changes: 9 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,22 @@ export(regist_naver_key)
export(regist_openai_key)
export(set_naver_key)
export(set_openai_key)
export(transcript_audio)
export(translate)
import(dplyr)
importFrom(assertthat,assert_that)
importFrom(assertthat,is.readable)
importFrom(assertthat,is.string)
importFrom(assertthat,noNA)
importFrom(base64enc,base64decode)
importFrom(base64enc,base64encode)
importFrom(glue,glue)
importFrom(httr,POST)
importFrom(httr,add_headers)
importFrom(httr,content)
importFrom(httr,http_error)
importFrom(httr,status_code)
importFrom(httr,upload_file)
importFrom(jsonlite,fromJSON)
importFrom(magick,image_read)
importFrom(magick,image_write)
Expand Down
8 changes: 8 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# bitSpatial 0.1.0.9000

## NEW FEATURES

* chatGPT를 이용한 Speech to Text. (#7).



# bitSpatial 0.0.2.9000

## NEW FEATURES
Expand Down
108 changes: 108 additions & 0 deletions R/audio.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#' Speech to Text with chatGPT
#' @description chatGPT를 이용해서 음성 오디오 파일로 STT(Speech to Text )를 수행함.
#' @param file character. 음성 오디오 파일 이름.
#' @param language character. 음성 오디오 파일의 언어. ISO-639-1 포맷으로 지정해야 하며,
#' 기본값은 한국어인 "ko".
#' @param openai_api_key character. openai의 API key.
#' @details 오디오 파일의 포맷은 mp3, mp4, mpeg, mpga, m4a, wav, webm중 하나만 허용함.
#' @examples
#' \dontrun{
#' # 음성 오디오 파일
#' speech <- system.file("audios", "korea_r_user.m4a", package = "bitGPT")
#'
#' # 음성 오디오를 텍스트로 전환
#' transcript_audio(speech)
#' }
#' @export
#' @importFrom httr upload_file POST add_headers content http_error status_code
#' @importFrom assertthat assert_that is.string noNA is.readable
#' @importFrom glue glue
#' @importFrom jsonlite fromJSON
transcript_audio <- function(file,
language = "ko",
openai_api_key = Sys.getenv("OPENAI_API_KEY")) {
#-----------------------------------------------------------------------------
# User defined function
verify_mime_type <- function (result) {
if (httr::http_type(result) != "application/json") {
paste("OpenAI API probably has been changed. If you see this, please",
"rise an issue at: https://github.com/irudnyts/openai/issues") %>%
stop()
}
}

#-----------------------------------------------------------------------------
# Validate arguments

assertthat::assert_that(
assertthat::is.string(file),
assertthat::noNA(file),
file.exists(file),
assertthat::is.readable(file)
)

if (!is.null(language)) {
assertthat::assert_that(
assertthat::is.string(language),
assertthat::noNA(language)
)
}

assertthat::assert_that(
assertthat::is.string(openai_api_key),
assertthat::noNA(openai_api_key)
)

#-----------------------------------------------------------------------------
# Build path parameters

task <- "audio/transcriptions"

base_url <- glue::glue("https://api.openai.com/v1/{task}")

headers <- c(
"Authorization" = paste("Bearer", openai_api_key),
"Content-Type" = "multipart/form-data"
)

#---------------------------------------------------------------------------
# Build request body

body <- list()
body[["file"]] <- httr::upload_file(file)
body[["model"]] <- "whisper-1"
body[["language"]] <- language

#---------------------------------------------------------------------------
# Make a request and parse it

response <- httr::POST(
url = base_url,
httr::add_headers(.headers = headers),
body = body
)

verify_mime_type(response)

parsed <- response %>%
httr::content(as = "text", encoding = "UTF-8") %>%
jsonlite::fromJSON(flatten = TRUE)

#---------------------------------------------------------------------------
# Check whether request failed and return parsed

if (httr::http_error(response)) {
paste0(
"OpenAI API request failed [",
httr::status_code(response),
"]:\n\n",
parsed$error$message
) %>%
stop(call. = FALSE)
}

parsed %>%
unlist()
}


File renamed without changes.
Binary file added inst/audios/korea_r_user.m4a
Binary file not shown.
2 changes: 1 addition & 1 deletion man/draw_img.Rd

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

2 changes: 1 addition & 1 deletion man/draw_img_variation.Rd

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

35 changes: 35 additions & 0 deletions man/transcript_audio.Rd

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

0 comments on commit c225d0b

Please sign in to comment.