File size: 20,583 Bytes
6e8f6f8 a2a8a67 6e8f6f8 a2a8a67 6e8f6f8 a2a8a67 6e8f6f8 a2a8a67 6e8f6f8 a2a8a67 6e8f6f8 a2a8a67 6e8f6f8 a2a8a67 6e8f6f8 264d760 6e8f6f8 264d760 6e8f6f8 264d760 6e8f6f8 264d760 6e8f6f8 264d760 6e8f6f8 a2a8a67 6e8f6f8 a2a8a67 6e8f6f8 a2a8a67 6e8f6f8 a2a8a67 6e8f6f8 a2a8a67 6e8f6f8 a2a8a67 6e8f6f8 a2a8a67 6e8f6f8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 |
import os
import re
import json
from pathlib import Path
from typing import Dict, List, Optional, Tuple, Any
import gradio as gr
# ----------------------
# Data loading utilities
# ----------------------
HERE = Path(__file__).parent
DATASOURCE_TXT = HERE / "datasource.txt"
STATIC_DATA_DIR = HERE / "static_data"
def _slugify(text: str) -> str:
text = text.strip().lower()
text = re.sub(r"[^a-z0-9\s_-]+", "", text)
text = re.sub(r"[\s_-]+", "-", text).strip("-")
return text or "uncategorized"
def _titleize_slug(slug: str) -> str:
return re.sub(r"[-_]+", " ", slug).title()
def _read_text(path: Path) -> Optional[str]:
try:
return path.read_text(encoding="utf-8")
except Exception:
return None
def _try_parse_json(text: str) -> Optional[dict]:
try:
return json.loads(text)
except Exception:
return None
def _try_parse_yaml(text: str) -> Optional[dict]:
try:
import yaml # type: ignore
return yaml.safe_load(text)
except Exception:
return None
def _extract_field(obj: dict, keys: List[str]) -> Optional[str]:
for k in keys:
if k in obj and isinstance(obj[k], str) and obj[k].strip():
return obj[k].strip()
# common nested forms, like { system: { content: "..." } } or messages: [{role: system, content: "..."}]
if "system" in obj and isinstance(obj["system"], dict):
for k in ("content", "prompt", "text"):
v = obj["system"].get(k)
if isinstance(v, str) and v.strip():
return v.strip()
if "messages" in obj and isinstance(obj["messages"], list):
for m in obj["messages"]:
if isinstance(m, dict) and m.get("role") == "system":
content = m.get("content")
if isinstance(content, str) and content.strip():
return content.strip()
return None
def _extract_agent_from_obj(obj: dict, fallback_category: str, source_path: Path) -> Optional[dict]:
# Heuristics to recognize agent-like configs.
raw_name = _extract_field(obj, ["name", "agent_name", "title", "id"])
description = _extract_field(obj, ["description", "desc", "about", "summary"])
system_prompt = _extract_field(
obj,
[
"system_prompt",
"prompt",
"instructions",
"system",
"system_instructions",
"system_text",
],
)
if not (raw_name and system_prompt):
return None
# Normalize the name for display
name = _titleize_slug(_slugify(raw_name))
category = (
_extract_field(obj, ["category", "group", "type"]) or fallback_category or "uncategorized"
)
category_slug = _slugify(category)
agent_id = _slugify(f"{category_slug}-{raw_name}")
return {
"id": agent_id,
"name": name,
"description": description or "",
"system_prompt": system_prompt,
"category": category_slug,
"source": str(source_path.relative_to(HERE)),
}
def _parse_markdown_frontmatter(text: str) -> Optional[dict]:
"""Parse YAML frontmatter from markdown files and include body content."""
if not text.startswith('---'):
return None
# Find the end of frontmatter
lines = text.split('\n')
end_idx = -1
for i, line in enumerate(lines[1:], 1):
if line.strip() == '---':
end_idx = i
break
if end_idx == -1:
return None
frontmatter = '\n'.join(lines[1:end_idx])
body = '\n'.join(lines[end_idx + 1:]).strip()
data = _try_parse_yaml(frontmatter)
if isinstance(data, dict) and body:
# Add the markdown body as system_prompt if not already present
if not data.get('system_prompt') and not data.get('prompt'):
data['system_prompt'] = body
return data
def _scan_static_data(root: Path) -> List[dict]:
agents: List[dict] = []
patterns = ["**/*.json", "**/*.yaml", "**/*.yml", "**/*.md"]
for pattern in patterns:
for fp in root.glob(pattern):
if not fp.is_file():
continue
text = _read_text(fp)
if not text:
continue
data = None
if fp.suffix.lower() == '.md':
data = _parse_markdown_frontmatter(text)
else:
data = _try_parse_json(text)
if data is None:
data = _try_parse_yaml(text)
if not isinstance(data, dict):
continue
# derive category from parent folder name as a fallback
fallback_category = fp.parent.name
agent = _extract_agent_from_obj(data, fallback_category, fp)
if agent:
agents.append(agent)
return agents
def _maybe_snapshot_download_from_hf(url: str, target_dir: Path) -> Optional[Path]:
# Attempt to fetch dataset to local static_data using huggingface_hub.
try:
from huggingface_hub import snapshot_download
# Accept full URL like https://huggingface.co/datasets/owner/name
m = re.match(r"https?://huggingface.co/datasets/([^/]+/[^/]+)", url.strip())
if not m:
return None
repo_id = m.group(1)
target_dir.mkdir(parents=True, exist_ok=True)
local_path = snapshot_download(repo_id=repo_id, repo_type="dataset")
# Mirror files into target_dir for predictable path
src = Path(local_path)
for p in src.rglob("*"):
if p.is_file():
rel = p.relative_to(src)
dest = target_dir / rel
dest.parent.mkdir(parents=True, exist_ok=True)
try:
dest.write_bytes(p.read_bytes())
except Exception:
pass
return target_dir
except Exception:
# No network or hub not installed; just skip
return None
def _parse_repo_id_from_url(url: str) -> Optional[str]:
m = re.match(r"https?://huggingface.co/datasets/([^/]+/[^/]+)", url.strip())
return m.group(1) if m else None
def _extract_agent_from_row(row: dict) -> Optional[dict]:
if not isinstance(row, dict):
return None
name = _extract_field(row, ["name", "agent_name", "title", "id"]) or row.get("name")
system_prompt = _extract_field(
row,
[
"system_prompt",
"prompt",
"instructions",
"system",
"system_instructions",
"system_text",
],
)
if not (name and system_prompt):
return None
description = _extract_field(row, ["description", "desc", "about", "summary"]) or ""
category = _extract_field(row, ["category", "group", "type"]) or "uncategorized"
category_slug = _slugify(category)
agent_id = _slugify(f"{category_slug}-{name}")
return {
"id": agent_id,
"name": name,
"description": description,
"system_prompt": system_prompt,
"category": category_slug,
"source": "hf-dataset-row",
}
def _maybe_load_hf_dataset_rows(url: str) -> Optional[List[dict]]:
try:
import datasets # type: ignore
repo_id = _parse_repo_id_from_url(url)
if not repo_id:
return None
# Try common splits; prefer train if present
result: List[dict] = []
loaded = datasets.load_dataset(repo_id)
if isinstance(loaded, dict):
split_order = ["train", "validation", "test"] + [k for k in loaded.keys() if k not in {"train", "validation", "test"}]
for split in split_order:
if split in loaded:
for row in loaded[split]:
a = _extract_agent_from_row(dict(row))
if a:
result.append(a)
else:
for row in loaded: # type: ignore
a = _extract_agent_from_row(dict(row))
if a:
result.append(a)
return result or None
except Exception:
return None
def load_agents() -> Tuple[Dict[str, Any], List[dict], List[str]]:
"""
Returns (catalog_by_category, agents, warnings)
catalog_by_category: {category_slug: {label: str, agents: [agent_id, ...]}}
agents: list of agent dicts
warnings: list of warning strings for UI
"""
warnings: List[str] = []
agents: List[dict] = []
# Resolve datasource
url = os.getenv("HF_DATASET_URL") or os.getenv("HF_DATASET_ID") or (_read_text(DATASOURCE_TXT) or "").strip()
# 1) Prefer local static_data if present
if STATIC_DATA_DIR.exists():
agents = _scan_static_data(STATIC_DATA_DIR)
# 2) Try to load dataset rows directly via datasets
if not agents and url:
maybe_agents = _maybe_load_hf_dataset_rows(url)
if maybe_agents:
agents = maybe_agents
# 3) If rows failed, snapshot the repo and scan files
if not agents and url:
maybe_dir = _maybe_snapshot_download_from_hf(url, STATIC_DATA_DIR)
if maybe_dir and maybe_dir.exists():
agents = _scan_static_data(maybe_dir)
else:
warnings.append(
"Dataset fetch unavailable. Add a local 'static_data' folder with agent configs."
)
if not url:
warnings.append("No datasource URL found; using fallback sample data.")
# 3) Fallback sample if nothing found
if not agents:
agents = [
{
"id": "code-assist-starter",
"name": "Code Assist Starter",
"description": "A simple code generation assistant for boilerplate tasks.",
"system_prompt": (
"You are a helpful coding agent. Generate concise, correct code and explain key steps."
),
"category": "code-assist",
"source": "sample",
},
{
"id": "docs-navigator",
"name": "Docs Navigator",
"description": "Answers questions using project docs and summarizes APIs.",
"system_prompt": (
"Act as a technical writer. Read provided docs and produce accurate, concise answers with citations when possible."
),
"category": "documentation",
"source": "sample",
},
]
warnings.append(
"Showing sample data. Add 'static_data' with JSON/YAML agent configs to replace."
)
# Dedupe by id, prefer first occurrence
deduped: Dict[str, dict] = {}
for a in agents:
if isinstance(a, dict) and a.get("id") and a["id"] not in deduped:
deduped[a["id"]] = a
agents = list(deduped.values())
# Build catalog
catalog: Dict[str, Dict[str, Any]] = {}
for a in agents:
cat = a.get("category") or "uncategorized"
cat_slug = _slugify(str(cat))
if cat_slug not in catalog:
catalog[cat_slug] = {"label": _titleize_slug(cat_slug), "agents": []}
catalog[cat_slug]["agents"].append(a["id"])
# Sort categories and agents by display name
catalog = dict(sorted(catalog.items(), key=lambda kv: kv[1]["label"]))
name_by_id = {a["id"]: a["name"] for a in agents}
for c in catalog.values():
c["agents"].sort(key=lambda aid: name_by_id.get(aid, aid).lower())
return catalog, agents, warnings
# ----------------------
# Gradio application
# ----------------------
def build_ui():
catalog, agents, warnings = load_agents()
agent_by_id = {a["id"]: a for a in agents}
# Initial selections
first_cat = next(iter(catalog.keys())) if catalog else None
first_agent = catalog[first_cat]["agents"][0] if first_cat and catalog[first_cat]["agents"] else None
def show_about():
"""Return About page content."""
about_content = """
# About Code-Gen-Agents-Network
This is a point-in-time network of code generation subagents created by **Daniel Rosehill**.
The network represents a curated collection of specialized AI agents designed for various coding and development tasks. Each agent has been configured with specific system prompts and capabilities to assist with different aspects of software development.
**Creator:** Daniel Rosehill
**Website:** [danielrosehill.com](https://danielrosehill.com)
**Dataset:** [Code-Gen-Agents-0925](https://huggingface.co/datasets/danielrosehill/Code-Gen-Agents-0925)
---
### Purpose
This network serves as a comprehensive resource for developers looking to leverage specialized AI agents for:
- Code generation and assistance
- Documentation and writing tasks
- Development workflow automation
- Deployment and infrastructure management
### Usage
Browse through the categories to find agents suited to your specific needs. Each agent includes:
- A detailed description of its capabilities
- The complete system prompt for implementation
- Source information for reference
Copy the system prompts to use these agents in your preferred AI interface or development environment.
### Data Source
The agent configurations are sourced from the [Code-Gen-Agents-0925 dataset](https://huggingface.co/datasets/danielrosehill/Code-Gen-Agents-0925) on Hugging Face, which contains the complete collection of specialized coding agents and their system prompts.
"""
return about_content
def on_category_select(evt: gr.SelectData):
"""Handle category selection from the category list."""
cat_slug = list(catalog.keys())[evt.index]
agents_in_cat = catalog[cat_slug]["agents"]
agent_choices = [agent_by_id[aid]["name"] for aid in agents_in_cat if aid in agent_by_id]
first_agent_in_cat = agents_in_cat[0] if agents_in_cat else None
# Update agent list and select first agent
agent_update = gr.update(choices=agent_choices, value=agent_choices[0] if agent_choices else None)
return agent_update, *on_agent_change(first_agent_in_cat)
def on_agent_select(evt: gr.SelectData):
"""Handle agent selection from the agent list."""
# Find which category is currently selected to get the right agent
for cat_slug, cat_data in catalog.items():
if evt.index < len(cat_data["agents"]):
agent_id = cat_data["agents"][evt.index]
return on_agent_change(agent_id)
return on_agent_change(None)
def on_agent_change(agent_id: Optional[str]):
if not agent_id or agent_id not in agent_by_id:
return (
"# Select an agent",
gr.update(value="", visible=True),
gr.update(value="", visible=True),
gr.update(value="", visible=False),
)
a = agent_by_id[agent_id]
header = f"# {a['name']}"
desc = a.get("description", "")
prompt = a.get("system_prompt", "")
src = a.get("source", "")
footer = f"**Source:** {src}" if src else ""
return (
header,
gr.update(value=desc, visible=True),
gr.update(value=prompt, visible=True),
gr.update(value=footer, visible=bool(footer)),
)
with gr.Blocks(title="Code Gen Agents Network", theme=gr.themes.Soft()) as demo:
# Tab interface for About and Main content
with gr.Tabs() as tabs:
with gr.TabItem("Agents Network", id="main"):
with gr.Row():
# Fixed-width left sidebar for categories
with gr.Column(scale=1, min_width=200):
gr.Markdown("### Categories")
category_list = gr.Radio(
choices=[catalog[slug]["label"] for slug in catalog.keys()],
value=catalog[first_cat]["label"] if first_cat else None,
label="",
interactive=True,
container=False
)
if warnings:
with gr.Accordion("⚠️ Notes", open=False):
gr.Markdown("\n".join(f"- {w}" for w in warnings))
# Main content area
with gr.Column(scale=4):
# Top horizontal bar for agents
gr.Markdown("### Agents")
agent_list = gr.Radio(
choices=[agent_by_id[aid]["name"] for aid in catalog[first_cat]["agents"]] if first_cat else [],
value=agent_by_id[first_agent]["name"] if first_agent and first_agent in agent_by_id else None,
label="",
interactive=True,
container=False
)
# Agent details below
md_header = gr.Markdown("# Select an agent")
tb_desc = gr.Textbox(
label="Description",
lines=3,
show_copy_button=True,
interactive=False
)
tb_prompt = gr.Textbox(
label="System Prompt",
lines=15,
show_copy_button=True,
interactive=False
)
md_footer = gr.Markdown(visible=False)
with gr.TabItem("About", id="about"):
about_markdown = gr.Markdown(show_about())
# Wire events
category_list.select(
on_category_select,
outputs=[agent_list, md_header, tb_desc, tb_prompt, md_footer]
)
# Handle agent selection - need to track current category
current_category = gr.State(first_cat)
def on_agent_radio_change(selected_agent_name, current_cat):
if not selected_agent_name or not current_cat:
return on_agent_change(None)
# Find agent ID by name in current category
for agent_id in catalog[current_cat]["agents"]:
if agent_id in agent_by_id and agent_by_id[agent_id]["name"] == selected_agent_name:
return on_agent_change(agent_id)
return on_agent_change(None)
def update_current_category(evt: gr.SelectData):
cat_slug = list(catalog.keys())[evt.index]
agents_in_cat = catalog[cat_slug]["agents"]
agent_choices = [agent_by_id[aid]["name"] for aid in agents_in_cat if aid in agent_by_id]
first_agent_in_cat = agents_in_cat[0] if agents_in_cat else None
return (
cat_slug,
gr.update(choices=agent_choices, value=agent_choices[0] if agent_choices else None),
*on_agent_change(first_agent_in_cat)
)
category_list.select(
update_current_category,
outputs=[current_category, agent_list, md_header, tb_desc, tb_prompt, md_footer]
)
agent_list.change(
on_agent_radio_change,
inputs=[agent_list, current_category],
outputs=[md_header, tb_desc, tb_prompt, md_footer]
)
# Initialize content
if first_agent:
header, desc_upd, prompt_upd, footer_upd = on_agent_change(first_agent)
md_header.value = header
tb_desc.value = desc_upd.value if hasattr(desc_upd, 'value') else ""
tb_prompt.value = prompt_upd.value if hasattr(prompt_upd, 'value') else ""
md_footer.value = footer_upd.value if hasattr(footer_upd, 'value') else ""
md_footer.visible = bool(footer_upd.value) if hasattr(footer_upd, 'value') else False
return demo
if __name__ == "__main__":
demo = build_ui()
demo.launch()
|