Skip to content

Commit

Permalink
fix: better manage faulty transcription files
Browse files Browse the repository at this point in the history
the "retry-after" waiting time in case of transcription errors from azure is taken directly from the error message.
Furthermore, faulty files were being written anyway since the results from the recurrent call weren't returned.
  • Loading branch information
bakaburg1 committed Feb 8, 2024
1 parent 8bee7d3 commit 0ca0573
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions R/speech_to_text.R
Original file line number Diff line number Diff line change
Expand Up @@ -387,20 +387,29 @@ use_azure_whisper_stt <- function(

# Check response status
if (response$status_code != 200) {
warning("Error in Azure Whisper API request: ",
httr::content(response, "text"))
if (response$status_code == 429 ||
grepl("temporarily unable to process", httr::content(response, "text"))
) {
message("Retrying in 30 seconds...")
Sys.sleep(30)
use_azure_whisper_stt(

warning("Error ", response$status_code, " in Azure Whisper API request: ",
httr::content(response, "text"), call. = FALSE, immediate. = TRUE)

wait_for <- stringr::str_extract(
httr::content(response, "text", encoding = "UTF-8"),
"\\d+(?= seconds)") |> as.numeric()

if (is.na(wait_for) && !interactive()) stop()

if (is.na(wait_for)) wait_for <- 30

message("Retrying in ", wait_for, " seconds...")

Sys.sleep(wait_for)

res <- use_azure_whisper_stt(
audio_file = audio_file,
language = language,
initial_prompt = initial_prompt,
temperature = temperature
)
} else stop()
temperature = temperature)

return(res)
}

# Return the response
Expand Down

0 comments on commit 0ca0573

Please # to comment.