forked from farach/huggingfaceR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME.Rmd
89 lines (63 loc) · 2.21 KB
/
README.Rmd
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
85
86
87
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# huggingfaceR
<!-- badges: start -->
<!-- badges: end -->
The goal of `huggingfaceR` is to to bring state-of-the-art NLP models to R. `huggingfaceR` is built on top of Hugging Face's [transformer](https://huggingface.co/docs/transformers/index) library.
## Installation
Prior to installing `huggingfaceR` please be sure to have your python environment set up correctly.
```{r eval = FALSE}
install.packages("reticulate")
library(reticulate)
install_miniconda()
```
If you are having issues, more detailed instructions on how to install and configure python can be found [here](https://support.rstudio.com/hc/en-us/articles/360023654474-Installing-and-Configuring-Python-with-RStudio).
Once you have python installed and configured you need to ensure that you have the `keras` python library installed.
```{r eval = FALSE}
py_install("keras")
```
After that you can install the development version of huggingfaceR from [GitHub](https://github.com/) with:
``` r
# install.packages("devtools")
devtools::install_github("farach/huggingfaceR")
```
## Example
`huggingfaceR` makes use of the `transformer` `pipline()` function to quickly make pre-trained models available for use in R. In this example we will load the `distilbert-base-uncased-finetuned-sst-2-english` model to obtain sentiment scores.
```{r example}
library(huggingfaceR)
distilBERT <- hf_load_model("distilbert-base-uncased-finetuned-sst-2-english")
```
With the model now loaded, we can begin using the model.
```{r}
distilBERT("I like you. I love you")
```
We can use this model in a typical tidyverse processing chunk. First we load some libraries.
```{r}
library(tidyverse)
library(janeaustenr)
library(tidytext)
```
Here we get the sentiment score assigned to the text in "Sense & Sensibility".
```{r}
austen_books() |>
filter(
book == "Sense & Sensibility",
text != ""
) %>%
sample_n(20) |>
mutate(
distilBERT_sent = distilBERT(text),
.before = text
) |>
unnest_wider(distilBERT_sent)
```