DelaliScratchwerk commited on
Commit
6cbec58
Β·
verified Β·
1 Parent(s): 6c7d4c0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -11
app.py CHANGED
@@ -3,17 +3,14 @@ from setfit import SetFitModel
3
  from huggingface_hub import hf_hub_download
4
  from evidence import extract_evidence
5
 
6
- UNCERTAINTY_THRESHOLD = 0.516
7
- MARGIN_THRESHOLD = 0.387
8
-
9
  MODEL_ID = "DelaliScratchwerk/text-period-setfit"
10
 
11
- # thresholds – tweak later with validation
12
  TOP_K = 3
13
- UNCERTAINTY_THRESHOLD = 0.42 # if top1 prob below this β†’ "uncertain"
14
- MARGIN_THRESHOLD = 0.08 # or if (top1 - top2) < this β†’ "uncertain"
15
 
16
- # Load labels: try from model repo, else local labels.json in the Space
17
  try:
18
  labels_path = hf_hub_download(MODEL_ID, "labels.json")
19
  LABELS = json.load(open(labels_path))
@@ -40,20 +37,21 @@ def predict(txt: str):
40
  return "β€”", f"Label mismatch: model has {probs.size} classes, labels.json has {len(LABELS)}", {}
41
 
42
  order = np.argsort(probs)[::-1]
43
- top1, top2 = probs[order[0]], probs[order[1]] if probs.size > 1 else 0.0
 
44
  ev = extract_evidence(txt)
45
 
46
  # uncertain mode
47
  if top1 < UNCERTAINTY_THRESHOLD or (top1 - top2) < MARGIN_THRESHOLD:
48
- topk = [{ "label": LABELS[i], "score": float(probs[i]) } for i in order[:TOP_K]]
49
- md = "**Uncertain** β€” here are the top candidates:\n" + "\n".join(
50
  [f"- **{d['label']}**: {d['score']:.3f}" for d in topk]
51
  )
52
  return "uncertain", md + "\n\n" + format_evidence(ev), {LABELS[i]: float(probs[i]) for i in order}
53
 
54
  # confident
55
  best = LABELS[order[0]]
56
- md = f"**Reasoning hints**\n\n" + format_evidence(ev)
57
  return best, md, {LABELS[i]: float(probs[i]) for i in order}
58
 
59
  with gr.Blocks(title="Text β†’ Time Period (SetFit)") as demo:
 
3
  from huggingface_hub import hf_hub_download
4
  from evidence import extract_evidence
5
 
 
 
 
6
  MODEL_ID = "DelaliScratchwerk/text-period-setfit"
7
 
8
+ # ---- thresholds (use your tuned values)
9
  TOP_K = 3
10
+ UNCERTAINTY_THRESHOLD = 0.516 # from tune_thresholds.py
11
+ MARGIN_THRESHOLD = 0.387 # from tune_thresholds.py
12
 
13
+ # ---- load labels (Hub -> local fallback)
14
  try:
15
  labels_path = hf_hub_download(MODEL_ID, "labels.json")
16
  LABELS = json.load(open(labels_path))
 
37
  return "β€”", f"Label mismatch: model has {probs.size} classes, labels.json has {len(LABELS)}", {}
38
 
39
  order = np.argsort(probs)[::-1]
40
+ top1 = probs[order[0]]
41
+ top2 = probs[order[1]] if probs.size > 1 else 0.0
42
  ev = extract_evidence(txt)
43
 
44
  # uncertain mode
45
  if top1 < UNCERTAINTY_THRESHOLD or (top1 - top2) < MARGIN_THRESHOLD:
46
+ topk = [{"label": LABELS[i], "score": float(probs[i])} for i in order[:TOP_K]]
47
+ md = "**Uncertain** β€” top candidates:\n" + "\n".join(
48
  [f"- **{d['label']}**: {d['score']:.3f}" for d in topk]
49
  )
50
  return "uncertain", md + "\n\n" + format_evidence(ev), {LABELS[i]: float(probs[i]) for i in order}
51
 
52
  # confident
53
  best = LABELS[order[0]]
54
+ md = "**Reasoning hints**\n\n" + format_evidence(ev)
55
  return best, md, {LABELS[i]: float(probs[i]) for i in order}
56
 
57
  with gr.Blocks(title="Text β†’ Time Period (SetFit)") as demo: