Tonic commited on
Commit
e33fed0
·
unverified ·
1 Parent(s): ab3fccf

improve submission

Browse files
Files changed (3) hide show
  1. requirements.txt +11 -12
  2. submission_script.py +5 -2
  3. tasks/text.py +103 -82
requirements.txt CHANGED
@@ -1,13 +1,12 @@
1
- fastapi>=0.68.0
2
- uvicorn>=0.15.0
3
- codecarbon>=2.3.1
4
- datasets>=2.14.0
5
- scikit-learn>=1.0.2
6
- pydantic>=1.10.0
7
- python-dotenv>=1.0.0
8
- gradio>=4.0.0
9
- requests>=2.31.0
10
- librosa==0.10.2.post1
11
- torch
12
- transformers
13
  accelerate
 
1
+ fastapi==0.103.2
2
+ uvicorn==0.23.2
3
+ transformers==4.34.0
4
+ torch==2.0.1
5
+ datasets==2.14.5
6
+ scikit-learn==1.3.1
7
+ codecarbon==2.3.1
8
+ python-dotenv==1.0.0
9
+ requests==2.31.0
10
+ numpy==1.24.3
11
+ pydantic==2.4.2
 
12
  accelerate
submission_script.py CHANGED
@@ -14,7 +14,6 @@ def evaluate_text_model(space_url: str, max_retries=3, retry_delay=5):
14
  "test_seed": 42,
15
  }
16
 
17
- # Construct base URL and API endpoints
18
  if "localhost" in space_url:
19
  base_url = space_url
20
  else:
@@ -63,7 +62,11 @@ def evaluate_text_model(space_url: str, max_retries=3, retry_delay=5):
63
  return response.json()
64
  else:
65
  print(f"Error: Status {response.status_code}")
66
- print(f"Response: {response.text}")
 
 
 
 
67
  if attempt < max_retries - 1:
68
  print(f"Waiting {retry_delay} seconds before retry...")
69
  time.sleep(retry_delay)
 
14
  "test_seed": 42,
15
  }
16
 
 
17
  if "localhost" in space_url:
18
  base_url = space_url
19
  else:
 
62
  return response.json()
63
  else:
64
  print(f"Error: Status {response.status_code}")
65
+ try:
66
+ error_detail = response.json()
67
+ print(f"Error detail: {error_detail}")
68
+ except:
69
+ print(f"Response: {response.text}")
70
  if attempt < max_retries - 1:
71
  print(f"Waiting {retry_delay} seconds before retry...")
72
  time.sleep(retry_delay)
tasks/text.py CHANGED
@@ -1,3 +1,4 @@
 
1
  from fastapi import APIRouter
2
  from datetime import datetime
3
  from datasets import load_dataset
@@ -5,10 +6,15 @@ from sklearn.metrics import accuracy_score
5
  import torch
6
  from transformers import AutoTokenizer, AutoModelForSequenceClassification
7
  from torch.utils.data import Dataset, DataLoader
 
8
 
9
  from .utils.evaluation import TextEvaluationRequest
10
  from .utils.emissions import tracker, clean_emissions_data, get_space_info
11
 
 
 
 
 
12
  router = APIRouter()
13
 
14
  DESCRIPTION = "Climate Guard Toxic Agent Model"
@@ -47,93 +53,108 @@ async def evaluate_text(request: TextEvaluationRequest):
47
  """
48
  Evaluate text classification for climate disinformation detection.
49
  """
50
- username, space_url = get_space_info()
 
 
51
 
52
- # Label mapping
53
- LABEL_MAPPING = {
54
- "0_not_relevant": 0,
55
- "1_not_happening": 1,
56
- "2_not_human": 2,
57
- "3_not_bad": 3,
58
- "4_solutions_harmful_unnecessary": 4,
59
- "5_science_unreliable": 5,
60
- "6_proponents_biased": 6,
61
- "7_fossil_fuels_needed": 7
62
- }
63
 
64
- # Load dataset
65
- dataset = load_dataset(request.dataset_name)
66
-
67
- # Convert string labels to integers
68
- dataset = dataset.map(lambda x: {"label": LABEL_MAPPING[x["label"]]})
69
-
70
- # Get test dataset
71
- test_dataset = dataset["test"]
72
-
73
- # Start tracking emissions
74
- tracker.start()
75
-
76
- try:
77
- # Load model and tokenizer
78
- model_name = "Tonic/climate-guard-toxic-agent"
79
- tokenizer = AutoTokenizer.from_pretrained(model_name)
80
- model = AutoModelForSequenceClassification.from_pretrained(model_name)
81
-
82
- # Prepare dataset
83
- test_data = TextDataset(
84
- texts=test_dataset["text"],
85
- labels=test_dataset["label"],
86
- tokenizer=tokenizer
87
- )
88
 
89
- test_loader = DataLoader(test_data, batch_size=16)
 
90
 
91
- # Model inference
92
- device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
93
- model = model.to(device)
94
- model.eval()
95
 
96
- predictions = []
97
- ground_truth = []
 
98
 
99
- with torch.no_grad():
100
- for batch in test_loader:
101
- input_ids = batch['input_ids'].to(device)
102
- attention_mask = batch['attention_mask'].to(device)
103
- labels = batch['labels'].to(device)
104
-
105
- outputs = model(input_ids=input_ids, attention_mask=attention_mask)
106
- _, predicted = torch.max(outputs.logits, 1)
107
-
108
- predictions.extend(predicted.cpu().numpy())
109
- ground_truth.extend(labels.cpu().numpy())
110
-
111
- # Calculate accuracy
112
- accuracy = accuracy_score(ground_truth, predictions)
113
-
114
- # Stop tracking emissions
115
- emissions_data = tracker.stop()
116
-
117
- # Prepare results
118
- results = {
119
- "username": username,
120
- "space_url": space_url,
121
- "submission_timestamp": datetime.now().isoformat(),
122
- "model_description": DESCRIPTION,
123
- "accuracy": float(accuracy),
124
- "energy_consumed_wh": emissions_data.energy_consumed * 1000,
125
- "emissions_gco2eq": emissions_data.emissions * 1000,
126
- "emissions_data": clean_emissions_data(emissions_data),
127
- "api_route": ROUTE,
128
- "dataset_config": {
129
- "dataset_name": request.dataset_name,
130
- "test_size": request.test_size,
131
- "test_seed": request.test_seed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
132
  }
133
- }
134
-
135
- return results
136
-
 
 
 
 
 
137
  except Exception as e:
138
- tracker.stop()
139
- raise e
 
1
+ # tasks/text.py
2
  from fastapi import APIRouter
3
  from datetime import datetime
4
  from datasets import load_dataset
 
6
  import torch
7
  from transformers import AutoTokenizer, AutoModelForSequenceClassification
8
  from torch.utils.data import Dataset, DataLoader
9
+ import logging
10
 
11
  from .utils.evaluation import TextEvaluationRequest
12
  from .utils.emissions import tracker, clean_emissions_data, get_space_info
13
 
14
+ # Set up logging
15
+ logging.basicConfig(level=logging.INFO)
16
+ logger = logging.getLogger(__name__)
17
+
18
  router = APIRouter()
19
 
20
  DESCRIPTION = "Climate Guard Toxic Agent Model"
 
53
  """
54
  Evaluate text classification for climate disinformation detection.
55
  """
56
+ try:
57
+ logger.info("Starting evaluation")
58
+ username, space_url = get_space_info()
59
 
60
+ # Label mapping
61
+ LABEL_MAPPING = {
62
+ "0_not_relevant": 0,
63
+ "1_not_happening": 1,
64
+ "2_not_human": 2,
65
+ "3_not_bad": 3,
66
+ "4_solutions_harmful_unnecessary": 4,
67
+ "5_science_unreliable": 5,
68
+ "6_proponents_biased": 6,
69
+ "7_fossil_fuels_needed": 7
70
+ }
71
 
72
+ logger.info("Loading dataset")
73
+ # Load dataset
74
+ dataset = load_dataset(request.dataset_name)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
 
76
+ # Convert string labels to integers
77
+ dataset = dataset.map(lambda x: {"label": LABEL_MAPPING[x["label"]]})
78
 
79
+ # Get test dataset
80
+ test_dataset = dataset["test"]
 
 
81
 
82
+ logger.info("Starting emissions tracking")
83
+ # Start tracking emissions
84
+ tracker.start()
85
 
86
+ try:
87
+ # Load model and tokenizer
88
+ logger.info("Loading model and tokenizer")
89
+ model_name = "Tonic/climate-guard-toxic-agent"
90
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
91
+ model = AutoModelForSequenceClassification.from_pretrained(model_name, num_labels=len(LABEL_MAPPING))
92
+
93
+ # Prepare dataset
94
+ logger.info("Preparing dataset")
95
+ test_data = TextDataset(
96
+ texts=test_dataset["text"],
97
+ labels=test_dataset["label"],
98
+ tokenizer=tokenizer
99
+ )
100
+
101
+ test_loader = DataLoader(test_data, batch_size=16)
102
+
103
+ # Model inference
104
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
105
+ logger.info(f"Using device: {device}")
106
+ model = model.to(device)
107
+ model.eval()
108
+
109
+ predictions = []
110
+ ground_truth = []
111
+
112
+ logger.info("Running inference")
113
+ with torch.no_grad():
114
+ for batch in test_loader:
115
+ input_ids = batch['input_ids'].to(device)
116
+ attention_mask = batch['attention_mask'].to(device)
117
+ labels = batch['labels'].to(device)
118
+
119
+ outputs = model(input_ids=input_ids, attention_mask=attention_mask)
120
+ _, predicted = torch.max(outputs.logits, 1)
121
+
122
+ predictions.extend(predicted.cpu().numpy())
123
+ ground_truth.extend(labels.cpu().numpy())
124
+
125
+ # Calculate accuracy
126
+ accuracy = accuracy_score(ground_truth, predictions)
127
+ logger.info(f"Accuracy: {accuracy}")
128
+
129
+ # Stop tracking emissions
130
+ emissions_data = tracker.stop()
131
+
132
+ # Prepare results
133
+ results = {
134
+ "username": username,
135
+ "space_url": space_url,
136
+ "submission_timestamp": datetime.now().isoformat(),
137
+ "model_description": DESCRIPTION,
138
+ "accuracy": float(accuracy),
139
+ "energy_consumed_wh": float(emissions_data.energy_consumed * 1000),
140
+ "emissions_gco2eq": float(emissions_data.emissions * 1000),
141
+ "emissions_data": clean_emissions_data(emissions_data),
142
+ "api_route": ROUTE,
143
+ "dataset_config": {
144
+ "dataset_name": request.dataset_name,
145
+ "test_size": request.test_size,
146
+ "test_seed": request.test_seed
147
+ }
148
  }
149
+
150
+ logger.info("Evaluation completed successfully")
151
+ return results
152
+
153
+ except Exception as e:
154
+ logger.error(f"Error during evaluation: {str(e)}")
155
+ tracker.stop()
156
+ raise e
157
+
158
  except Exception as e:
159
+ logger.error(f"Error in evaluate_text: {str(e)}")
160
+ raise HTTPException(status_code=500, detail=str(e))