Spaces:
Runtime error
Runtime error
Commit
·
b72368a
1
Parent(s):
e7f424b
updated sentiment classifier
Browse files- sentiment.py +21 -0
sentiment.py
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from transformers import AutoModelForSequenceClassification,AutoTokenizer
|
| 3 |
+
|
| 4 |
+
model=AutoModelForSequenceClassification.from_pretrained('sentiment_classifier/')
|
| 5 |
+
tokenizer=AutoTokenizer.from_pretrained('sentiment_classifier/')
|
| 6 |
+
|
| 7 |
+
def classify_sentiment(texts,model=model,tokenizer=tokenizer):
|
| 8 |
+
"""
|
| 9 |
+
user will pass texts separated by comma
|
| 10 |
+
"""
|
| 11 |
+
try:
|
| 12 |
+
texts=texts.split(',')
|
| 13 |
+
except:
|
| 14 |
+
pass
|
| 15 |
+
|
| 16 |
+
input = tokenizer(texts, padding=True, truncation=True,
|
| 17 |
+
return_tensors="pt")
|
| 18 |
+
logits = model(**input)['logits'].softmax(dim=1)
|
| 19 |
+
logits = torch.argmax(logits, dim=1)
|
| 20 |
+
output = ['Positive' if i == 1 else 'Negative' for i in logits]
|
| 21 |
+
return output
|