Skip to content

Commit

Permalink
Merge pull request #543 from hfhoffman1144/huggingface-transformers
Browse files Browse the repository at this point in the history
Huggingface transformers
  • Loading branch information
brendaweles committed Jul 14, 2024
2 parents cfa3804 + de47200 commit c14b862
Show file tree
Hide file tree
Showing 9 changed files with 13,055 additions and 0 deletions.
24 changes: 24 additions & 0 deletions huggingface-transformers/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Hugging Face Transformers: Leverage Open-Source AI in Python

This folder contains the materials for the tutorial (Hugging Face Transformers: Leverage Open-Source AI in Python)[https://realpython.com/huggingface-transformers/].

Transformers is available on [PyPI](https://pypi.org/) and you can install it with [pip](https://realpython.com/what-is-pip/). Open a terminal or command prompt, create a new virtual environment, and then run the following command:

```console
(venv) $ python -m pip install transformers
```

This command will install the latest version of Transformers from PyPI onto your machine. Throughout this tutorial, you'll also leverage [PyTorch](https://realpython.com/pytorch-vs-tensorflow/) to interact with models at a lower level. You can install PyTorch with the following command:

```console
(venv) $ python -m pip install torch
```

To verify that the installations were successful, start a [Python REPL](https://realpython.com/python-repl/) and import `transformers` and `torch`:

```pycon
>>> import transformers
>>> import torch
```

If the imports run without errors, then you've successfully installed the dependencies needed for this tutorial.
25 changes: 25 additions & 0 deletions huggingface-transformers/auto_classes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import torch
from transformers import (
AutoConfig,
AutoModelForSequenceClassification,
AutoTokenizer,
)

model_name = "cardiffnlp/twitter-roberta-base-sentiment-latest"

config = AutoConfig.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name)

text = "I love using the Transformers library!"
encoded_input = tokenizer(text, return_tensors="pt")

with torch.no_grad():
output = model(**encoded_input)

scores = output.logits[0]
probabilities = torch.softmax(scores, dim=0)

for i, prob in enumerate(probabilities):
label = config.id2label[i]
print(f"{i + 1}) {label}: {prob}")
8,500 changes: 8,500 additions & 0 deletions huggingface-transformers/cars_data/Scraped_Car_Review_dodge.csv

Large diffs are not rendered by default.

Loading

0 comments on commit c14b862

Please sign in to comment.