Spaces:
Sleeping
Sleeping
Download tests/unit/test_model.py from Lordemarco/sentiment-fastapi: direct link, hf CLI and curl.
- Browser
- Download file 924 Bytes
-
https://huggingface.co/spaces/Lordemarco/sentiment-fastapi/resolve/main/tests/unit/test_model.py
- Command line
-
hf download hf://spaces/Lordemarco/sentiment-fastapi/tests/unit/test_model.py
-
curl -L -o test_model.py https://huggingface.co/spaces/Lordemarco/sentiment-fastapi/resolve/main/tests/unit/test_model.py
924 Bytes
| from transformers import AutoTokenizer, AutoModelForSequenceClassification | |
| import torch | |
| MODEL_NAME = "cardiffnlp/twitter-roberta-base-sentiment-latest" | |
| LABELS = ["negative", "neutral", "positive"] | |
| tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME) | |
| model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME) | |
| def test_model_loads(): | |
| assert model is not None | |
| assert tokenizer is not None | |
| def test_model_prediction_shape(): | |
| text = "I love this product!" | |
| inputs = tokenizer(text, return_tensors="pt") | |
| with torch.no_grad(): | |
| outputs = model(**inputs) | |
| assert outputs.logits.shape[-1] == len(LABELS) | |
| def test_sentiment_confidence(): | |
| text = "I hate this" | |
| inputs = tokenizer(text, return_tensors="pt") | |
| with torch.no_grad(): | |
| probs = torch.nn.functional.softmax(model(**inputs).logits, dim=-1) | |
| assert torch.isclose(probs.sum(), torch.tensor(1.0), atol=1e-3) | |