Spaces:
Sleeping
Sleeping
Update tasks/text.py
Browse files- tasks/text.py +37 -2
tasks/text.py
CHANGED
@@ -4,12 +4,14 @@ 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 = "
|
13 |
ROUTE = "/text"
|
14 |
|
15 |
@router.post(ROUTE, tags=["Text Task"],
|
@@ -55,10 +57,43 @@ async def evaluate_text(request: TextEvaluationRequest):
|
|
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
|
|
|
4 |
from sklearn.metrics import accuracy_score
|
5 |
import random
|
6 |
|
7 |
+
from transformers import AutoTokenizer,BertForSequenceClassification,AutoModelForSequenceClassification,Trainer, TrainingArguments,DataCollatorWithPadding
|
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 = "BERT V1.1"
|
15 |
ROUTE = "/text"
|
16 |
|
17 |
@router.post(ROUTE, tags=["Text Task"],
|
|
|
57 |
# YOUR MODEL INFERENCE CODE HERE
|
58 |
# 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.
|
59 |
#--------------------------------------------------------------------------------------------
|
60 |
+
|
61 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
62 |
+
|
63 |
+
tokenizer = AutoTokenizer.from_pretrained("cococli/bert-base-uncased-frugalai")
|
64 |
+
model = AutoModelForSequenceClassification.from_pretrained("cococli/bert-base-uncased-frugalai").to(device)
|
65 |
+
|
66 |
+
# Fonction de préprocessing
|
67 |
+
def preprocess_function(df):
|
68 |
+
tokenized = tokenizer(df["quote"], truncation=True) # Removed padding here
|
69 |
+
return tokenized
|
70 |
+
|
71 |
+
tokenized_test = test_dataset.map(preprocess_function, batched=True)
|
72 |
+
|
73 |
+
tokenized_test.set_format(type="torch", columns=["input_ids", "attention_mask"])
|
74 |
+
data_collator = DataCollatorWithPadding(tokenizer=tokenizer)
|
75 |
+
|
76 |
+
batch_size = 16
|
77 |
+
test_loader = DataLoader(tokenized_test, batch_size=batch_size, collate_fn=data_collator)
|
78 |
+
model.eval()
|
79 |
+
|
80 |
+
# Inférence sur GPU
|
81 |
+
predictions = []
|
82 |
+
with torch.no_grad():
|
83 |
+
for batch in test_loader:
|
84 |
+
input_ids = batch['input_ids'].to(device)
|
85 |
+
attention_mask = batch['attention_mask'].to(device)
|
86 |
+
|
87 |
+
outputs = model(input_ids=input_ids, attention_mask=attention_mask)
|
88 |
+
logits = outputs.logits
|
89 |
+
preds = torch.argmax(logits, dim=-1)
|
90 |
+
|
91 |
+
predictions.extend(preds.cpu().numpy())
|
92 |
+
|
93 |
|
94 |
# Make random predictions (placeholder for actual model inference)
|
95 |
true_labels = test_dataset["label"]
|
96 |
+
# predictions = [random.randint(0, 7) for _ in range(len(true_labels))]
|
97 |
|
98 |
#--------------------------------------------------------------------------------------------
|
99 |
# YOUR MODEL INFERENCE STOPS HERE
|