gaurv007 commited on
Commit
dfbfd81
Β·
verified Β·
1 Parent(s): ddf5d46

Upload alpha_factory/ui.py

Browse files
Files changed (1) hide show
  1. alpha_factory/ui.py +57 -72
alpha_factory/ui.py CHANGED
@@ -8,7 +8,6 @@ Run: uv run python -m alpha_factory.ui
8
  import os
9
  import sys
10
  import subprocess
11
- import json
12
  import asyncio
13
  import duckdb
14
  import gradio as gr
@@ -29,6 +28,7 @@ DB_PATH = Path("factor_store/alphas.duckdb")
29
  # ── Globals (shared across Gradio sessions) ──────────────────────────────────
30
  _LAST_DISCOVERED_MODELS: list[ModelInfo] = []
31
 
 
32
  def _model_choice_name(m: ModelInfo) -> str:
33
  """Human-readable label for a model in the dropdown."""
34
  size = f" ({m.size_gb:.1f}GB)" if m.size_gb else ""
@@ -43,7 +43,6 @@ def _discover_models_sync(
43
  """Synchronous wrapper around async model discovery."""
44
  global _LAST_DISCOVERED_MODELS
45
 
46
- # Resolve HF token
47
  token = hf_token or os.getenv("HF_TOKEN") or os.getenv("HUGGINGFACE_TOKEN", "")
48
 
49
  manager = ModelManager(ollama_url=ollama_url, hf_token=token)
@@ -57,12 +56,28 @@ def _discover_models_sync(
57
 
58
 
59
  def _get_dropdown_choices(models: list[ModelInfo]) -> list[str]:
60
- """Build dropdown choices: [Use Default (auto-assign)] + discovered models."""
61
  choices = ["Use Default (auto-assign)"]
62
  choices.extend([_model_choice_name(m) for m in models])
63
  return choices
64
 
65
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
  # ── DB helpers (unchanged) ──────────────────────────────────────────────────
67
 
68
  def get_alphas_from_db(limit=50):
@@ -117,7 +132,19 @@ def get_full_expression(evt: gr.SelectData):
117
  return ""
118
 
119
 
120
- # ── Pipeline runner (now with per-tier model overrides) ───────────────────────
 
 
 
 
 
 
 
 
 
 
 
 
121
 
122
  def _run_pipeline_subprocess(
123
  batch_size: int,
@@ -136,7 +163,6 @@ def _run_pipeline_subprocess(
136
  env["NO_COLOR"] = "1"
137
  env["TERM"] = "dumb"
138
 
139
- # Build CLI args
140
  cmd = [
141
  sys.executable, "-m", "alpha_factory.run",
142
  "--batch-size", str(int(batch_size)),
@@ -148,15 +174,6 @@ def _run_pipeline_subprocess(
148
  if enable_brain:
149
  cmd.append("--enable-brain")
150
 
151
- # Only pass per-tier overrides if user selected something other than default
152
- def _extract_model_name(choice: str) -> Optional[str]:
153
- if not choice or choice == "Use Default (auto-assign)":
154
- return None
155
- # Strip the [PROVIDER] prefix and size/quant suffix
156
- if "]" in choice:
157
- return choice.split("]", 1)[1].strip().split(" (")[0].split(" [")[0].strip()
158
- return choice.strip()
159
-
160
  mf = _extract_model_name(microfish)
161
  tf = _extract_model_name(tinyfish)
162
  mmf = _extract_model_name(mediumfish)
@@ -171,8 +188,7 @@ def _run_pipeline_subprocess(
171
  if bf:
172
  cmd.extend(["--bigfish", bf])
173
 
174
- # Log the command for debugging
175
- print(f"Running: {' '.join(cmd)}")
176
 
177
  try:
178
  result = subprocess.run(
@@ -217,23 +233,6 @@ def generate_and_refresh(
217
  return table, log
218
 
219
 
220
- # ── Model discovery refresh ──────────────────────────────────────────────────
221
-
222
- def refresh_model_list(ollama_url: str, hf_token: str) -> tuple[str, list[str]]:
223
- """Discover models and return (status_msg, dropdown_choices)."""
224
- models = _discover_models_sync(ollama_url=ollama_url, hf_token=hf_token)
225
-
226
- if not models:
227
- return "No models found. Is Ollama running? Is HF_TOKEN set?", ["Use Default (auto-assign)"]
228
-
229
- local_count = sum(1 for m in models if m.provider == ModelProvider.OLLAMA)
230
- cloud_count = sum(1 for m in models if m.provider == ModelProvider.HUGGINGFACE)
231
-
232
- msg = f"Found {local_count} Ollama + {cloud_count} HF models"
233
- choices = _get_dropdown_choices(models)
234
- return msg, choices
235
-
236
-
237
  # ── UI Builder ──────────────────────────────────────────────────────────────
238
 
239
  def build_ui():
@@ -251,11 +250,13 @@ def build_ui():
251
  ollama_url_input = gr.Textbox(
252
  value="http://localhost:11434",
253
  label="Ollama URL",
 
254
  )
255
  hf_token_input = gr.Textbox(
256
  value=os.getenv("HF_TOKEN", ""),
257
  label="HF Token (optional)",
258
  type="password",
 
259
  )
260
  refresh_models_btn = gr.Button("πŸ” Refresh Model List", variant="secondary")
261
  discovery_status = gr.Textbox(
@@ -267,63 +268,43 @@ def build_ui():
267
  with gr.Column(scale=2):
268
  gr.Markdown("### Model Selection β€” One Per Tier")
269
  gr.Markdown("""
270
- | Tier | Role | Typical Size |
271
- |------|------|-------------|
272
- | **Microfish** | Hypothesis generation (bulk) | 1.5B-3B |
273
- | **Tinyfish** | Expression compilation | 3B-7B |
274
- | **Mediumfish** | Crowd scout + Performance surgeon | 7B-14B |
275
- | **Bigfish** | Gatekeeper (final memo) | 14B-72B |
276
  """)
277
 
278
- # Initial choices: just default until discovery
279
  default_choices = ["Use Default (auto-assign)"]
280
 
281
  microfish_dropdown = gr.Dropdown(
282
  choices=default_choices,
283
  value="Use Default (auto-assign)",
284
- label="Microfish β€” Hypothesis Generation",
 
285
  )
286
  tinyfish_dropdown = gr.Dropdown(
287
  choices=default_choices,
288
  value="Use Default (auto-assign)",
289
- label="Tinyfish β€” Expression Compilation",
 
290
  )
291
  mediumfish_dropdown = gr.Dropdown(
292
  choices=default_choices,
293
  value="Use Default (auto-assign)",
294
- label="Mediumfish β€” Critique & Diagnosis",
 
295
  )
296
  bigfish_dropdown = gr.Dropdown(
297
  choices=default_choices,
298
  value="Use Default (auto-assign)",
299
- label="Bigfish β€” Final Gatekeeper",
 
300
  )
301
 
302
- # When refresh is clicked, update all dropdowns
303
- refresh_models_btn.click(
304
- fn=refresh_model_list,
305
- inputs=[ollama_url_input, hf_token_input],
306
- outputs=[discovery_status, microfish_dropdown],
307
- ).then(
308
- lambda choices: gr.Dropdown(choices=choices),
309
- inputs=microfish_dropdown,
310
- outputs=tinyfish_dropdown,
311
- ).then(
312
- lambda choices: gr.Dropdown(choices=choices),
313
- inputs=microfish_dropdown,
314
- outputs=mediumfish_dropdown,
315
- ).then(
316
- lambda choices: gr.Dropdown(choices=choices),
317
- inputs=microfish_dropdown,
318
- outputs=bigfish_dropdown,
319
- )
320
- # Actually the proper way: refresh returns one choices list,
321
- # then update all 4 dropdowns with that same list
322
- def _update_all_dropdowns(status, choices):
323
- return status, choices, choices, choices, choices
324
-
325
  refresh_models_btn.click(
326
- fn=lambda url, token: _update_all_dropdowns(*refresh_model_list(url, token)),
327
  inputs=[ollama_url_input, hf_token_input],
328
  outputs=[
329
  discovery_status,
@@ -342,13 +323,17 @@ def build_ui():
342
  value=3, label="Batch Size", minimum=1, maximum=20,
343
  )
344
  proven_mode_cb = gr.Checkbox(
345
- value=False, label="Proven Templates (no LLM)",
 
 
346
  )
347
  enable_brain_cb = gr.Checkbox(
348
- value=False, label="Enable BRAIN Submission (needs token)",
 
 
349
  )
350
  gr.Markdown("---")
351
- gr.Markdown("*Selected models carry over from the Settings tab*")
352
  generate_btn = gr.Button("Generate New Batch", variant="primary")
353
  refresh_table_btn = gr.Button("Refresh Table Only")
354
 
@@ -417,7 +402,7 @@ def build_ui():
417
  - Requires `BRAIN_SESSION_TOKEN` from browser devtools
418
  - Enable "BRAIN Submission" checkbox (disabled by default for safety)
419
 
420
- [GitHub / HuggingFace](https://huggingface.co/gaurv007/alpha-factory)
421
  """)
422
 
423
  return app
 
8
  import os
9
  import sys
10
  import subprocess
 
11
  import asyncio
12
  import duckdb
13
  import gradio as gr
 
28
  # ── Globals (shared across Gradio sessions) ──────────────────────────────────
29
  _LAST_DISCOVERED_MODELS: list[ModelInfo] = []
30
 
31
+
32
  def _model_choice_name(m: ModelInfo) -> str:
33
  """Human-readable label for a model in the dropdown."""
34
  size = f" ({m.size_gb:.1f}GB)" if m.size_gb else ""
 
43
  """Synchronous wrapper around async model discovery."""
44
  global _LAST_DISCOVERED_MODELS
45
 
 
46
  token = hf_token or os.getenv("HF_TOKEN") or os.getenv("HUGGINGFACE_TOKEN", "")
47
 
48
  manager = ModelManager(ollama_url=ollama_url, hf_token=token)
 
56
 
57
 
58
  def _get_dropdown_choices(models: list[ModelInfo]) -> list[str]:
59
+ """Build dropdown choices: [Use Default] + discovered models."""
60
  choices = ["Use Default (auto-assign)"]
61
  choices.extend([_model_choice_name(m) for m in models])
62
  return choices
63
 
64
 
65
+ def refresh_all_dropdowns(ollama_url: str, hf_token: str):
66
+ """Discover models and return status + 4 identical choice lists."""
67
+ models = _discover_models_sync(ollama_url=ollama_url, hf_token=hf_token)
68
+
69
+ if not models:
70
+ msg = "⚠️ No models found. Is Ollama running? Is HF_TOKEN set?"
71
+ return msg, ["Use Default (auto-assign)"], ["Use Default (auto-assign)"], ["Use Default (auto-assign)"], ["Use Default (auto-assign)"]
72
+
73
+ local_count = sum(1 for m in models if m.provider == ModelProvider.OLLAMA)
74
+ cloud_count = sum(1 for m in models if m.provider == ModelProvider.HUGGINGFACE)
75
+ msg = f"βœ… Found {local_count} Ollama + {cloud_count} HuggingFace models"
76
+
77
+ choices = _get_dropdown_choices(models)
78
+ return msg, choices, choices, choices, choices
79
+
80
+
81
  # ── DB helpers (unchanged) ──────────────────────────────────────────────────
82
 
83
  def get_alphas_from_db(limit=50):
 
132
  return ""
133
 
134
 
135
+ # ── Pipeline runner (with per-tier model overrides) ───────────────────────────
136
+
137
+ def _extract_model_name(choice: str) -> Optional[str]:
138
+ """Strip [PROVIDER] prefix and size/quant suffix to get raw model name."""
139
+ if not choice or choice == "Use Default (auto-assign)":
140
+ return None
141
+ if "]" in choice:
142
+ raw = choice.split("]", 1)[1].strip()
143
+ # Remove trailing size/quant suffixes: " (4.7GB) [q4_k_m]" etc.
144
+ raw = raw.split(" (")[0].split(" [")[0].strip()
145
+ return raw
146
+ return choice.strip()
147
+
148
 
149
  def _run_pipeline_subprocess(
150
  batch_size: int,
 
163
  env["NO_COLOR"] = "1"
164
  env["TERM"] = "dumb"
165
 
 
166
  cmd = [
167
  sys.executable, "-m", "alpha_factory.run",
168
  "--batch-size", str(int(batch_size)),
 
174
  if enable_brain:
175
  cmd.append("--enable-brain")
176
 
 
 
 
 
 
 
 
 
 
177
  mf = _extract_model_name(microfish)
178
  tf = _extract_model_name(tinyfish)
179
  mmf = _extract_model_name(mediumfish)
 
188
  if bf:
189
  cmd.extend(["--bigfish", bf])
190
 
191
+ print(f"[UI] Running: {' '.join(cmd)}")
 
192
 
193
  try:
194
  result = subprocess.run(
 
233
  return table, log
234
 
235
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
236
  # ── UI Builder ──────────────────────────────────────────────────────────────
237
 
238
  def build_ui():
 
250
  ollama_url_input = gr.Textbox(
251
  value="http://localhost:11434",
252
  label="Ollama URL",
253
+ info="URL of your local Ollama instance",
254
  )
255
  hf_token_input = gr.Textbox(
256
  value=os.getenv("HF_TOKEN", ""),
257
  label="HF Token (optional)",
258
  type="password",
259
+ info="HuggingFace token for cloud model access",
260
  )
261
  refresh_models_btn = gr.Button("πŸ” Refresh Model List", variant="secondary")
262
  discovery_status = gr.Textbox(
 
268
  with gr.Column(scale=2):
269
  gr.Markdown("### Model Selection β€” One Per Tier")
270
  gr.Markdown("""
271
+ | Tier | Role | Typical Size | Suggested |
272
+ |------|------|-------------|-----------|
273
+ | **Microfish** | Hypothesis generation (bulk) | 1.5B–3B | `qwen2.5:1.5b` |
274
+ | **Tinyfish** | Expression compilation | 3B–7B | `qwen2.5:3b` |
275
+ | **Mediumfish** | Crowd scout + Performance surgeon | 7B–14B | `qwen2.5:7b` |
276
+ | **Bigfish** | Gatekeeper (final memo) | 14B–72B | `qwen2.5:14b` |
277
  """)
278
 
 
279
  default_choices = ["Use Default (auto-assign)"]
280
 
281
  microfish_dropdown = gr.Dropdown(
282
  choices=default_choices,
283
  value="Use Default (auto-assign)",
284
+ label="🐟 Microfish β€” Hypothesis Generation",
285
+ info="Bulk idea generation (fast, small model)",
286
  )
287
  tinyfish_dropdown = gr.Dropdown(
288
  choices=default_choices,
289
  value="Use Default (auto-assign)",
290
+ label="🐠 Tinyfish β€” Expression Compilation",
291
+ info="Converts hypothesis to BRAIN expression",
292
  )
293
  mediumfish_dropdown = gr.Dropdown(
294
  choices=default_choices,
295
  value="Use Default (auto-assign)",
296
+ label="🦈 Mediumfish β€” Critique & Diagnosis",
297
+ info="Novelty check + performance analysis",
298
  )
299
  bigfish_dropdown = gr.Dropdown(
300
  choices=default_choices,
301
  value="Use Default (auto-assign)",
302
+ label="πŸ‹ Bigfish β€” Final Gatekeeper",
303
+ info="Production go/no-go decision (slowest, most capable)",
304
  )
305
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
306
  refresh_models_btn.click(
307
+ fn=refresh_all_dropdowns,
308
  inputs=[ollama_url_input, hf_token_input],
309
  outputs=[
310
  discovery_status,
 
323
  value=3, label="Batch Size", minimum=1, maximum=20,
324
  )
325
  proven_mode_cb = gr.Checkbox(
326
+ value=False,
327
+ label="Proven Templates (no LLM)",
328
+ info="Deterministic generation, guaranteed valid expressions",
329
  )
330
  enable_brain_cb = gr.Checkbox(
331
+ value=False,
332
+ label="Enable BRAIN Submission",
333
+ info="Requires BRAIN_SESSION_TOKEN env var",
334
  )
335
  gr.Markdown("---")
336
+ gr.Markdown("*Model selections carry over from the Settings tab*")
337
  generate_btn = gr.Button("Generate New Batch", variant="primary")
338
  refresh_table_btn = gr.Button("Refresh Table Only")
339
 
 
402
  - Requires `BRAIN_SESSION_TOKEN` from browser devtools
403
  - Enable "BRAIN Submission" checkbox (disabled by default for safety)
404
 
405
+ [Repository](https://huggingface.co/gaurv007/alpha-factory)
406
  """)
407
 
408
  return app