-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path2_1_talkEmbed.R
More file actions
86 lines (74 loc) · 3.44 KB
/
2_1_talkEmbed.R
File metadata and controls
86 lines (74 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#' Transform audio recordings to embeddings
#'
#' @param talk_filepaths (string) path to a video file (.wav/) list of audio filepaths, each is embedded separately
#' @param model shortcut name for Hugging Face pretained model. Full list https://huggingface.co/transformers/pretrained_models.html
#' @param audio_transcriptions (strings) audio_transcriptions : list
#' (optional) list of audio transcriptions, to be used for Whisper's decoder-based embeddings
#' @param use_decoder (boolean) whether to use Whisper's decoder last hidden state representation.
#' If you just want embeddings from a given audio file where vocal acoustics and sound related harmonics are more important to you, then you should
#' have `use_decoder`=FALSE.
#' If you want semantic embeddings which have more language based meaning "baked into" the audio embeddings, you should use `use_decoder`=TRUE.
#' Note: If you use the decoder’s last hidden state, you must also pass a list of `audio_transcriptions` because the decoder takes in BOTH audio and text.
#' This you can use the talkTranscribe() function which will return the list of transcripts, which you can pass to the `audio_transcriptions` parameter
#' @param tokenizer_parallelism (boolean) whether to use device parallelization during tokenization.
#' @param device (string) name of device: 'cpu', 'gpu', or 'gpu:k' where k is a specific device number
#' @param model_max_length (integer) maximum length of the tokenized text
#' @param hg_gated (boolean) set to True if the model is gated
#' @param hg_token (string) the token to access the gated model got in huggingface website
#' @param trust_remote_code (boolean) use a model with custom code on the Huggingface Hub.
#' @param logging_level (string) Set logging level, options: "critical", "error", "warning", "info", "debug".
#' @return A tibble with embeddings.
#' @examples
#' # Transform audio recordings in the example dataset:
#' # voice_data (included in talk-package), to embeddings.
#' \dontrun{
#' wav_path <- system.file("extdata/",
#' "test_short.wav",
#' package = "talk")
#'
#' talk_embeddings <- talkEmbed(
#' wav_path
#' )
#' talk_embeddings
#' }
#' @seealso \code{\link{talkText}}.
#' @importFrom reticulate source_python
#' @importFrom tibble as_tibble
#' @export
talkEmbed <- function(
talk_filepaths,
model = "openai/whisper-small",
audio_transcriptions = "None",
use_decoder = FALSE,
tokenizer_parallelism = FALSE,
model_max_length = "None",
device = 'cpu',
hg_gated = FALSE,
hg_token = "",
trust_remote_code = FALSE,
logging_level = 'warning'){
reticulate::source_python(system.file("python",
"huggingface_Interface4.py",
package = "talk",
mustWork = TRUE
))
embeddings <- hgTransformerGetEmbedding(
audio_filepaths = talk_filepaths,
audio_transcriptions = audio_transcriptions,
model = model,
use_decoder = use_decoder,
tokenizer_parallelism = tokenizer_parallelism,
model_max_length = model_max_length,
device = device,
hg_gated = hg_gated,
hg_token = hg_token,
trust_remote_code = trust_remote_code,
logging_level = logging_level
)
embeddings <- embeddings[[1]]
emb_tibble <- tibble::as_tibble(
t(embeddings), # Transpose the vector into a single-row matrix
.name_repair = ~ paste0("Dim", seq_along(embeddings)) # Assign column names
)
return(emb_tibble)
}