KeivanR commited on
Commit
d45f589
·
1 Parent(s): b4296bf

debug msg hidden, eval limit

Browse files
Files changed (3) hide show
  1. app.py +17 -6
  2. qwen_classifier/evaluate.py +11 -10
  3. qwen_classifier/predict.py +7 -7
app.py CHANGED
@@ -5,15 +5,18 @@ os.makedirs(os.environ['HF_HOME'], exist_ok=True) # Ensure directory exists
5
 
6
  from fastapi import FastAPI
7
  from fastapi.responses import HTMLResponse
8
- from qwen_classifier.predict import predict_single # Your existing function
9
- from qwen_classifier.evaluate import evaluate_batch # Your existing function
10
- from qwen_classifier.globals import model, tokenizer
11
  import torch
12
  from transformers import AutoTokenizer
13
  from huggingface_hub import login
 
 
 
 
 
 
 
14
  from qwen_classifier.model import QwenClassifier
15
  from qwen_classifier.config import HF_REPO
16
- from pydantic import BaseModel
17
 
18
 
19
  app = FastAPI(title="Qwen Classifier")
@@ -21,6 +24,14 @@ hf_repo = os.getenv("HF_REPO")
21
  if not hf_repo:
22
  hf_repo = HF_REPO
23
 
 
 
 
 
 
 
 
 
24
  # Add this endpoint
25
  @app.get("/", response_class=HTMLResponse)
26
  def home():
@@ -44,7 +55,7 @@ def home():
44
 
45
  @app.on_event("startup")
46
  async def load_model():
47
- global model, tokenizer
48
  # Warm up GPU
49
  torch.zeros(1).cuda()
50
  # Read HF_TOKEN from Hugging Face Space secrets
@@ -60,7 +71,7 @@ async def load_model():
60
  model = QwenClassifier.from_pretrained(
61
  hf_repo,
62
  )
63
- tokenizer = AutoTokenizer.from_pretrained(hf_repo)
64
  print("Model loaded successfully!")
65
 
66
 
 
5
 
6
  from fastapi import FastAPI
7
  from fastapi.responses import HTMLResponse
 
 
 
8
  import torch
9
  from transformers import AutoTokenizer
10
  from huggingface_hub import login
11
+ from pydantic import BaseModel
12
+ import warnings
13
+ from transformers import logging as hf_logging
14
+
15
+ 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")
 
24
  if not hf_repo:
25
  hf_repo = HF_REPO
26
 
27
+ debug = False
28
+ if not debug:
29
+ warnings.filterwarnings("ignore", message="Some weights of the model checkpoint")
30
+ hf_logging.set_verbosity_error()
31
+ else:
32
+ hf_logging.set_verbosity_info()
33
+ warnings.simplefilter("default")
34
+
35
  # Add this endpoint
36
  @app.get("/", response_class=HTMLResponse)
37
  def home():
 
55
 
56
  @app.on_event("startup")
57
  async def load_model():
58
+ global global_model, global_tokenizer
59
  # Warm up GPU
60
  torch.zeros(1).cuda()
61
  # Read HF_TOKEN from Hugging Face Space secrets
 
71
  model = QwenClassifier.from_pretrained(
72
  hf_repo,
73
  )
74
+ global_tokenizer = AutoTokenizer.from_pretrained(hf_repo)
75
  print("Model loaded successfully!")
76
 
77
 
qwen_classifier/evaluate.py CHANGED
@@ -9,8 +9,9 @@ import torch
9
  from datasets import Dataset
10
  from torch.utils.data import DataLoader
11
  import requests
12
- from .config import TAG_NAMES, DEVICE, SPACE_URL
13
- from .globals import model, tokenizer
 
14
 
15
  def load_data(test_data_path):
16
  # zip file handler
@@ -22,7 +23,7 @@ def load_data(test_data_path):
22
  features = ["prob_desc_description","prob_desc_input_spec","prob_desc_output_spec"]
23
  cols = features + ["tags"]
24
  # extract a specific file from the zip container
25
- for name in names[1:]:
26
  f = zip_file.open(name)
27
 
28
  # save the extraced file
@@ -73,15 +74,15 @@ def evaluate_batch(file_path, hf_repo, backend="local", hf_token=None):
73
  raise ValueError(f"Unknown backend: {backend}")
74
 
75
  def _evaluate_local(test_data_path, hf_repo):
76
- global model, tokenizer
77
 
78
  # Lazy-loading to avoid slow startup
79
- if model is None:
80
  from .model import QwenClassifier
81
  from transformers import AutoTokenizer
82
 
83
- model = QwenClassifier.from_pretrained(hf_repo).eval()
84
- tokenizer = AutoTokenizer.from_pretrained(hf_repo)
85
  df = load_data(test_data_path)
86
  df = preprocessing(df)
87
 
@@ -89,7 +90,7 @@ def _evaluate_local(test_data_path, hf_repo):
89
 
90
  # Then apply tokenization
91
  def tokenize_function(examples):
92
- return tokenizer(examples["text"], padding="max_length", truncation=True, max_length=512)
93
 
94
  dataset = hf_dataset.map(tokenize_function, batched=True)
95
 
@@ -98,7 +99,7 @@ def _evaluate_local(test_data_path, hf_repo):
98
  dataloader = DataLoader(dataset, batch_size=8, shuffle=True)
99
 
100
 
101
- model.eval()
102
  all_preds = []
103
  all_labels = []
104
 
@@ -107,7 +108,7 @@ def _evaluate_local(test_data_path, hf_repo):
107
  batch = {k: v.to(DEVICE) for k, v in batch.items()}
108
  labels = batch["labels"].type(torch.float32)
109
 
110
- logits = model(batch["input_ids"], batch["attention_mask"])
111
 
112
  preds = torch.sigmoid(logits).cpu().numpy() > 0.5
113
  labels = labels.cpu().numpy()
 
9
  from datasets import Dataset
10
  from torch.utils.data import DataLoader
11
  import requests
12
+
13
+ from .config import TAG_NAMES, DEVICE, SPACE_URL, EVAL_LIMIT
14
+ from .globals import global_model, global_tokenizer
15
 
16
  def load_data(test_data_path):
17
  # zip file handler
 
23
  features = ["prob_desc_description","prob_desc_input_spec","prob_desc_output_spec"]
24
  cols = features + ["tags"]
25
  # extract a specific file from the zip container
26
+ for name in names[1:1+EVAL_LIMIT]:
27
  f = zip_file.open(name)
28
 
29
  # save the extraced file
 
74
  raise ValueError(f"Unknown backend: {backend}")
75
 
76
  def _evaluate_local(test_data_path, hf_repo):
77
+ global global_model, global_tokenizer
78
 
79
  # Lazy-loading to avoid slow startup
80
+ if global_model is None:
81
  from .model import QwenClassifier
82
  from transformers import AutoTokenizer
83
 
84
+ global_model = QwenClassifier.from_pretrained(hf_repo).eval()
85
+ global_tokenizer = AutoTokenizer.from_pretrained(hf_repo)
86
  df = load_data(test_data_path)
87
  df = preprocessing(df)
88
 
 
90
 
91
  # Then apply tokenization
92
  def tokenize_function(examples):
93
+ return global_tokenizer(examples["text"], padding="max_length", truncation=True, max_length=512)
94
 
95
  dataset = hf_dataset.map(tokenize_function, batched=True)
96
 
 
99
  dataloader = DataLoader(dataset, batch_size=8, shuffle=True)
100
 
101
 
102
+ global_model.eval()
103
  all_preds = []
104
  all_labels = []
105
 
 
108
  batch = {k: v.to(DEVICE) for k, v in batch.items()}
109
  labels = batch["labels"].type(torch.float32)
110
 
111
+ logits = global_model(batch["input_ids"], batch["attention_mask"])
112
 
113
  preds = torch.sigmoid(logits).cpu().numpy() > 0.5
114
  labels = labels.cpu().numpy()
qwen_classifier/predict.py CHANGED
@@ -1,7 +1,7 @@
1
  import torch
2
  import requests
3
  from .config import TAG_NAMES, SPACE_URL
4
- from .globals import model, tokenizer
5
 
6
  def predict_single(text, hf_repo, backend="local", hf_token=None):
7
  if backend == "local":
@@ -12,19 +12,19 @@ def predict_single(text, hf_repo, backend="local", hf_token=None):
12
  raise ValueError(f"Unknown backend: {backend}")
13
 
14
  def _predict_local(text, hf_repo):
15
- global model, tokenizer
16
 
17
  # Lazy-loading to avoid slow startup
18
- if model is None:
19
  from .model import QwenClassifier
20
  from transformers import AutoTokenizer
21
 
22
- model = QwenClassifier.from_pretrained(hf_repo).eval()
23
- tokenizer = AutoTokenizer.from_pretrained(hf_repo)
24
 
25
- inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=512)
26
  with torch.no_grad():
27
- logits = model(**inputs)
28
  return _process_output(logits)
29
 
30
  def _predict_hf_api(text, hf_token=None):
 
1
  import torch
2
  import requests
3
  from .config import TAG_NAMES, SPACE_URL
4
+ from .globals import global_model, global_tokenizer
5
 
6
  def predict_single(text, hf_repo, backend="local", hf_token=None):
7
  if backend == "local":
 
12
  raise ValueError(f"Unknown backend: {backend}")
13
 
14
  def _predict_local(text, hf_repo):
15
+ global global_model, global_tokenizer
16
 
17
  # Lazy-loading to avoid slow startup
18
+ if global_model is None:
19
  from .model import QwenClassifier
20
  from transformers import AutoTokenizer
21
 
22
+ global_model = QwenClassifier.from_pretrained(hf_repo).eval()
23
+ global_tokenizer = AutoTokenizer.from_pretrained(hf_repo)
24
 
25
+ inputs = global_tokenizer(text, return_tensors="pt", truncation=True, max_length=512)
26
  with torch.no_grad():
27
+ logits = global_model(**inputs)
28
  return _process_output(logits)
29
 
30
  def _predict_hf_api(text, hf_token=None):