Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- app.py +1 -1
- evaluation.py +26 -0
- model_setup.py +2 -17
- monitoring.py +4 -1
app.py
CHANGED
|
@@ -19,4 +19,4 @@ demo = gr.Interface(
|
|
| 19 |
]
|
| 20 |
)
|
| 21 |
|
| 22 |
-
demo.launch(
|
|
|
|
| 19 |
]
|
| 20 |
)
|
| 21 |
|
| 22 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
evaluation.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sklearn.metrics import accuracy_score, f1_score
|
| 2 |
+
import numpy as np
|
| 3 |
+
from datasets import load_dataset
|
| 4 |
+
from model_setup import predict
|
| 5 |
+
|
| 6 |
+
SAMPLE_SIZE = 100
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def evaluate_model(sample_size=SAMPLE_SIZE):
|
| 10 |
+
"""Score the model on a slice of the test set, return accuracy and weighted F1."""
|
| 11 |
+
dataset = load_dataset("cardiffnlp/tweet_eval", "sentiment")
|
| 12 |
+
test_dataset = dataset["test"]
|
| 13 |
+
|
| 14 |
+
texts = test_dataset["text"][:sample_size]
|
| 15 |
+
true_labels = np.array(test_dataset["label"][:sample_size], dtype=int)
|
| 16 |
+
pred_labels = np.array([predict(text) for text in texts], dtype=int)
|
| 17 |
+
|
| 18 |
+
accuracy = accuracy_score(true_labels, pred_labels)
|
| 19 |
+
f1 = f1_score(true_labels, pred_labels, average="weighted")
|
| 20 |
+
return accuracy, f1
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
if __name__ == "__main__":
|
| 24 |
+
accuracy, f1 = evaluate_model()
|
| 25 |
+
print(f"{'Accuracy:':<12}{accuracy:>8.4f}")
|
| 26 |
+
print(f"{'F1 score:':<12}{f1:>8.4f}")
|
model_setup.py
CHANGED
|
@@ -2,13 +2,6 @@ from transformers import AutoModelForSequenceClassification
|
|
| 2 |
from transformers import AutoTokenizer, AutoConfig
|
| 3 |
import numpy as np
|
| 4 |
from scipy.special import softmax
|
| 5 |
-
from datasets import load_dataset
|
| 6 |
-
|
| 7 |
-
'''
|
| 8 |
-
Load the dataset tweet_eval with sentiment task
|
| 9 |
-
train / validation / test already Splitted
|
| 10 |
-
'''
|
| 11 |
-
dataset = load_dataset("cardiffnlp/tweet_eval", "sentiment")
|
| 12 |
|
| 13 |
# Preprocess text as required by the model
|
| 14 |
# - @username -> @user
|
|
@@ -34,13 +27,5 @@ def predict(text):
|
|
| 34 |
text = preprocess(text)
|
| 35 |
encoded_input = tokenizer(text, return_tensors='pt')
|
| 36 |
output = model(**encoded_input)
|
| 37 |
-
scores = output[0][0].detach().numpy()
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
ranking = np.argsort(scores)[::-1]
|
| 41 |
-
top_idx = int(ranking[0])
|
| 42 |
-
top_label = config.id2label[top_idx]
|
| 43 |
-
top_score = np.round(float(scores[top_idx]), 4)
|
| 44 |
-
print(f"Prediction: {top_label} ({top_score})")
|
| 45 |
-
|
| 46 |
-
return top_idx
|
|
|
|
| 2 |
from transformers import AutoTokenizer, AutoConfig
|
| 3 |
import numpy as np
|
| 4 |
from scipy.special import softmax
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
# Preprocess text as required by the model
|
| 7 |
# - @username -> @user
|
|
|
|
| 27 |
text = preprocess(text)
|
| 28 |
encoded_input = tokenizer(text, return_tensors='pt')
|
| 29 |
output = model(**encoded_input)
|
| 30 |
+
scores = softmax(output[0][0].detach().numpy())
|
| 31 |
+
return int(np.argsort(scores)[::-1][0])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
monitoring.py
CHANGED
|
@@ -1,6 +1,9 @@
|
|
| 1 |
-
from model_setup import dataset, predict
|
| 2 |
import numpy as np
|
| 3 |
import random
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
# Take a sample of random tweets to monitor
|
| 6 |
test_dataset = dataset["test"]
|
|
|
|
|
|
|
| 1 |
import numpy as np
|
| 2 |
import random
|
| 3 |
+
from datasets import load_dataset
|
| 4 |
+
from model_setup import predict
|
| 5 |
+
|
| 6 |
+
dataset = load_dataset("cardiffnlp/tweet_eval", "sentiment")
|
| 7 |
|
| 8 |
# Take a sample of random tweets to monitor
|
| 9 |
test_dataset = dataset["test"]
|