evgeniiarazum commited on
Commit
3824263
·
verified ·
1 Parent(s): 0ae53cb

Update tasks/text.py

Browse files
Files changed (1) hide show
  1. tasks/text.py +110 -25
tasks/text.py CHANGED
@@ -1,23 +1,103 @@
1
- from fastapi import APIRouter
2
  from datetime import datetime
3
  from datasets import load_dataset
4
  from sklearn.metrics import accuracy_score
 
5
  import random
 
 
 
6
 
7
  from .utils.evaluation import TextEvaluationRequest
8
  from .utils.emissions import tracker, clean_emissions_data, get_space_info
9
 
10
  router = APIRouter()
11
 
12
- DESCRIPTION = "Random Baseline"
 
 
 
 
 
 
 
 
13
  ROUTE = "/text"
14
 
15
- @router.post(ROUTE, tags=["Text Task"],
16
- description=DESCRIPTION)
17
- async def evaluate_text(request: TextEvaluationRequest):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  """
19
  Evaluate text classification for climate disinformation detection.
20
-
21
  Current Model: Random Baseline
22
  - Makes random predictions from the label space (0-7)
23
  - Used as a baseline for comparison
@@ -34,7 +114,7 @@ async def evaluate_text(request: TextEvaluationRequest):
34
  "4_solutions_harmful_unnecessary": 4,
35
  "5_science_unreliable": 5,
36
  "6_proponents_biased": 6,
37
- "7_fossil_fuels_needed": 7
38
  }
39
 
40
  # Load and prepare the dataset
@@ -44,39 +124,44 @@ async def evaluate_text(request: TextEvaluationRequest):
44
  dataset = dataset.map(lambda x: {"label": LABEL_MAPPING[x["label"]]})
45
 
46
  # Split dataset
47
- train_test = dataset["train"]
48
- test_dataset = dataset["test"]
49
-
 
 
50
  # Start tracking emissions
51
  tracker.start()
52
  tracker.start_task("inference")
53
 
54
- #--------------------------------------------------------------------------------------------
55
  # YOUR MODEL INFERENCE CODE HERE
56
  # Update the code below to replace the random baseline by your model inference within the inference pass where the energy consumption and emissions are tracked.
57
- #--------------------------------------------------------------------------------------------
58
-
59
- # Make random predictions (placeholder for actual model inference)
60
  true_labels = test_dataset["label"]
61
- predictions = [random.randint(0, 7) for _ in range(len(true_labels))]
 
 
 
 
 
62
 
63
- #--------------------------------------------------------------------------------------------
64
  # YOUR MODEL INFERENCE STOPS HERE
65
- #--------------------------------------------------------------------------------------------
66
 
67
-
68
  # Stop tracking emissions
69
  emissions_data = tracker.stop_task()
70
-
71
  # Calculate accuracy
72
  accuracy = accuracy_score(true_labels, predictions)
73
-
74
  # Prepare results dictionary
75
  results = {
76
  "username": username,
77
  "space_url": space_url,
78
  "submission_timestamp": datetime.now().isoformat(),
79
- "model_description": DESCRIPTION,
80
  "accuracy": float(accuracy),
81
  "energy_consumed_wh": emissions_data.energy_consumed * 1000,
82
  "emissions_gco2eq": emissions_data.emissions * 1000,
@@ -85,8 +170,8 @@ async def evaluate_text(request: TextEvaluationRequest):
85
  "dataset_config": {
86
  "dataset_name": request.dataset_name,
87
  "test_size": request.test_size,
88
- "test_seed": request.test_seed
89
- }
90
  }
91
-
92
- return results
 
1
+ from fastapi import APIRouter, Query
2
  from datetime import datetime
3
  from datasets import load_dataset
4
  from sklearn.metrics import accuracy_score
5
+ import numpy as np
6
  import random
7
+ import torch
8
+ from torch.utils.data import Dataset, DataLoader
9
+ from transformers import AutoConfig, AutoModelForSequenceClassification, AutoTokenizer
10
 
11
  from .utils.evaluation import TextEvaluationRequest
12
  from .utils.emissions import tracker, clean_emissions_data, get_space_info
13
 
14
  router = APIRouter()
15
 
16
+ MODEL_TYPE = "bert-mini"
17
+ DESCRIPTIONS = {
18
+ "baseline": "baseline most common class",
19
+ "bert-base": "bert base fine tuned on just training data, Nvidia T4 small",
20
+ "bert-medium": "bert medium fine tuned on just training data, Nvidia T4 small",
21
+ "bert-small": "bert small fine tuned on just training data, Nvidia T4 small",
22
+ "bert-mini": "bert mini fine tuned on just training data, Nvidia T4 small",
23
+ "bert-tiny": "bert tiny fine tuned on just training data, Nvidia T4 small",
24
+ }
25
  ROUTE = "/text"
26
 
27
+
28
+ class TextDataset(Dataset):
29
+ def __init__(self, texts, tokenizer, max_length=256):
30
+ self.texts = texts
31
+ self.encodings = tokenizer(
32
+ texts,
33
+ truncation=True,
34
+ padding=True,
35
+ max_length=max_length,
36
+ return_tensors="pt",
37
+ )
38
+
39
+ def __getitem__(self, idx):
40
+ item = {key: val[idx] for key, val in self.encodings.items()}
41
+ return item
42
+
43
+ def __len__(self) -> int:
44
+ return len(self.texts)
45
+
46
+
47
+ def baseline_model(dataset_length: int):
48
+ # Make random predictions (placeholder for actual model inference)
49
+ # predictions = [random.randint(0, 7) for _ in range(dataset_length)]
50
+
51
+ # My favorite baseline is the most common class.
52
+ predictions = [0] * dataset_length
53
+
54
+ return predictions
55
+
56
+
57
+ def bert_model(test_dataset: dict, model_type: str):
58
+ print("Starting my code block.")
59
+ texts = test_dataset["quote"]
60
+
61
+ model_repo = f"Nonnormalizable/frugal-ai-text-{model_type}"
62
+ print(f"Loading from model_repo: {model_repo}")
63
+ config = AutoConfig.from_pretrained(model_repo)
64
+ model = AutoModelForSequenceClassification.from_pretrained(model_repo)
65
+ tokenizer = AutoTokenizer.from_pretrained(model_repo)
66
+
67
+ if torch.cuda.is_available():
68
+ device = torch.device("cuda")
69
+ else:
70
+ device = torch.device("cpu")
71
+ print("Using device:", device)
72
+ model = model.to(device)
73
+ dataset = TextDataset(texts, tokenizer=tokenizer)
74
+ dataloader = DataLoader(dataset, batch_size=32, shuffle=False)
75
+ model.eval()
76
+ with torch.no_grad():
77
+ print("Starting model run.")
78
+ predictions = np.array([])
79
+ for batch in dataloader:
80
+ test_input_ids = batch["input_ids"].to(device)
81
+ test_attention_mask = batch["attention_mask"].to(device)
82
+ outputs = model(test_input_ids, test_attention_mask)
83
+ p = torch.argmax(outputs.logits, dim=1)
84
+ predictions = np.append(predictions, p.cpu().numpy())
85
+ print("End of model run.")
86
+
87
+ print("End of my code block.")
88
+ return predictions
89
+
90
+
91
+ @router.post(ROUTE, tags=["Text Task"])
92
+ async def evaluate_text(
93
+ request: TextEvaluationRequest,
94
+ model_type: str = MODEL_TYPE,
95
+ # This should be an API query parameter, but it looks like the submission repo
96
+ # https://huggingface.co/spaces/frugal-ai-challenge/submission-portal
97
+ # is built in a way to not accept any other endpoints or parameters.
98
+ ):
99
  """
100
  Evaluate text classification for climate disinformation detection.
 
101
  Current Model: Random Baseline
102
  - Makes random predictions from the label space (0-7)
103
  - Used as a baseline for comparison
 
114
  "4_solutions_harmful_unnecessary": 4,
115
  "5_science_unreliable": 5,
116
  "6_proponents_biased": 6,
117
+ "7_fossil_fuels_needed": 7,
118
  }
119
 
120
  # Load and prepare the dataset
 
124
  dataset = dataset.map(lambda x: {"label": LABEL_MAPPING[x["label"]]})
125
 
126
  # Split dataset
127
+ train_test = dataset["train"].train_test_split(
128
+ test_size=request.test_size, seed=request.test_seed
129
+ )
130
+ test_dataset = train_test["test"]
131
+
132
  # Start tracking emissions
133
  tracker.start()
134
  tracker.start_task("inference")
135
 
136
+ # --------------------------------------------------------------------------------------------
137
  # YOUR MODEL INFERENCE CODE HERE
138
  # Update the code below to replace the random baseline by your model inference within the inference pass where the energy consumption and emissions are tracked.
139
+ # --------------------------------------------------------------------------------------------
140
+
 
141
  true_labels = test_dataset["label"]
142
+ if model_type == "baseline":
143
+ predictions = baseline_model(len(true_labels))
144
+ elif model_type[:5] == "bert-":
145
+ predictions = bert_model(test_dataset, model_type)
146
+ else:
147
+ raise ValueError(model_type)
148
 
149
+ # --------------------------------------------------------------------------------------------
150
  # YOUR MODEL INFERENCE STOPS HERE
151
+ # --------------------------------------------------------------------------------------------
152
 
 
153
  # Stop tracking emissions
154
  emissions_data = tracker.stop_task()
155
+
156
  # Calculate accuracy
157
  accuracy = accuracy_score(true_labels, predictions)
158
+
159
  # Prepare results dictionary
160
  results = {
161
  "username": username,
162
  "space_url": space_url,
163
  "submission_timestamp": datetime.now().isoformat(),
164
+ "model_description": DESCRIPTIONS[model_type],
165
  "accuracy": float(accuracy),
166
  "energy_consumed_wh": emissions_data.energy_consumed * 1000,
167
  "emissions_gco2eq": emissions_data.emissions * 1000,
 
170
  "dataset_config": {
171
  "dataset_name": request.dataset_name,
172
  "test_size": request.test_size,
173
+ "test_seed": request.test_seed,
174
+ },
175
  }
176
+
177
+ return results