-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_skill.sh
More file actions
executable file
·196 lines (166 loc) · 6.41 KB
/
update_skill.sh
File metadata and controls
executable file
·196 lines (166 loc) · 6.41 KB
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
#!/bin/bash
# update_skill.sh - Update all MHub skill caches
#
# Usage: ./update_skill.sh
#
# This script updates:
# - Model list from MHub API
# - Default workflow configs from GitHub
# - SegDB segment database
#
# Requirements:
# - curl
# - Python 3.8+
# - pip install requests segdb
set -e
cd "$(dirname "$0")"
echo "╔════════════════════════════════════════════╗"
echo "║ MHub Segmentation Skill - Cache Update ║"
echo "╚════════════════════════════════════════════╝"
echo ""
# Check dependencies
command -v curl >/dev/null 2>&1 || { echo "Error: curl required"; exit 1; }
command -v python3 >/dev/null 2>&1 || { echo "Error: python3 required"; exit 1; }
# 1. Models cache
echo "┌─ Step 1: Fetching MHub models from API..."
curl -s "https://mhub.ai/api/v2/models/detailed" \
-H "accept: */*" \
-H "origin: https://mhub.ai" \
-H "referer: https://mhub.ai/" \
> data/models_cache.json
MODEL_COUNT=$(python3 -c "import json; print(len(json.load(open('data/models_cache.json'))['data']))")
echo "│ ✓ Fetched $MODEL_COUNT models"
# 2. Rebuild summary
echo "├─ Step 2: Rebuilding model index..."
python3 scripts/mhub_helper.py refresh 2>/dev/null || python3 << 'EOF'
import json
from datetime import datetime
with open('data/models_cache.json') as f:
raw = json.load(f)
models = raw['data']
summary = {
"cache_date": datetime.now().strftime("%Y-%m-%d"),
"source": "https://mhub.ai/api/v2/models/detailed",
"model_count": len(models),
"models": {},
"by_modality": {},
"by_segment": {},
"all_segments": []
}
all_segments = set()
for m in models:
name = m['name']
segments = m.get('segmentations', [])
modalities = m.get('modalities', [])
summary["models"][name] = {
"label": m.get('label', name),
"description": m.get('description', ''),
"modalities": modalities,
"segments": segments,
"segment_count": len(segments),
"predictions": m.get('predictions', []),
"category": m.get('categories', ['Unknown'])[0],
"license_code": m.get('licence', {}).get('model', 'Unknown'),
"license_weights": m.get('licence', {}).get('weights', 'Unknown'),
"cite": m.get('cite', ''),
"inputs": m.get('inputs', []),
"docker_image": f"mhubai/{name}:latest"
}
for mod in modalities:
summary["by_modality"].setdefault(mod, []).append(name)
for seg in segments:
all_segments.add(seg)
summary["by_segment"].setdefault(seg, []).append(name)
summary["all_segments"] = sorted(all_segments)
with open('data/models_summary.json', 'w') as f:
json.dump(summary, f, indent=2)
print(" Rebuilt index")
EOF
echo "│ ✓ Index rebuilt"
# 3. Fetch default configs
echo "├─ Step 3: Fetching default workflow configs..."
CONFIG_COUNT=0
python3 << 'PYEOF'
import json
import subprocess
from pathlib import Path
with open('data/models_summary.json') as f:
models = json.load(f)['models']
config_dir = Path('assets/workflow-templates/defaults')
config_dir.mkdir(parents=True, exist_ok=True)
count = 0
for name in models:
url = f'https://raw.githubusercontent.com/MHubAI/models/main/models/{name}/config/default.yml'
result = subprocess.run(['curl', '-s', '-f', url], capture_output=True, text=True)
if result.returncode == 0 and result.stdout.strip():
(config_dir / f'{name}.yml').write_text(result.stdout)
count += 1
print(count)
PYEOF
echo "│ ✓ Fetched configs for $MODEL_COUNT models"
# 4. Update SegDB
echo "├─ Step 4: Updating SegDB cache..."
# Try to install/upgrade segdb
pip install -q segdb --upgrade --break-system-packages 2>/dev/null || \
pip install -q segdb --upgrade 2>/dev/null || \
echo "│ ⚠ Could not upgrade segdb, using existing version"
python3 << 'PYEOF'
import json
from datetime import datetime
try:
from segdb.lookup import db
types_dict = db.types.to_dict('index')
categories_dict = db.categories.to_dict('index')
segments = {}
for seg_id, row in db.segmentations.iterrows():
color_parts = [int(c) for c in str(row.get('color', '128,128,128')).split(',')]
anat_region = row.get('anatomic_region', '')
type_info = types_dict.get(anat_region, {})
cat_id = row.get('category', '')
cat_info = categories_dict.get(cat_id, {})
segments[seg_id] = {
'name': row.get('name', seg_id),
'category': cat_id,
'category_code': cat_info.get('CodeValue', ''),
'category_scheme': cat_info.get('CodingSchemeDesignator', ''),
'category_meaning': cat_info.get('CodeMeaning', ''),
'type_id': anat_region,
'type_code': type_info.get('CodeValue', ''),
'type_scheme': type_info.get('CodingSchemeDesignator', ''),
'type_meaning': type_info.get('CodeMeaning', ''),
'modifier': row.get('modifier', None),
'color_rgb': color_parts
}
with open('data/segdb_cache.json', 'w') as f:
json.dump({
'cache_date': datetime.now().strftime('%Y-%m-%d'),
'source': 'segdb Python package',
'segment_count': len(segments),
'segments': segments,
'categories': db.categories.to_dict('index'),
'modifiers': db.modifiers.to_dict('index')
}, f, indent=2, default=str)
print(f"SEGDB_OK:{len(segments)}")
except ImportError:
print("SEGDB_SKIP:segdb not installed")
except Exception as e:
print(f"SEGDB_ERR:{e}")
PYEOF
echo "│ ✓ SegDB cache updated"
# 5. Summary
echo "└─ Step 5: Verification"
echo ""
python3 scripts/mhub_helper.py info
# Get today's date for reminder
TODAY=$(date +%Y-%m-%d)
echo ""
echo "╔════════════════════════════════════════════╗"
echo "║ Update Complete! ║"
echo "╚════════════════════════════════════════════╝"
echo ""
echo "Next steps:"
echo " 1. Update cache date in SKILL.md to: $TODAY"
echo " 2. Update cache date in README.md to: $TODAY"
echo " 3. Test: python scripts/mhub_helper.py models"
echo " 4. Rebuild ZIP: zip -r mhub-segmentation.zip ."
echo ""