Skip to content

Instantly share code, notes, and snippets.

@coolbutuseless
Last active June 20, 2023 16:29
Show Gist options
  • Save coolbutuseless/d24cc225c473e12be8af0a065dcbc977 to your computer and use it in GitHub Desktop.
Save coolbutuseless/d24cc225c473e12be8af0a065dcbc977 to your computer and use it in GitHub Desktop.
run dplyr commands verbally
library(carelesswhisper)
library(styler)
library(dplyr)
library(stringr)
ctx <- whisper_init()
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' Record audio and convert to text
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
speech2text <- function(dur = 5) {
snd <- record_audio(dur)
whisper(ctx, snd)
}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' A huge manual hack
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
text2code <- function(text) {
text <- tolower(text)
text <- str_replace_all(text, "take|,|\\.|\\?|is|with|where|\\bthe\\b|\\ba\\b|number of|thank you|you", "")
text <- str_replace_all(text, "and then" , "%>%")
text <- str_replace_all(text, "greater than" , ">")
text <- str_replace_all(text, "is equal to|equals" , "==")
text <- str_replace_all(text, "empty cars" , "mtcars")
text <- str_replace_all(text, "filter" , "filter(")
text <- str_replace_all(text, "weight|wait|white" , "wt")
text <- str_replace_all(text, "cylinders|sill" , "cyl")
text <- str_replace_all(text, "zero" , "0")
text <- str_replace_all(text, "one" , "1")
text <- str_replace_all(text, "two" , "2")
text <- str_replace_all(text, "three" , "3")
text <- str_replace_all(text, "four" , "4")
text <- str_replace_all(text, "five" , "5")
text <- str_replace_all(text, "six" , "6")
text <- str_replace_all(text, "seven" , "7")
text <- str_replace_all(text, "eight" , "8")
text <- str_replace_all(text, "nine" , "9")
text <- str_replace_all(text, "ten" , "10")
text <- paste0(text, ")")
text <- styler::style_text(text)
text
}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' Wait for input and run it
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
possibly_text2code <- purrr::possibly(text2code)
hey_rstats <- function() {
while(TRUE) {
message(".")
text <- speech2text(6)
message(text)
if (text == '') next
code <- possibly_text2code(text)
if (is.null(code)) {
message("Try again. Did not understand: '", text, "'")
next
}
cat("Code:", code, "\n")
print(eval(parse(text = code)))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment