Spaces:
Sleeping
Sleeping
File size: 914 Bytes
1d6bb40 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | from transformers import pipeline
print("Loading DistilBERT sentiment model (first run downloads ~260MB)...")
classifier = pipeline(
"sentiment-analysis",
model="distilbert-base-uncased-finetuned-sst-2-english",
device=-1 # force CPU; avoids trying to find a GPU that isn't there
# on most deploy hosts, which can otherwise raise or stall
)
print("DistilBERT model loaded.")
def get_bert_prediction(text):
result = classifier(text)[0]
label = result["label"]
confidence = result["score"]
prediction = 1 if label == "POSITIVE" else 0
entropy = 1 - confidence
return prediction, confidence, entropy
if __name__ == "__main__":
text = "this movie was amazing"
prediction, confidence, entropy = (
get_bert_prediction(text)
)
print("Prediction:", prediction)
print("Confidence:", confidence)
print("Entropy:", entropy) |