fix: ui.py — compatible with Gradio 6.x (remove show_copy_button, move theme to launch)"
Browse files- alpha_factory/ui.py +28 -86
alpha_factory/ui.py
CHANGED
|
@@ -14,7 +14,6 @@ DB_PATH = Path("factor_store/alphas.duckdb")
|
|
| 14 |
|
| 15 |
|
| 16 |
def get_alphas_from_db(limit=50):
|
| 17 |
-
"""Fetch alphas from DuckDB with all relevant info."""
|
| 18 |
if not DB_PATH.exists():
|
| 19 |
return []
|
| 20 |
conn = duckdb.connect(str(DB_PATH), read_only=True)
|
|
@@ -36,142 +35,85 @@ def get_alphas_from_db(limit=50):
|
|
| 36 |
LIMIT {limit}
|
| 37 |
""").fetchall()
|
| 38 |
return rows
|
| 39 |
-
except Exception
|
| 40 |
return []
|
| 41 |
finally:
|
| 42 |
conn.close()
|
| 43 |
|
| 44 |
|
| 45 |
-
def format_alphas_table():
|
| 46 |
-
"""Format alphas into a display-friendly list."""
|
| 47 |
-
rows = get_alphas_from_db()
|
| 48 |
-
if not rows:
|
| 49 |
-
return "No alphas generated yet. Run the pipeline first:\n\nuv run python -m alpha_factory.run --dry-run --batch-size 5"
|
| 50 |
-
|
| 51 |
-
output = []
|
| 52 |
-
for row in rows:
|
| 53 |
-
alpha_id, submitted_at, expression, theme, archetype, tag, neutral, decay, fields, verdict = row
|
| 54 |
-
timestamp = submitted_at.strftime("%Y-%m-%d %H:%M:%S") if submitted_at else "unknown"
|
| 55 |
-
fields_str = ", ".join(fields) if fields else "none"
|
| 56 |
-
verdict_emoji = {"promote": "✅", "iterate": "🔄", "kill": "❌"}.get(verdict or "", "⏳")
|
| 57 |
-
|
| 58 |
-
output.append(f"""{'='*80}
|
| 59 |
-
{verdict_emoji} Alpha: {alpha_id}
|
| 60 |
-
📅 Generated: {timestamp}
|
| 61 |
-
🏷️ Theme: {theme} | Archetype: {archetype} | Tag: {tag}
|
| 62 |
-
⚙️ Neutralization: {neutral} | Decay: {decay}
|
| 63 |
-
📊 Fields: {fields_str}
|
| 64 |
-
|
| 65 |
-
📝 EXPRESSION (copy below):
|
| 66 |
-
{expression}
|
| 67 |
-
{'='*80}
|
| 68 |
-
""")
|
| 69 |
-
|
| 70 |
-
return "\n".join(output)
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
def get_single_alpha(index: int):
|
| 74 |
-
"""Get a single alpha expression for easy copying."""
|
| 75 |
-
rows = get_alphas_from_db()
|
| 76 |
-
if not rows or index >= len(rows):
|
| 77 |
-
return "No alpha at this index"
|
| 78 |
-
return rows[index][2] # expression column
|
| 79 |
-
|
| 80 |
-
|
| 81 |
def get_alpha_cards():
|
| 82 |
-
"""Return alpha data as list of dicts for dataframe display."""
|
| 83 |
rows = get_alphas_from_db()
|
| 84 |
if not rows:
|
| 85 |
return []
|
| 86 |
-
|
| 87 |
data = []
|
| 88 |
for row in rows:
|
| 89 |
alpha_id, submitted_at, expression, theme, archetype, tag, neutral, decay, fields, verdict = row
|
| 90 |
timestamp = submitted_at.strftime("%Y-%m-%d %H:%M") if submitted_at else "?"
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
"ID": alpha_id[:8] + "...",
|
| 95 |
-
"Theme": theme or "",
|
| 96 |
-
"Archetype": archetype or "",
|
| 97 |
-
"Tag": tag or "",
|
| 98 |
-
"Decay": decay or 0,
|
| 99 |
-
"Status": verdict_emoji,
|
| 100 |
-
"Expression": expression[:100] + "..." if len(expression or "") > 100 else (expression or ""),
|
| 101 |
-
})
|
| 102 |
return data
|
| 103 |
|
| 104 |
|
| 105 |
-
def get_full_expression(evt: gr.SelectData
|
| 106 |
-
"""When user clicks a row, show the full expression."""
|
| 107 |
-
if evt.index is None:
|
| 108 |
-
return ""
|
| 109 |
rows = get_alphas_from_db()
|
| 110 |
-
if not rows:
|
| 111 |
return ""
|
| 112 |
row_idx = evt.index[0] if isinstance(evt.index, (list, tuple)) else evt.index
|
| 113 |
if row_idx < len(rows):
|
| 114 |
-
return rows[row_idx][2]
|
| 115 |
return ""
|
| 116 |
|
| 117 |
|
| 118 |
def run_batch_and_refresh(batch_size):
|
| 119 |
-
|
| 120 |
-
import subprocess
|
| 121 |
-
import sys
|
| 122 |
result = subprocess.run(
|
| 123 |
[sys.executable, "-m", "alpha_factory.run", "--dry-run", "--batch-size", str(int(batch_size))],
|
| 124 |
capture_output=True, text=True, encoding="utf-8", errors="replace"
|
| 125 |
)
|
| 126 |
-
|
|
|
|
| 127 |
|
| 128 |
|
| 129 |
def build_ui():
|
| 130 |
-
|
| 131 |
-
with gr.Blocks(title="Alpha Factory", theme=gr.themes.Soft()) as app:
|
| 132 |
gr.Markdown("""
|
| 133 |
# 🏭 Alpha Factory — Generated Alphas
|
| 134 |
View, copy, and manage alphas generated by the pipeline.
|
| 135 |
""")
|
| 136 |
-
|
| 137 |
with gr.Row():
|
| 138 |
with gr.Column(scale=1):
|
| 139 |
batch_size_input = gr.Number(value=5, label="Batch Size", minimum=1, maximum=20)
|
| 140 |
generate_btn = gr.Button("🚀 Generate New Batch (Dry Run)", variant="primary")
|
| 141 |
refresh_btn = gr.Button("🔄 Refresh Table")
|
| 142 |
with gr.Column(scale=3):
|
| 143 |
-
gr.Markdown("
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
alpha_table = gr.Dataframe(
|
| 149 |
value=get_alpha_cards(),
|
| 150 |
headers=["Time", "ID", "Theme", "Archetype", "Tag", "Decay", "Status", "Expression"],
|
| 151 |
interactive=False,
|
| 152 |
wrap=True,
|
| 153 |
)
|
| 154 |
-
|
| 155 |
-
gr.Markdown("### 📝 Full Expression
|
| 156 |
full_expr = gr.Textbox(
|
| 157 |
-
label="Full Expression
|
| 158 |
-
lines=
|
| 159 |
-
show_copy_button=True,
|
| 160 |
interactive=False,
|
| 161 |
)
|
| 162 |
-
|
| 163 |
-
gr.Markdown("### 📜 Pipeline
|
| 164 |
-
pipeline_log = gr.Textbox(label="Last Run
|
| 165 |
-
|
| 166 |
-
#
|
| 167 |
-
alpha_table.select(get_full_expression,
|
| 168 |
refresh_btn.click(lambda: get_alpha_cards(), outputs=[alpha_table])
|
| 169 |
-
generate_btn.click(
|
| 170 |
-
|
| 171 |
-
inputs=[batch_size_input],
|
| 172 |
-
outputs=[alpha_table, pipeline_log],
|
| 173 |
-
)
|
| 174 |
-
|
| 175 |
return app
|
| 176 |
|
| 177 |
|
|
|
|
| 14 |
|
| 15 |
|
| 16 |
def get_alphas_from_db(limit=50):
|
|
|
|
| 17 |
if not DB_PATH.exists():
|
| 18 |
return []
|
| 19 |
conn = duckdb.connect(str(DB_PATH), read_only=True)
|
|
|
|
| 35 |
LIMIT {limit}
|
| 36 |
""").fetchall()
|
| 37 |
return rows
|
| 38 |
+
except Exception:
|
| 39 |
return []
|
| 40 |
finally:
|
| 41 |
conn.close()
|
| 42 |
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
def get_alpha_cards():
|
|
|
|
| 45 |
rows = get_alphas_from_db()
|
| 46 |
if not rows:
|
| 47 |
return []
|
|
|
|
| 48 |
data = []
|
| 49 |
for row in rows:
|
| 50 |
alpha_id, submitted_at, expression, theme, archetype, tag, neutral, decay, fields, verdict = row
|
| 51 |
timestamp = submitted_at.strftime("%Y-%m-%d %H:%M") if submitted_at else "?"
|
| 52 |
+
verdict_str = {"promote": "PASS", "iterate": "PENDING", "kill": "FAIL"}.get(verdict or "", "NEW")
|
| 53 |
+
expr_preview = (expression[:100] + "...") if expression and len(expression) > 100 else (expression or "")
|
| 54 |
+
data.append([timestamp, alpha_id[:10], theme or "", archetype or "", tag or "", decay or 0, verdict_str, expr_preview])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
return data
|
| 56 |
|
| 57 |
|
| 58 |
+
def get_full_expression(evt: gr.SelectData):
|
|
|
|
|
|
|
|
|
|
| 59 |
rows = get_alphas_from_db()
|
| 60 |
+
if not rows or evt.index is None:
|
| 61 |
return ""
|
| 62 |
row_idx = evt.index[0] if isinstance(evt.index, (list, tuple)) else evt.index
|
| 63 |
if row_idx < len(rows):
|
| 64 |
+
return rows[row_idx][2] or ""
|
| 65 |
return ""
|
| 66 |
|
| 67 |
|
| 68 |
def run_batch_and_refresh(batch_size):
|
| 69 |
+
import subprocess, sys
|
|
|
|
|
|
|
| 70 |
result = subprocess.run(
|
| 71 |
[sys.executable, "-m", "alpha_factory.run", "--dry-run", "--batch-size", str(int(batch_size))],
|
| 72 |
capture_output=True, text=True, encoding="utf-8", errors="replace"
|
| 73 |
)
|
| 74 |
+
log = result.stdout[-3000:] if result.stdout else result.stderr[-3000:]
|
| 75 |
+
return get_alpha_cards(), log
|
| 76 |
|
| 77 |
|
| 78 |
def build_ui():
|
| 79 |
+
with gr.Blocks(title="Alpha Factory") as app:
|
|
|
|
| 80 |
gr.Markdown("""
|
| 81 |
# 🏭 Alpha Factory — Generated Alphas
|
| 82 |
View, copy, and manage alphas generated by the pipeline.
|
| 83 |
""")
|
| 84 |
+
|
| 85 |
with gr.Row():
|
| 86 |
with gr.Column(scale=1):
|
| 87 |
batch_size_input = gr.Number(value=5, label="Batch Size", minimum=1, maximum=20)
|
| 88 |
generate_btn = gr.Button("🚀 Generate New Batch (Dry Run)", variant="primary")
|
| 89 |
refresh_btn = gr.Button("🔄 Refresh Table")
|
| 90 |
with gr.Column(scale=3):
|
| 91 |
+
gr.Markdown("Click any row below to see the **full expression** with copy support.")
|
| 92 |
+
|
| 93 |
+
gr.Markdown("### 📋 Generated Alphas")
|
| 94 |
+
|
|
|
|
| 95 |
alpha_table = gr.Dataframe(
|
| 96 |
value=get_alpha_cards(),
|
| 97 |
headers=["Time", "ID", "Theme", "Archetype", "Tag", "Decay", "Status", "Expression"],
|
| 98 |
interactive=False,
|
| 99 |
wrap=True,
|
| 100 |
)
|
| 101 |
+
|
| 102 |
+
gr.Markdown("### 📝 Full Expression — select all & copy (Ctrl+A, Ctrl+C)")
|
| 103 |
full_expr = gr.Textbox(
|
| 104 |
+
label="Full Expression",
|
| 105 |
+
lines=6,
|
|
|
|
| 106 |
interactive=False,
|
| 107 |
)
|
| 108 |
+
|
| 109 |
+
gr.Markdown("### 📜 Pipeline Log")
|
| 110 |
+
pipeline_log = gr.Textbox(label="Last Run Output", lines=12, interactive=False)
|
| 111 |
+
|
| 112 |
+
# Events
|
| 113 |
+
alpha_table.select(get_full_expression, outputs=[full_expr])
|
| 114 |
refresh_btn.click(lambda: get_alpha_cards(), outputs=[alpha_table])
|
| 115 |
+
generate_btn.click(run_batch_and_refresh, inputs=[batch_size_input], outputs=[alpha_table, pipeline_log])
|
| 116 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
return app
|
| 118 |
|
| 119 |
|