Spaces:
Sleeping
Sleeping
improve submission
Browse files- requirements.txt +11 -12
- submission_script.py +5 -2
- tasks/text.py +103 -82
requirements.txt
CHANGED
|
@@ -1,13 +1,12 @@
|
|
| 1 |
-
fastapi
|
| 2 |
-
uvicorn
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
requests
|
| 10 |
-
|
| 11 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
|
|
|
|
|
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 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 |
-
|
|
|
|
| 90 |
|
| 91 |
-
#
|
| 92 |
-
|
| 93 |
-
model = model.to(device)
|
| 94 |
-
model.eval()
|
| 95 |
|
| 96 |
-
|
| 97 |
-
|
|
|
|
| 98 |
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
"
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
}
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
except Exception as e:
|
| 138 |
-
|
| 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))
|