comgen42 commited on
Commit
16466a0
路
verified 路
1 Parent(s): 85e7882

v0.2: Kodiak-v0.2-1B default; ZeroGPU (@spaces.GPU)

Browse files
Files changed (2) hide show
  1. app.py +25 -8
  2. requirements.txt +1 -0
app.py CHANGED
@@ -13,6 +13,24 @@ from kodiak_s1.hub import Kodiak
13
  # KODIAK_MODELS: comma-separated repo ids (first = default). KODIAK_MODEL still works for a single model.
14
  MODELS = [m.strip() for m in os.environ.get("KODIAK_MODELS", os.environ.get("KODIAK_MODEL", "cortex-agent-llc/kodiak-small-v2-preview")).split(",") if m.strip()]
15
  LOADED = {m: Kodiak.from_pretrained(m, token=os.environ.get("HF_TOKEN")) for m in MODELS}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  MODEL = MODELS[0]
17
  kodiak = LOADED[MODEL]
18
  # Bulk jobs (Categorize a list) always use the fast small model, so a 500-row batch doesn't tie up the CPU in accuracy mode.
@@ -126,7 +144,7 @@ def render_answers(answers: dict, questions: list, threshold: float) -> str:
126
 
127
 
128
  HERO = """<div class="k-hero">
129
- <div class="k-kicker">Open decision model 路 research preview</div>
130
  <h1>Kodiak</h1>
131
  <p class="k-pitch">Ask questions about any text and get an answer from <em>your</em> options, a confidence you can trust, or an honest
132
  "can't tell", in one fast pass. The fast first step before a person or an LLM.</p>
@@ -192,7 +210,7 @@ def run(state_text: str, questions_json: str, threshold: float, model: str = MOD
192
  try:
193
  state = json.loads(state_text) if state_text.strip()[:1] in "[{" else state_text
194
  questions = json.loads(questions_json)
195
- answers = LOADED.get(model, kodiak).decide(state, questions, null_threshold=threshold)
196
  except Exception as e: # show errors in the UI instead of a stack trace
197
  return f'<div class="k-err"><b>Couldn\'t run that:</b> {_esc(e)}</div>', {}, None, gr.update(choices=[], value=None)
198
  last = {"model": model, "state": state, "questions": questions, "threshold": threshold, "answers": answers}
@@ -325,10 +343,9 @@ def categorize(rows_text: str, file, cats_text: str, question: str, review_below
325
  return "**Add at least one row and two categories.**", [], None
326
  rows = rows[:MAX_ROWS]
327
  q = {"type": "choice", "id": "category", "text": question.strip() or "Which category fits best?", "labels": cats}
328
- model = FAST
329
- k = LOADED[model]
330
  t = time.perf_counter()
331
- out = k.answer([{"state": r, "questions": [q]} for r in rows])
332
  secs = time.perf_counter() - t
333
  table, n_review = [], 0
334
  for r, resp in zip(rows, out):
@@ -393,7 +410,7 @@ def game_answer(g: dict | None, want_txt: str, next_txt: str):
393
  return g, gr.update(), gr.update(), gr.update(), "Pick an answer for both questions.", "", gr.update()
394
  case = GAME_CASES[g["picks"][g["i"]]]
395
  qs = [q for q in case["questions"] if q["id"] in GAME_QS]
396
- k = LOADED.get(g["model"], kodiak).decide(case["state"], qs, null_threshold=1.0)
397
  rows, you_pts, bear_pts = [], 0, 0
398
  for qid, mine in (("want", want_txt), ("next", next_txt)):
399
  labels = _labels(case, qid)
@@ -426,8 +443,8 @@ def game_answer(g: dict | None, want_txt: str, next_txt: str):
426
 
427
  with gr.Blocks(title="Kodiak 路 open decision model") as demo:
428
  gr.HTML(HERO.replace("MODELS_LINKS", " 路 ".join(f'<a href="https://huggingface.co/{m}" target="_blank">{m.split("/")[-1]}</a>' for m in MODELS)))
429
- model = gr.Dropdown(MODELS, value=MODEL, label="Model for Decide and the game (xl-v2: most accurate; large-v2-ensemble = accuracy mode: most trustworthy "
430
- "\"can't tell\"; small: fastest)",
431
  visible=len(MODELS) > 1)
432
  with gr.Tab("Decide"):
433
  example = gr.Dropdown(list(EXAMPLES), value="Support ticket", label="Example")
 
13
  # KODIAK_MODELS: comma-separated repo ids (first = default). KODIAK_MODEL still works for a single model.
14
  MODELS = [m.strip() for m in os.environ.get("KODIAK_MODELS", os.environ.get("KODIAK_MODEL", "cortex-agent-llc/kodiak-small-v2-preview")).split(",") if m.strip()]
15
  LOADED = {m: Kodiak.from_pretrained(m, token=os.environ.get("HF_TOKEN")) for m in MODELS}
16
+
17
+ try: # ZeroGPU: a GPU is attached only while a @spaces.GPU function runs; off ZeroGPU the decorator does nothing
18
+ import spaces
19
+
20
+ gpu = spaces.GPU
21
+ except ImportError:
22
+ def gpu(fn=None, **_):
23
+ return fn if fn else (lambda f: f)
24
+
25
+
26
+ @gpu(duration=60)
27
+ def _decide(model: str, state, questions, **opts):
28
+ return LOADED.get(model, kodiak).decide(state, questions, **opts)
29
+
30
+
31
+ @gpu(duration=120)
32
+ def _answer(model: str, requests: list[dict]):
33
+ return LOADED.get(model, kodiak).answer(requests)
34
  MODEL = MODELS[0]
35
  kodiak = LOADED[MODEL]
36
  # Bulk jobs (Categorize a list) always use the fast small model, so a 500-row batch doesn't tie up the CPU in accuracy mode.
 
144
 
145
 
146
  HERO = """<div class="k-hero">
147
+ <div class="k-kicker">Open decision model 路 Kodiak-v0.2-1B</div>
148
  <h1>Kodiak</h1>
149
  <p class="k-pitch">Ask questions about any text and get an answer from <em>your</em> options, a confidence you can trust, or an honest
150
  "can't tell", in one fast pass. The fast first step before a person or an LLM.</p>
 
210
  try:
211
  state = json.loads(state_text) if state_text.strip()[:1] in "[{" else state_text
212
  questions = json.loads(questions_json)
213
+ answers = _decide(model, state, questions, null_threshold=threshold)
214
  except Exception as e: # show errors in the UI instead of a stack trace
215
  return f'<div class="k-err"><b>Couldn\'t run that:</b> {_esc(e)}</div>', {}, None, gr.update(choices=[], value=None)
216
  last = {"model": model, "state": state, "questions": questions, "threshold": threshold, "answers": answers}
 
343
  return "**Add at least one row and two categories.**", [], None
344
  rows = rows[:MAX_ROWS]
345
  q = {"type": "choice", "id": "category", "text": question.strip() or "Which category fits best?", "labels": cats}
346
+ model = MODEL # on ZeroGPU the default model is fast enough for a table
 
347
  t = time.perf_counter()
348
+ out = _answer(model, [{"state": r, "questions": [q]} for r in rows])
349
  secs = time.perf_counter() - t
350
  table, n_review = [], 0
351
  for r, resp in zip(rows, out):
 
410
  return g, gr.update(), gr.update(), gr.update(), "Pick an answer for both questions.", "", gr.update()
411
  case = GAME_CASES[g["picks"][g["i"]]]
412
  qs = [q for q in case["questions"] if q["id"] in GAME_QS]
413
+ k = _decide(g["model"], case["state"], qs, null_threshold=1.0)
414
  rows, you_pts, bear_pts = [], 0, 0
415
  for qid, mine in (("want", want_txt), ("next", next_txt)):
416
  labels = _labels(case, qid)
 
443
 
444
  with gr.Blocks(title="Kodiak 路 open decision model") as demo:
445
  gr.HTML(HERO.replace("MODELS_LINKS", " 路 ".join(f'<a href="https://huggingface.co/{m}" target="_blank">{m.split("/")[-1]}</a>' for m in MODELS)))
446
+ model = gr.Dropdown(MODELS, value=MODEL, label="Model for Decide and the game (v0.2-1b: the default; v0.2-1b-accuracy = accuracy mode: three models "
447
+ "averaged, most accurate and most trustworthy \"can't tell\", about 3脳 the compute)",
448
  visible=len(MODELS) > 1)
449
  with gr.Tab("Decide"):
450
  example = gr.Dropdown(list(EXAMPLES), value="Support ticket", label="Example")
requirements.txt CHANGED
@@ -1,2 +1,3 @@
1
  kodiak-s1[infer] @ git+https://github.com/grizzlypeaksoftware/kodiak@1cf20116581cf7d177f50e4f3cb37fec884965d0
2
  gradio==6.29.0
 
 
1
  kodiak-s1[infer] @ git+https://github.com/grizzlypeaksoftware/kodiak@1cf20116581cf7d177f50e4f3cb37fec884965d0
2
  gradio==6.29.0
3
+ spaces