Create README.md
Browse files
README.md
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: mit
|
| 3 |
+
datasets:
|
| 4 |
+
- sartajbhuvaji/gutenberg
|
| 5 |
+
language:
|
| 6 |
+
- en
|
| 7 |
+
base_model:
|
| 8 |
+
- openai-community/gpt2
|
| 9 |
+
pipeline_tag: text-classification
|
| 10 |
+
library_name: transformers
|
| 11 |
+
tags:
|
| 12 |
+
- text-classification
|
| 13 |
+
---
|
| 14 |
+
|
| 15 |
+
```python
|
| 16 |
+
from transformers import GPT2ForSequenceClassification, GPT2Tokenizer
|
| 17 |
+
from datasets import load_dataset
|
| 18 |
+
from transformers import pipeline
|
| 19 |
+
import pandas as pd
|
| 20 |
+
|
| 21 |
+
# Load the model from Hugging Face
|
| 22 |
+
model = GPT2ForSequenceClassification.from_pretrained('sartajbhuvaji/gutenberg-gpt2', num_labels=num_labels)
|
| 23 |
+
tokenizer = GPT2Tokenizer.from_pretrained("sartajbhuvaji/gutenberg-gpt2")
|
| 24 |
+
|
| 25 |
+
# Create a text classification pipeline
|
| 26 |
+
classifier = pipeline("text-classification", model=model, tokenizer=tokenizer)
|
| 27 |
+
|
| 28 |
+
# Test the pipeline
|
| 29 |
+
result = classifier("This is a great book!")
|
| 30 |
+
print(result) # [{'label': 'LABEL_7', 'score': 0.8302432298660278}]
|
| 31 |
+
|
| 32 |
+
# Test the pipeline on a document
|
| 33 |
+
doc_id = 1
|
| 34 |
+
doc_text = df.loc[df['DocID'] == doc_id, 'Text'].values[0]
|
| 35 |
+
result = classifier(doc_text[:1024])
|
| 36 |
+
print(result) # [{'label': 'LABEL_4', 'score': 0.6285566091537476}]
|
| 37 |
+
```
|