original commit
Browse files- demo.py +39 -0
- distilbert.py +20 -0
- google.py +10 -0
- roberta.py +10 -0
demo.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#high level way
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
from transformers import AutoConfig
|
| 4 |
+
from transformers import AutoModelForSequenceClassification
|
| 5 |
+
|
| 6 |
+
#pipeline(task, model, tokenizer, device (0 being gpu))
|
| 7 |
+
#['any-to-any', 'audio-classification', 'automatic-speech-recognition', 'depth-estimation',
|
| 8 |
+
# 'document-question-answering', 'feature-extraction', 'fill-mask', 'image-classification',
|
| 9 |
+
# 'image-feature-extraction', 'image-segmentation', 'image-text-to-text', 'keypoint-matching',
|
| 10 |
+
# 'mask-generation', 'ner'(named entity recognition), 'object-detection', 'sentiment-analysis', 'table-question-answering',
|
| 11 |
+
# 'text-classification', 'text-generation', 'text-to-audio', 'text-to-speech', 'token-classification',
|
| 12 |
+
# 'video-classification', 'zero-shot-audio-classification', 'zero-shot-classification',
|
| 13 |
+
# 'zero-shot-image-classification', 'zero-shot-object-detection']
|
| 14 |
+
|
| 15 |
+
# Load the model (this downloads weights automatically on first run)
|
| 16 |
+
#classifier = pipeline("text-classification", model="distilbert-base-uncased")
|
| 17 |
+
|
| 18 |
+
#for mask gen
|
| 19 |
+
predictions = []
|
| 20 |
+
unmasker = pipeline('fill-mask', model='distilbert-base-uncased', device=0)
|
| 21 |
+
results = unmasker("Obama is [MASK] today.", top_k=1)
|
| 22 |
+
predictions.append(results[0])
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
unmasker1 = pipeline('fill-mask', model='roberta-base', device=0)
|
| 27 |
+
results1 = unmasker1("Obama is <mask> today.", top_k=1)
|
| 28 |
+
predictions.append(results1[0])
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
unmasker2 = pipeline('fill-mask', model='bert-base-uncased', top_k=1)
|
| 32 |
+
results2 = unmasker2("Obama is [MASK] today.")
|
| 33 |
+
predictions.append(results2[0])
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
best_overall = max(predictions, key=lambda x: x['score'])
|
| 37 |
+
|
| 38 |
+
print(f"The winning phrase was: {best_overall['sequence']}")
|
| 39 |
+
print(f"With a confidence score of: {best_overall['score']:.4f}")
|
distilbert.py
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import pipeline
|
| 2 |
+
import numpy
|
| 3 |
+
# distilbert tasks
|
| 4 |
+
# fill-mask
|
| 5 |
+
# feature extraction
|
| 6 |
+
# have to use more specific models/other models for other tasks
|
| 7 |
+
|
| 8 |
+
#for mask gen
|
| 9 |
+
unmasker = pipeline('fill-mask', model='distilbert-base-uncased', device=0)
|
| 10 |
+
results = unmasker("I [MASK] today.", top_k=10)
|
| 11 |
+
for prediction in results:
|
| 12 |
+
print(f"Score: {prediction['score']:.4f} | Prediction: {prediction['sequence']}")
|
| 13 |
+
|
| 14 |
+
#for feature extraction
|
| 15 |
+
extractor = pipeline('feature-extraction', model='distilbert-base-uncased', device=0)
|
| 16 |
+
results = extractor("Obama")
|
| 17 |
+
|
| 18 |
+
#idk what this really does
|
| 19 |
+
features = numpy.array(results)
|
| 20 |
+
print(f"Shape: {features.shape}")
|
google.py
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import pipeline
|
| 2 |
+
# google bert tasks
|
| 3 |
+
# fill-mask
|
| 4 |
+
# feature extraction
|
| 5 |
+
# have to use more specific models/other models for other tasks
|
| 6 |
+
# for mask gen
|
| 7 |
+
unmasker = pipeline('fill-mask', model='bert-base-uncased')
|
| 8 |
+
results = unmasker("Obama is [MASK].")
|
| 9 |
+
for prediction in results:
|
| 10 |
+
print(f"Score: {prediction['score']:.4f} | Prediction: {prediction['sequence']}")
|
roberta.py
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import pipeline
|
| 2 |
+
# roberta tasks
|
| 3 |
+
# fill-mask
|
| 4 |
+
# feature extraction
|
| 5 |
+
# have to use more specific models/other models for other tasks
|
| 6 |
+
# for mask gen
|
| 7 |
+
unmasker = pipeline('fill-mask', model='roberta-base', device=0)
|
| 8 |
+
results = unmasker("Obama is <mask>.", top_k=10)
|
| 9 |
+
for prediction in results:
|
| 10 |
+
print(f"Score: {prediction['score']:.4f} | Prediction: {prediction['sequence']}")
|