Spaces:
Build error
Build error
Download model.py from Justkushere/MoodMeter: direct link, hf CLI and curl.
- Browser
- Download file 732 Bytes
-
https://huggingface.co/spaces/Justkushere/MoodMeter/resolve/main/model.py
- Command line
-
hf download hf://spaces/Justkushere/MoodMeter/model.py
-
curl -L -o model.py https://huggingface.co/spaces/Justkushere/MoodMeter/resolve/main/model.py
732 Bytes
| # model.py | |
| import torch | |
| from transformers import DistilBertTokenizer, DistilBertForSequenceClassification | |
| tokenizer = DistilBertTokenizer.from_pretrained("distilbert-base-uncased-finetuned-sst-2-english") | |
| model = DistilBertForSequenceClassification.from_pretrained("distilbert-base-uncased-finetuned-sst-2-english") | |
| def get_sentiment(text): | |
| inputs = tokenizer(text, return_tensors="pt") | |
| with torch.no_grad(): | |
| logits = model(**inputs).logits | |
| predicted_class_id = logits.argmax().item() | |
| return model.config.id2label[predicted_class_id] | |
| if __name__ == '__main__': | |
| test_text = "This movie was NOt great!" | |
| sentiment = get_sentiment(test_text) | |
| print(f"Sentiment: {sentiment}") | |