Spaces:
Sleeping
Sleeping
eval limit as env and fix evaluate
Browse files- app.py +2 -2
- qwen_classifier/evaluate.py +10 -7
app.py
CHANGED
@@ -16,7 +16,7 @@ from qwen_classifier.predict import predict_single # Your existing function
|
|
16 |
from qwen_classifier.evaluate import evaluate_batch # Your existing function
|
17 |
from qwen_classifier.globals import global_model, global_tokenizer
|
18 |
from qwen_classifier.model import QwenClassifier
|
19 |
-
from qwen_classifier.config import HF_REPO
|
20 |
|
21 |
|
22 |
app = FastAPI(title="Qwen Classifier")
|
@@ -70,7 +70,7 @@ async def load_model():
|
|
70 |
|
71 |
model = QwenClassifier.from_pretrained(
|
72 |
hf_repo,
|
73 |
-
)
|
74 |
global_tokenizer = AutoTokenizer.from_pretrained(hf_repo)
|
75 |
print("Model loaded successfully!")
|
76 |
|
|
|
16 |
from qwen_classifier.evaluate import evaluate_batch # Your existing function
|
17 |
from qwen_classifier.globals import global_model, global_tokenizer
|
18 |
from qwen_classifier.model import QwenClassifier
|
19 |
+
from qwen_classifier.config import HF_REPO, DEVICE
|
20 |
|
21 |
|
22 |
app = FastAPI(title="Qwen Classifier")
|
|
|
70 |
|
71 |
model = QwenClassifier.from_pretrained(
|
72 |
hf_repo,
|
73 |
+
).to(DEVICE)
|
74 |
global_tokenizer = AutoTokenizer.from_pretrained(hf_repo)
|
75 |
print("Model loaded successfully!")
|
76 |
|
qwen_classifier/evaluate.py
CHANGED
@@ -15,6 +15,9 @@ from pathlib import Path
|
|
15 |
from .config import TAG_NAMES, DEVICE, SPACE_URL, EVAL_LIMIT
|
16 |
from .globals import global_model, global_tokenizer
|
17 |
|
|
|
|
|
|
|
18 |
|
19 |
def _load_data(test_data_path):
|
20 |
test_data_path = Path(__file__).parent / test_data_path
|
@@ -40,7 +43,7 @@ def _load_data(test_data_path):
|
|
40 |
raise ValueError("Empty zip archive - no files found")
|
41 |
|
42 |
# Process files with limit
|
43 |
-
for name in names[1:1+
|
44 |
try:
|
45 |
with zip_file.open(name) as f:
|
46 |
content = f.read()
|
@@ -136,14 +139,14 @@ def _evaluate_local(test_data_path, hf_repo):
|
|
136 |
with torch.no_grad():
|
137 |
for batch in dataloader:
|
138 |
print(f"EVALUATION RUNNING ON {global_model.device}")
|
139 |
-
batch = {k: v.to(
|
140 |
labels = batch["labels"].type(torch.float32)
|
141 |
|
142 |
logits = global_model(batch["input_ids"], batch["attention_mask"])
|
143 |
|
144 |
preds = torch.sigmoid(logits).cpu() > 0.5 # Keeps as PyTorch tensor
|
145 |
preds = preds.float() # Convert to 0.0/1.0 if needed
|
146 |
-
labels = labels.cpu()
|
147 |
|
148 |
all_preds.extend(preds)
|
149 |
all_labels.extend(labels)
|
@@ -157,10 +160,10 @@ def _evaluate_local(test_data_path, hf_repo):
|
|
157 |
val_f1_per_class = f1_score(all_labels, all_preds, average=None)
|
158 |
|
159 |
metrics = {
|
160 |
-
'Accuracy':(100*val_acc)
|
161 |
-
'Precision':(100*val_prec)
|
162 |
-
'Recall':(100*val_rec)
|
163 |
-
'F1':(100*val_f1)
|
164 |
'Precision_per_class':(100*val_prec_per_class).astype(int),
|
165 |
'Recall_per_class':(100*val_rec_per_class).astype(int),
|
166 |
'F1_per_class':(100*val_f1_per_class).astype(int),
|
|
|
15 |
from .config import TAG_NAMES, DEVICE, SPACE_URL, EVAL_LIMIT
|
16 |
from .globals import global_model, global_tokenizer
|
17 |
|
18 |
+
eval_limit = os.getenv("EVAL_LIM")
|
19 |
+
if not eval_limit:
|
20 |
+
eval_limit == EVAL_LIMIT
|
21 |
|
22 |
def _load_data(test_data_path):
|
23 |
test_data_path = Path(__file__).parent / test_data_path
|
|
|
43 |
raise ValueError("Empty zip archive - no files found")
|
44 |
|
45 |
# Process files with limit
|
46 |
+
for name in names[1:1+eval_limit]:
|
47 |
try:
|
48 |
with zip_file.open(name) as f:
|
49 |
content = f.read()
|
|
|
139 |
with torch.no_grad():
|
140 |
for batch in dataloader:
|
141 |
print(f"EVALUATION RUNNING ON {global_model.device}")
|
142 |
+
batch = {k: v.to(DEVICE) for k, v in batch.items()}
|
143 |
labels = batch["labels"].type(torch.float32)
|
144 |
|
145 |
logits = global_model(batch["input_ids"], batch["attention_mask"])
|
146 |
|
147 |
preds = torch.sigmoid(logits).cpu() > 0.5 # Keeps as PyTorch tensor
|
148 |
preds = preds.float() # Convert to 0.0/1.0 if needed
|
149 |
+
labels = labels.cpu()
|
150 |
|
151 |
all_preds.extend(preds)
|
152 |
all_labels.extend(labels)
|
|
|
160 |
val_f1_per_class = f1_score(all_labels, all_preds, average=None)
|
161 |
|
162 |
metrics = {
|
163 |
+
'Accuracy':int(100*val_acc),
|
164 |
+
'Precision':int(100*val_prec),
|
165 |
+
'Recall':int(100*val_rec),
|
166 |
+
'F1':int(100*val_f1),
|
167 |
'Precision_per_class':(100*val_prec_per_class).astype(int),
|
168 |
'Recall_per_class':(100*val_rec_per_class).astype(int),
|
169 |
'F1_per_class':(100*val_f1_per_class).astype(int),
|