Skip to content

Commit ed8d925

Browse files
authored
Merge branch 'master' into python-string
2 parents 8ff7ddd + bd32d97 commit ed8d925

44 files changed

Lines changed: 14302 additions & 1 deletion

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/dircheck.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
f"{f}: ensure folder name only uses "
4747
f"lowercase letters, numbers, and hyphens"
4848
)
49-
has_error = True
49+
has_errors = True
5050

5151
files = sorted(_.name for _ in f.glob("*"))
5252
if "README.md" not in files:

huggingface-transformers/README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Hugging Face Transformers: Leverage Open-Source AI in Python
2+
3+
This folder contains the materials for the tutorial [Hugging Face Transformers: Leverage Open-Source AI in Python](https://realpython.com/huggingface-transformers/).
4+
5+
## Installation
6+
7+
Create and activate a virtual environment, and then install the required dependencies:
8+
9+
```sh
10+
(venv) $ python -m pip install transformers torch pillow notebook ipywidgets
11+
```
12+
13+
Alternatively, if you use [Poetry](https://realpython.com/dependency-management-python-poetry/), then you can issue the following command to handle the installation process for you:
14+
15+
```sh
16+
$ poetry install
17+
```
18+
19+
## Usage
20+
21+
Run the sample Python scripts:
22+
23+
```sh
24+
(venv) $ python auto_classes.py
25+
(venv) $ python running_pipelines.py
26+
```
27+
28+
Run the sample Jupyter notebook:
29+
30+
```sh
31+
(venv) $ python -m jupyter notebook gpus_google_colab.ipynb
32+
```
33+
34+
Note that if you run this notebook in Google Colab, then you'll need to update the path to the `requirements.txt` file and the `DATA_PATH` constant accordingly:
35+
36+
```diff
37+
-!pip install -r requirements.txt
38+
+!pip install -r /content/requirements.txt
39+
40+
-DATA_PATH = "Scraped_Car_Review_dodge.csv"
41+
+DATA_PATH = "/content/Scraped_Car_Review_dodge.csv"
42+
```

huggingface-transformers/Scraped_Car_Review_dodge.csv

Lines changed: 8500 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import torch
2+
from transformers import (
3+
AutoConfig,
4+
AutoModelForSequenceClassification,
5+
AutoTokenizer,
6+
)
7+
8+
model_name = "cardiffnlp/twitter-roberta-base-sentiment-latest"
9+
10+
config = AutoConfig.from_pretrained(model_name)
11+
tokenizer = AutoTokenizer.from_pretrained(model_name)
12+
model = AutoModelForSequenceClassification.from_pretrained(model_name)
13+
14+
text = "I love using the Transformers library!"
15+
encoded_input = tokenizer(text, return_tensors="pt")
16+
17+
with torch.no_grad():
18+
output = model(**encoded_input)
19+
20+
scores = output.logits[0]
21+
probabilities = torch.softmax(scores, dim=0)
22+
23+
for i, prob in enumerate(probabilities):
24+
label = config.id2label[i]
25+
print(f"{i + 1}) {label}: {prob}")

0 commit comments

Comments
 (0)