-
Notifications
You must be signed in to change notification settings - Fork 1
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
7b287c0
commit c225d0b
Showing
9 changed files
with
164 additions
and
3 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 |
---|---|---|
@@ -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")) | ||
|
@@ -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, | ||
|
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,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 not shown.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.