jaydeepb commited on
Commit
1ceaa1d
·
verified ·
1 Parent(s): 929bb2f

Fix short-input error: check token length in main process before @spaces.GPU call

Browse files
Files changed (1) hide show
  1. app.py +7 -1
app.py CHANGED
@@ -45,7 +45,7 @@ MEMCLR, NONCLR = "#f58518", "#4c78a8" # memorized (orange) vs not-memorized
45
  _ON_ZEROGPU = bool(os.environ.get("SPACES_ZERO_GPU"))
46
  DEVICE = "cuda" if (_ON_ZEROGPU or torch.cuda.is_available()) else "cpu"
47
  try:
48
- from byo_features import BYOExtractor
49
  EXTRACTOR = BYOExtractor(BASE_MODEL, device=DEVICE)
50
  _LOAD_ERR = None
51
  except Exception as e:
@@ -136,6 +136,12 @@ def predict(text, clf_name):
136
  return "Please enter some input text first.", None
137
  if EXTRACTOR is None:
138
  return f"⚠️ Base model unavailable: {_LOAD_ERR}", None
 
 
 
 
 
 
139
  try:
140
  feats = _compute_features(text)
141
  except ValueError as e:
 
45
  _ON_ZEROGPU = bool(os.environ.get("SPACES_ZERO_GPU"))
46
  DEVICE = "cuda" if (_ON_ZEROGPU or torch.cuda.is_available()) else "cpu"
47
  try:
48
+ from byo_features import BYOExtractor, WINDOW_LEN
49
  EXTRACTOR = BYOExtractor(BASE_MODEL, device=DEVICE)
50
  _LOAD_ERR = None
51
  except Exception as e:
 
136
  return "Please enter some input text first.", None
137
  if EXTRACTOR is None:
138
  return f"⚠️ Base model unavailable: {_LOAD_ERR}", None
139
+ # length check in the main process (CPU tokenization); a ValueError raised inside the
140
+ # @spaces.GPU worker would not survive the process boundary as a catchable ValueError.
141
+ n_tokens = len(EXTRACTOR.tok(text, truncation=True, max_length=WINDOW_LEN)["input_ids"])
142
+ if n_tokens < WINDOW_LEN:
143
+ return (f"⚠️ The input should be at least {WINDOW_LEN} tokens (or about 75 words); "
144
+ f"you entered {n_tokens} tokens."), None
145
  try:
146
  feats = _compute_features(text)
147
  except ValueError as e: