-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #543 from hfhoffman1144/huggingface-transformers
Huggingface transformers
- Loading branch information
Showing
9 changed files
with
13,055 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
8,500
huggingface-transformers/cars_data/Scraped_Car_Review_dodge.csv
Large diffs are not rendered by default.
Oops, something went wrong.
Oops, something went wrong.