{"id":13665772,"url":"https://github.com/farach/huggingfaceR","last_synced_at":"2025-04-26T08:33:13.763Z","repository":{"id":37929203,"uuid":"496020735","full_name":"farach/huggingfaceR","owner":"farach","description":"Hugging Face state-of-the-art models in R","archived":false,"fork":false,"pushed_at":"2023-01-28T12:23:18.000Z","size":2455,"stargazers_count":141,"open_issues_count":8,"forks_count":17,"subscribers_count":9,"default_branch":"main","last_synced_at":"2024-11-11T00:37:08.575Z","etag":null,"topics":["huggingface","nlp","r","rstats"],"latest_commit_sha":null,"homepage":"","language":"R","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/farach.png","metadata":{"files":{"readme":"README.Rmd","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-05-24T23:41:21.000Z","updated_at":"2024-11-02T09:05:00.000Z","dependencies_parsed_at":"2023-02-15T16:15:49.410Z","dependency_job_id":null,"html_url":"https://github.com/farach/huggingfaceR","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/farach%2FhuggingfaceR","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/farach%2FhuggingfaceR/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/farach%2FhuggingfaceR/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/farach%2FhuggingfaceR/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/farach","download_url":"https://codeload.github.com/farach/huggingfaceR/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250960899,"owners_count":21514541,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["huggingface","nlp","r","rstats"],"created_at":"2024-08-02T06:00:50.345Z","updated_at":"2025-04-26T08:33:08.755Z","avatar_url":"https://github.com/farach.png","language":"R","readme":"---\noutput: github_document\n---\n\n\u003c!-- README.md is generated from README.Rmd. Please edit that file --\u003e\n\n```{r, include = FALSE}\nknitr::opts_chunk$set(\n  collapse = TRUE,\n  comment = \"#\u003e\",\n  fig.path = \"man/figures/README-\",\n  out.width = \"100%\"\n)\n```\n\n# huggingfaceR\n\n\u003c!-- badges: start --\u003e\n\n\u003c!-- badges: end --\u003e\n\nThe goal of `huggingfaceR` is to to bring state-of-the-art NLP models to R. `huggingfaceR` is built on top of Hugging Face's [transformers](https://huggingface.co/docs/transformers/index) library; and has support for navigating the Hugging Face Hub [The Hub](https://huggingface.co/models).\n\n## Installation\n\nPrior to installing `huggingfaceR` please be sure to have your python environment set up correctly.\n\n```{r eval = FALSE}\ninstall.packages(\"reticulate\")\nlibrary(reticulate)\n\ninstall_miniconda()\n```\n\nIf 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).\n\nAfter that you can install the development version of huggingfaceR from [GitHub](https://github.com/) with:\n\n``` r\n# install.packages(\"devtools\")\ndevtools::install_github(\"farach/huggingfaceR\")\n```\n\n## Example\n\n`huggingfaceR` makes use of the `transformers` `pipline()` abstraction to quickly make pre-trained language models available for use in R. In this example we will load the `distilbert-base-uncased-finetuned-sst-2-english` model and its tokenizer into a pipeline object to obtain sentiment scores.\n\n```{r example}\nlibrary(huggingfaceR)\n\ndistilBERT \u003c- hf_load_pipeline(\n  model_id = \"distilbert-base-uncased-finetuned-sst-2-english\", \n  task = \"text-classification\"\n  )\n\ndistilBERT\n```\n\nWith the pipeline now loaded, we can begin using the model.\n\n```{r}\ndistilBERT(\"I like you. I love you\")\n```\n\nWe can use this pipeline in a typical tidyverse processing chunk. First we load the `tidyverse`.\n\n```{r}\nlibrary(tidyverse)\n```\n\nWe can use the `huggingfaceR` `hf_load_dataset()` function to pull in the [emotion](https://huggingface.co/datasets/emotion) Hugging Face dataset. This dataset contains English Twitter messages with six basic emotions: anger, fear, love, sadness, and surprise. We are interested in how well the Distilbert model classifies these emotions as either a positive or a negative sentiment.\n\n```{r}\nemo \u003c- hf_load_dataset(\n  dataset = \"emo\", \n  split = \"train\", \n  as_tibble = TRUE, \n  label_name = \"int2str\"\n  )\n\nemo_model \u003c- emo %\u003e%\n  sample_n(100) %\u003e% \n  transmute(\n    text,\n    emotion_id = label,\n    emotion_name = label_name,\n    distilBERT_sent = distilBERT(text)\n  ) %\u003e%\n  unnest_wider(distilBERT_sent)\n\nglimpse(emo_model)\n```\n\nWe can use `ggplot2` to visualize the results.\n\n```{r}\nemo_model |\u003e\n  mutate(\n    label = paste0(\"Distilbert class:\\n\", label),\n    emotion_name = str_to_title(emotion_name)\n  ) |\u003e\n  ggplot(aes(x = emotion_name, y = score, color = label)) +\n  geom_boxplot(show.legend = FALSE, outlier.alpha = 0.4, ) +\n  scale_color_manual(values = c(\"#D55E00\", \"#6699CC\")) +\n  facet_wrap(~ label) +\n  labs(\n    title = \"Reviewing Distilbert classification predictions\",\n    x = \"Original label\",\n    y = \"Model score\",\n    caption = \"source:\\nhttps://huggingface.co/datasets/emo\"\n  ) +\n  theme_minimal() +\n  theme(\n    plot.title = element_text(hjust = 0.5),\n    axis.text.x = element_text(angle = 45),\n    axis.title.y = element_text(margin = margin(t = 0, r = 10, b = 0, l = 0)),\n    axis.title.x = element_text(margin = margin(t = 10, r = 0, b = 0, l = 0))\n  )\n```\n","funding_links":[],"categories":["R"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffarach%2FhuggingfaceR","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffarach%2FhuggingfaceR","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffarach%2FhuggingfaceR/lists"}