Spaces:
Sleeping
Sleeping
Update tasks/text.py
Browse files- tasks/text.py +28 -8
tasks/text.py
CHANGED
@@ -1,10 +1,12 @@
|
|
|
|
|
|
1 |
from fastapi import APIRouter
|
2 |
from datetime import datetime
|
3 |
from datasets import load_dataset
|
4 |
from sklearn.metrics import accuracy_score
|
5 |
-
import
|
6 |
-
from
|
7 |
-
import
|
8 |
|
9 |
from .utils.evaluation import TextEvaluationRequest
|
10 |
from .utils.emissions import tracker, clean_emissions_data, get_space_info
|
@@ -14,6 +16,23 @@ router = APIRouter()
|
|
14 |
DESCRIPTION = "GTE Architecture"
|
15 |
ROUTE = "/text"
|
16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
@router.post(ROUTE, tags=["Text Task"],
|
18 |
description=DESCRIPTION)
|
19 |
async def evaluate_text(request: TextEvaluationRequest):
|
@@ -49,13 +68,14 @@ async def evaluate_text(request: TextEvaluationRequest):
|
|
49 |
|
50 |
true_labels = test_dataset["label"]
|
51 |
texts = test_dataset["quote"]
|
52 |
-
|
|
|
|
|
53 |
model_repo = "elucidator8918/frugal-ai-text"
|
54 |
-
|
55 |
-
model =
|
56 |
tokenizer = AutoTokenizer.from_pretrained(model_repo)
|
57 |
-
|
58 |
-
device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
|
59 |
model = model.to(device)
|
60 |
model.eval()
|
61 |
|
|
|
1 |
+
import torch
|
2 |
+
import random
|
3 |
from fastapi import APIRouter
|
4 |
from datetime import datetime
|
5 |
from datasets import load_dataset
|
6 |
from sklearn.metrics import accuracy_score
|
7 |
+
from transformers import AutoTokenizer, AutoModel, AutoConfig
|
8 |
+
from huggingface_hub import hf_hub_download
|
9 |
+
from safetensors.torch import load_file
|
10 |
|
11 |
from .utils.evaluation import TextEvaluationRequest
|
12 |
from .utils.emissions import tracker, clean_emissions_data, get_space_info
|
|
|
16 |
DESCRIPTION = "GTE Architecture"
|
17 |
ROUTE = "/text"
|
18 |
|
19 |
+
class AutoBertClassifier(nn.Module):
|
20 |
+
def __init__(self, num_labels=8, model_path="Alibaba-NLP/gte-modernbert-base"):
|
21 |
+
super().__init__()
|
22 |
+
self.tokenizer = AutoTokenizer.from_pretrained(model_path)
|
23 |
+
self.bert = AutoModel.from_pretrained(model_path)
|
24 |
+
self.config = AutoConfig.from_pretrained(model_path)
|
25 |
+
self.config.num_labels = num_labels
|
26 |
+
self.dropout = nn.Dropout(0.05)
|
27 |
+
self.classifier = nn.Linear(self.bert.config.hidden_size, num_labels)
|
28 |
+
|
29 |
+
def forward(self, input_ids, attention_mask):
|
30 |
+
outputs = self.bert(input_ids=input_ids, attention_mask=attention_mask)
|
31 |
+
pooled_output = outputs.last_hidden_state[:, 0] # Using [CLS] token representation
|
32 |
+
pooled_output = self.dropout(pooled_output)
|
33 |
+
logits = self.classifier(pooled_output)
|
34 |
+
return logits
|
35 |
+
|
36 |
@router.post(ROUTE, tags=["Text Task"],
|
37 |
description=DESCRIPTION)
|
38 |
async def evaluate_text(request: TextEvaluationRequest):
|
|
|
68 |
|
69 |
true_labels = test_dataset["label"]
|
70 |
texts = test_dataset["quote"]
|
71 |
+
|
72 |
+
device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
|
73 |
+
|
74 |
model_repo = "elucidator8918/frugal-ai-text"
|
75 |
+
model = AutoBertClassifier(num_labels=8)
|
76 |
+
model.load_state_dict(load_file(hf_hub_download(repo_id=model_repo, filename="model.safetensors")))
|
77 |
tokenizer = AutoTokenizer.from_pretrained(model_repo)
|
78 |
+
|
|
|
79 |
model = model.to(device)
|
80 |
model.eval()
|
81 |
|