transformerX helps you to work with the Transformer models available in Huggingface.
Install the devtools package if you haven’t already:
install.packages("devtools")Install transformerX from its GitHub repository:
devtools::install_github("socialx-analytics/transformerX")There are several ways to setup Transformer models. You can choose to use the base Python or the Anaconda distribution. The simplest way is to use Miniconda which can be installed directly from Reticulate.
# Installing Miniconda using reticulate
reticulate::install_miniconda()
# Creating a new conda environment named 'llm'
# Feel free to adjust the environment name as needed
reticulate::conda_create("llm")
# Defining the required packages for the environment
requirements <- c("pytorch", "transformers", "huggingface_hub")
# Installing each package in the 'llm' environment
for (package in requirements) {
reticulate::conda_install("llm", package, channel = c("conda-forge", "pytorch", "huggingface"))
}
# Note: Restart your R session after running this script for changes to take effect
# Setting the conda environment to 'bert'
# Ensure to change this to the name of your created environment if different
reticulate::use_condaenv("llm")
# Checking if the transformers are correctly installed and configured
transformerX::check_transformers()If the Transformers library is successfully imported, you’re ready to go!
Here’s an example of how to use transformerX for Zero Shoot
Classification:
# Load necessary libraries
library(purrr)
library(transformerX)
# Input text
text <- c(
"Gak Mood Makan",
"Otw Sekolah",
"Makasih Bebeb udah Ditraktir"
)
# Load a Model
zs_classifier <- load_zsc_model("ilos-vigil/bigbird-small-indonesian-nli")
# Apply the "apply_zsc" function to each element of the "text" vector
output <- purrr::map_dfr(
.x = text,
.f = apply_zsc,
model = zs_classifier,
candidate_labels = c("Senang", "Sedih", "Netral")
)For any questions, issues, or feedback, please open an issue on our GitHub repository.
