Spaces:
Sleeping
Sleeping
update
Browse files- tasks/text.py +19 -48
tasks/text.py
CHANGED
|
@@ -5,7 +5,7 @@ from sklearn.metrics import accuracy_score
|
|
| 5 |
import random
|
| 6 |
from torch.utils.data import DataLoader
|
| 7 |
|
| 8 |
-
from transformers import AutoTokenizer,
|
| 9 |
|
| 10 |
from .utils.evaluation import TextEvaluationRequest
|
| 11 |
from .utils.emissions import tracker, clean_emissions_data, get_space_info
|
|
@@ -24,9 +24,8 @@ async def evaluate_text(request: TextEvaluationRequest):
|
|
| 24 |
"""
|
| 25 |
Evaluate text classification for climate disinformation detection.
|
| 26 |
|
| 27 |
-
Current Model:
|
| 28 |
-
-
|
| 29 |
-
- Used as a baseline for comparison
|
| 30 |
"""
|
| 31 |
# Get space info
|
| 32 |
username, space_url = get_space_info()
|
|
@@ -52,9 +51,7 @@ async def evaluate_text(request: TextEvaluationRequest):
|
|
| 52 |
# Split dataset
|
| 53 |
train_test = dataset["train"]
|
| 54 |
test_dataset = dataset["test"]
|
| 55 |
-
|
| 56 |
-
print(test_dataset['quote'])
|
| 57 |
-
test_dataset = test_dataset['quote']
|
| 58 |
# Start tracking emissions
|
| 59 |
tracker.start()
|
| 60 |
tracker.start_task("inference")
|
|
@@ -69,74 +66,48 @@ async def evaluate_text(request: TextEvaluationRequest):
|
|
| 69 |
tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
|
| 70 |
model = AutoModelForSequenceClassification.from_pretrained("cococli/bert-base-uncased-frugalai").to(device)
|
| 71 |
|
| 72 |
-
|
| 73 |
-
# def tokenize_frugal(batch, tokenizer):
|
| 74 |
-
# return tokenizer(batch, padding=True, truncation=True)
|
| 75 |
def tokenize_function(examples):
|
| 76 |
return tokenizer(examples["quote"], padding=True, truncation=True, return_tensors='pt')
|
| 77 |
|
| 78 |
-
# Tokenize the test dataset
|
| 79 |
-
tokenized_test =
|
| 80 |
-
|
| 81 |
-
# tokenized_test = test_dataset.map(lambda batch: tokenize_frugal(batch, tokenizer), batched=True)
|
| 82 |
-
# tokenized_test = tokenizer(test_dataset, padding = True, truncation= True)
|
| 83 |
-
|
| 84 |
-
# dataloader = DataLoader(tokenized_test, batch_size=16, shuffle=False)
|
| 85 |
-
# print("Started prediction run")
|
| 86 |
-
|
| 87 |
-
# model.eval()
|
| 88 |
-
# with torch.no_grad():
|
| 89 |
-
# predictions = np.array([])
|
| 90 |
-
# for batch in dataloader:
|
| 91 |
-
# test_input_ids = batch["input_ids"].to(device)
|
| 92 |
-
# test_attention_mask = batch["attention_mask"].to(device)
|
| 93 |
-
# outputs = model(test_input_ids, test_attention_mask)
|
| 94 |
-
# p = torch.argmax(outputs.logits, dim=1)
|
| 95 |
-
# predictions = np.append(predictions, p.cpu().numpy())
|
| 96 |
-
|
| 97 |
-
# print("Finished prediction run")
|
| 98 |
-
data_collator = DataCollatorWithPadding(tokenizer=tokenizer)
|
| 99 |
-
|
| 100 |
# Create DataLoader
|
|
|
|
| 101 |
dataloader = DataLoader(tokenized_test, batch_size=16, shuffle=False, collate_fn=data_collator)
|
| 102 |
-
|
| 103 |
print("Started prediction run")
|
| 104 |
-
|
| 105 |
# Model inference
|
| 106 |
model.eval()
|
| 107 |
predictions = np.array([])
|
| 108 |
-
|
| 109 |
with torch.no_grad():
|
| 110 |
-
|
| 111 |
for batch in dataloader:
|
| 112 |
test_input_ids = batch["input_ids"].to(device)
|
| 113 |
test_attention_mask = batch["attention_mask"].to(device)
|
| 114 |
outputs = model(test_input_ids, test_attention_mask)
|
| 115 |
p = torch.argmax(outputs.logits, dim=1)
|
| 116 |
predictions = np.append(predictions, p.cpu().numpy())
|
| 117 |
-
|
| 118 |
print("Finished prediction run")
|
| 119 |
-
|
| 120 |
# Ensure "label" column exists in dataset
|
| 121 |
print(test_dataset.column_names) # Debugging step
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
# Make random predictions (placeholder for actual model inference)
|
| 126 |
true_labels = test_dataset["label"]
|
| 127 |
-
# predictions = [random.randint(0, 7) for _ in range(len(true_labels))]
|
| 128 |
|
| 129 |
#--------------------------------------------------------------------------------------------
|
| 130 |
# YOUR MODEL INFERENCE STOPS HERE
|
| 131 |
#--------------------------------------------------------------------------------------------
|
| 132 |
|
| 133 |
-
|
| 134 |
# Stop tracking emissions
|
| 135 |
emissions_data = tracker.stop_task()
|
| 136 |
-
|
| 137 |
# Calculate accuracy
|
| 138 |
accuracy = accuracy_score(true_labels, predictions)
|
| 139 |
-
|
| 140 |
# Prepare results dictionary
|
| 141 |
results = {
|
| 142 |
"username": username,
|
|
@@ -154,5 +125,5 @@ async def evaluate_text(request: TextEvaluationRequest):
|
|
| 154 |
"test_seed": request.test_seed
|
| 155 |
}
|
| 156 |
}
|
| 157 |
-
|
| 158 |
-
return results
|
|
|
|
| 5 |
import random
|
| 6 |
from torch.utils.data import DataLoader
|
| 7 |
|
| 8 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification, Trainer, TrainingArguments, DataCollatorWithPadding
|
| 9 |
|
| 10 |
from .utils.evaluation import TextEvaluationRequest
|
| 11 |
from .utils.emissions import tracker, clean_emissions_data, get_space_info
|
|
|
|
| 24 |
"""
|
| 25 |
Evaluate text classification for climate disinformation detection.
|
| 26 |
|
| 27 |
+
Current Model: BERT
|
| 28 |
+
- Uses a pre-trained BERT model for sequence classification
|
|
|
|
| 29 |
"""
|
| 30 |
# Get space info
|
| 31 |
username, space_url = get_space_info()
|
|
|
|
| 51 |
# Split dataset
|
| 52 |
train_test = dataset["train"]
|
| 53 |
test_dataset = dataset["test"]
|
| 54 |
+
|
|
|
|
|
|
|
| 55 |
# Start tracking emissions
|
| 56 |
tracker.start()
|
| 57 |
tracker.start_task("inference")
|
|
|
|
| 66 |
tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
|
| 67 |
model = AutoModelForSequenceClassification.from_pretrained("cococli/bert-base-uncased-frugalai").to(device)
|
| 68 |
|
|
|
|
|
|
|
|
|
|
| 69 |
def tokenize_function(examples):
|
| 70 |
return tokenizer(examples["quote"], padding=True, truncation=True, return_tensors='pt')
|
| 71 |
|
| 72 |
+
# Tokenize the test dataset
|
| 73 |
+
tokenized_test = test_dataset.map(tokenize_function, batched=True)
|
| 74 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
# Create DataLoader
|
| 76 |
+
data_collator = DataCollatorWithPadding(tokenizer=tokenizer)
|
| 77 |
dataloader = DataLoader(tokenized_test, batch_size=16, shuffle=False, collate_fn=data_collator)
|
| 78 |
+
|
| 79 |
print("Started prediction run")
|
| 80 |
+
|
| 81 |
# Model inference
|
| 82 |
model.eval()
|
| 83 |
predictions = np.array([])
|
| 84 |
+
|
| 85 |
with torch.no_grad():
|
|
|
|
| 86 |
for batch in dataloader:
|
| 87 |
test_input_ids = batch["input_ids"].to(device)
|
| 88 |
test_attention_mask = batch["attention_mask"].to(device)
|
| 89 |
outputs = model(test_input_ids, test_attention_mask)
|
| 90 |
p = torch.argmax(outputs.logits, dim=1)
|
| 91 |
predictions = np.append(predictions, p.cpu().numpy())
|
| 92 |
+
|
| 93 |
print("Finished prediction run")
|
| 94 |
+
|
| 95 |
# Ensure "label" column exists in dataset
|
| 96 |
print(test_dataset.column_names) # Debugging step
|
| 97 |
+
|
| 98 |
+
# Extract true labels
|
|
|
|
|
|
|
| 99 |
true_labels = test_dataset["label"]
|
|
|
|
| 100 |
|
| 101 |
#--------------------------------------------------------------------------------------------
|
| 102 |
# YOUR MODEL INFERENCE STOPS HERE
|
| 103 |
#--------------------------------------------------------------------------------------------
|
| 104 |
|
|
|
|
| 105 |
# Stop tracking emissions
|
| 106 |
emissions_data = tracker.stop_task()
|
| 107 |
+
|
| 108 |
# Calculate accuracy
|
| 109 |
accuracy = accuracy_score(true_labels, predictions)
|
| 110 |
+
|
| 111 |
# Prepare results dictionary
|
| 112 |
results = {
|
| 113 |
"username": username,
|
|
|
|
| 125 |
"test_seed": request.test_seed
|
| 126 |
}
|
| 127 |
}
|
| 128 |
+
|
| 129 |
+
return results
|