LorenzoBioinfo commited on
Commit
8b7e49b
·
1 Parent(s): d32d7f7

Update monitoring

Browse files
Files changed (2) hide show
  1. src/data_preparation.py +4 -0
  2. src/monitoring.py +20 -27
src/data_preparation.py CHANGED
@@ -42,6 +42,8 @@ def tokenize_function(examples):
42
  )
43
 
44
 
 
 
45
  # ----------------------------- #
46
  # PREPARAZIONE DEI DATASET #
47
  # ----------------------------- #
@@ -126,6 +128,8 @@ def prepare_youtube(tokenizer, output_path):
126
 
127
 
128
 
 
 
129
  if __name__ == "__main__":
130
  parser = argparse.ArgumentParser(description="Prepara dataset per sentiment analysis.")
131
  parser.add_argument("dataset", choices=["tweet_eval", "youtube"], help="Nome del dataset da preparare.")
 
42
  )
43
 
44
 
45
+
46
+
47
  # ----------------------------- #
48
  # PREPARAZIONE DEI DATASET #
49
  # ----------------------------- #
 
128
 
129
 
130
 
131
+
132
+
133
  if __name__ == "__main__":
134
  parser = argparse.ArgumentParser(description="Prepara dataset per sentiment analysis.")
135
  parser.add_argument("dataset", choices=["tweet_eval", "youtube"], help="Nome del dataset da preparare.")
src/monitoring.py CHANGED
@@ -1,10 +1,10 @@
1
- from transformers import AutoTokenizer, AutoModelForSequenceClassification
2
  from datasets import load_from_disk
3
  from sklearn.metrics import accuracy_score, f1_score, confusion_matrix
4
  import torch
5
  import json
6
  import os
7
- from src.train_model import train_model
8
 
9
  ACCURACY_THRESHOLD = 0.75
10
  MODEL_PATH = "models/sentiment_model"
@@ -13,61 +13,55 @@ YT_PATH = "data/processed/youtube_tokenized"
13
  REPORTS_DIR = "reports"
14
 
15
 
16
- def evaluate_model(model, tokenizer, dataset, dataset_name, sample_size=300):
17
  print(f"Valutazione su {dataset_name}")
18
- if "test" not in dataset:
19
- subset = dataset["train"].train_test_split(test_size=0.1)
20
- else:
21
  subset = dataset["test"].select(range(min(sample_size, len(dataset["test"]))))
 
 
22
 
23
- texts = subset["text"]
24
- labels = subset["label"]
 
25
 
26
- inputs = tokenizer(texts, truncation=True, padding=True, return_tensors="pt")
27
  with torch.no_grad():
28
- outputs = model(**inputs)
29
- preds = torch.argmax(outputs.logits, dim=1).numpy()
30
 
31
- acc = accuracy_score(labels, preds)
32
- f1 = f1_score(labels, preds, average="weighted")
33
- cm = confusion_matrix(labels, preds).tolist()
34
 
35
  print(f"{dataset_name} — Accuracy: {acc:.3f}, F1: {f1:.3f}")
36
  return {"dataset": dataset_name, "accuracy": acc, "f1": f1, "confusion_matrix": cm}
37
 
38
 
39
  def retrain_on_youtube_sample():
40
- from datasets import load_from_disk
41
  youtube_data = load_from_disk(YT_PATH)["train"]
42
-
43
  youtube_sample = youtube_data.shuffle(seed=42).select(range(500))
44
  train_model(additional_data=youtube_sample, output_dir=MODEL_PATH)
45
 
46
 
47
-
48
-
49
  def main():
50
  print("Caricamento del modello")
51
 
52
  if os.path.exists(MODEL_PATH):
53
  model = AutoModelForSequenceClassification.from_pretrained(MODEL_PATH)
54
- tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)
55
  else:
56
- print("⚠️ Modello locale non trovato. Uso modello pre-addestrato di default.")
57
  model = AutoModelForSequenceClassification.from_pretrained(
58
  "cardiffnlp/twitter-roberta-base-sentiment-latest"
59
  )
60
- tokenizer = AutoTokenizer.from_pretrained(
61
- "cardiffnlp/twitter-roberta-base-sentiment-latest"
62
- )
63
 
64
  model.eval()
65
 
66
  tweet_ds = load_from_disk(TWEET_PATH)
67
  youtube_ds = load_from_disk(YT_PATH)
68
 
69
- tweet_metrics = evaluate_model(model, tokenizer, tweet_ds, "TweetEval")
70
- youtube_metrics = evaluate_model(model, tokenizer, youtube_ds, "YouTube Comments")
71
 
72
  print(f"Accuracy su YouTube: {youtube_metrics['accuracy']:.3f}")
73
  if youtube_metrics["accuracy"] < ACCURACY_THRESHOLD:
@@ -84,6 +78,5 @@ def main():
84
  print(f"Risultati salvati in: {metrics_path}")
85
 
86
 
87
-
88
  if __name__ == "__main__":
89
- main()
 
1
+ from transformers import AutoModelForSequenceClassification
2
  from datasets import load_from_disk
3
  from sklearn.metrics import accuracy_score, f1_score, confusion_matrix
4
  import torch
5
  import json
6
  import os
7
+ from train_model import train_model
8
 
9
  ACCURACY_THRESHOLD = 0.75
10
  MODEL_PATH = "models/sentiment_model"
 
13
  REPORTS_DIR = "reports"
14
 
15
 
16
+ def evaluate_model(model, dataset, dataset_name, sample_size=300):
17
  print(f"Valutazione su {dataset_name}")
18
+
19
+ # Prendo il sottoinsieme dei dati
20
+ if "test" in dataset:
21
  subset = dataset["test"].select(range(min(sample_size, len(dataset["test"]))))
22
+ else:
23
+ subset = dataset["train"].train_test_split(test_size=0.1)["test"]
24
 
25
+ input_ids = torch.tensor(subset["input_ids"])
26
+ attention_mask = torch.tensor(subset["attention_mask"])
27
+ labels = torch.tensor(subset["label"])
28
 
 
29
  with torch.no_grad():
30
+ outputs = model(input_ids=input_ids, attention_mask=attention_mask)
31
+ preds = torch.argmax(outputs.logits, dim=1)
32
 
33
+ acc = accuracy_score(labels.numpy(), preds.numpy())
34
+ f1 = f1_score(labels.numpy(), preds.numpy(), average="weighted")
35
+ cm = confusion_matrix(labels.numpy(), preds.numpy()).tolist()
36
 
37
  print(f"{dataset_name} — Accuracy: {acc:.3f}, F1: {f1:.3f}")
38
  return {"dataset": dataset_name, "accuracy": acc, "f1": f1, "confusion_matrix": cm}
39
 
40
 
41
  def retrain_on_youtube_sample():
 
42
  youtube_data = load_from_disk(YT_PATH)["train"]
 
43
  youtube_sample = youtube_data.shuffle(seed=42).select(range(500))
44
  train_model(additional_data=youtube_sample, output_dir=MODEL_PATH)
45
 
46
 
 
 
47
  def main():
48
  print("Caricamento del modello")
49
 
50
  if os.path.exists(MODEL_PATH):
51
  model = AutoModelForSequenceClassification.from_pretrained(MODEL_PATH)
 
52
  else:
53
+ print("Modello locale non trovato. Uso modello pre-addestrato di default.")
54
  model = AutoModelForSequenceClassification.from_pretrained(
55
  "cardiffnlp/twitter-roberta-base-sentiment-latest"
56
  )
 
 
 
57
 
58
  model.eval()
59
 
60
  tweet_ds = load_from_disk(TWEET_PATH)
61
  youtube_ds = load_from_disk(YT_PATH)
62
 
63
+ tweet_metrics = evaluate_model(model, tweet_ds, "TweetEval")
64
+ youtube_metrics = evaluate_model(model, youtube_ds, "YouTube Comments")
65
 
66
  print(f"Accuracy su YouTube: {youtube_metrics['accuracy']:.3f}")
67
  if youtube_metrics["accuracy"] < ACCURACY_THRESHOLD:
 
78
  print(f"Risultati salvati in: {metrics_path}")
79
 
80
 
 
81
  if __name__ == "__main__":
82
+ main()