-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathstartup-fetch.py
More file actions
348 lines (285 loc) · 11.2 KB
/
Copy pathstartup-fetch.py
File metadata and controls
348 lines (285 loc) · 11.2 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
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
"""
startup-fetch.py — SIDIX Auto-Knowledge Fetcher
Dijalankan setiap startup Windows (via Task Scheduler).
Fetch artikel umum dari Wikipedia, ArXiv, tech news → simpan ke corpus → reindex.
Topics sesuai persona SIDIX:
- MIGHAN: AI image gen, video gen, music gen, design tools
- TOARD: AI planning, productivity, project management
- FACH: Machine learning, NLP, computer science
- HAYFAR: Python, API, backend, DevOps
- INAN: General tech news, AI news Indonesia
Cara jalankan:
python startup-fetch.py
python startup-fetch.py --topics "AI image generation" "video generation AI"
python startup-fetch.py --dry-run (preview tanpa save)
"""
from __future__ import annotations
import argparse
import datetime
import hashlib
import json
import os
import re
import sys
import time
import urllib.error
import urllib.request
from pathlib import Path
# ── Config ───────────────────────────────────────────────────────────────────
CORPUS_DIR = Path(__file__).parent / "brain" / "public" / "sources" / "web_clips"
INDEX_CMD_VENV = Path(__file__).parent / "apps" / "brain_qa" / ".venv" / "Scripts" / "python.exe"
INDEX_CMD_FALLBACK = "python"
INDEX_MODULE = "brain_qa"
INDEX_SUBDIR = Path(__file__).parent / "apps" / "brain_qa"
MAX_ARTICLES_PER_RUN = 15
MAX_CHARS_PER_ARTICLE = 8000
REQUEST_DELAY = 2.0 # seconds between requests
USER_AGENT = "SIDIX-KnowledgeFetcher/1.0 (educational; non-commercial)"
# ── Wikipedia topics sesuai persona SIDIX ────────────────────────────────────
TOPICS = {
"AI_CORE": [
"Artificial intelligence",
"Large language model",
"Retrieval-augmented generation",
"Transformer (deep learning)",
"Generative adversarial network",
"Diffusion model",
"Reinforcement learning from human feedback",
"Prompt engineering",
],
"MIGHAN_CREATIVE": [
"Text-to-image model",
"AI-generated art",
"Stable Diffusion",
"Midjourney (software)",
"AI music generation",
"Video generation AI",
"Generative design",
"Neural style transfer",
],
"TOARD_PLANNING": [
"AI planning",
"Autonomous agent",
"Multi-agent system",
"Workflow automation",
"Project management software",
"Knowledge management",
],
"FACH_ACADEMIC": [
"Natural language processing",
"Machine learning",
"Deep learning",
"Computer vision",
"Information retrieval",
"Semantic search",
],
"HAYFAR_TECHNICAL": [
"Application programming interface",
"FastAPI",
"Python (programming language)",
"Docker (software)",
"Microservices",
"Vector database",
],
"GENERAL_TECH": [
"Artificial general intelligence",
"OpenAI",
"Anthropic",
"Google DeepMind",
"Open-source software",
"Cloud computing",
],
}
# Flatten dengan semua topics
ALL_TOPICS = [t for group in TOPICS.values() for t in group]
# ── Wikipedia API ─────────────────────────────────────────────────────────────
def fetch_wikipedia(title: str, lang: str = "en") -> dict | None:
"""Fetch Wikipedia article summary via REST API."""
title_encoded = title.replace(" ", "_")
url = f"https://{lang}.wikipedia.org/api/rest_v1/page/summary/{urllib.request.quote(title_encoded)}"
try:
req = urllib.request.Request(url, headers={"User-Agent": USER_AGENT})
with urllib.request.urlopen(req, timeout=15) as resp:
if resp.status == 200:
data = json.loads(resp.read().decode())
return {
"title": data.get("title", title),
"description": data.get("description", ""),
"extract": data.get("extract", ""),
"url": data.get("content_urls", {}).get("desktop", {}).get("page", ""),
"lang": lang,
"source": "wikipedia",
}
except Exception as e:
print(f" ⚠ Wikipedia fetch failed: {title} → {e}")
return None
def fetch_wikipedia_sections(title: str, lang: str = "en") -> str:
"""Fetch fuller Wikipedia article content (intro section only)."""
title_encoded = title.replace(" ", "_")
url = f"https://{lang}.wikipedia.org/w/api.php"
params = urllib.parse.urlencode({
"action": "query",
"prop": "extracts",
"exintro": "1",
"explaintext": "1",
"titles": title,
"format": "json",
"exsentences": 30,
})
full_url = f"{url}?{params}"
try:
import urllib.parse
req = urllib.request.Request(full_url, headers={"User-Agent": USER_AGENT})
with urllib.request.urlopen(req, timeout=15) as resp:
data = json.loads(resp.read().decode())
pages = data.get("query", {}).get("pages", {})
for page in pages.values():
return page.get("extract", "")
except Exception:
pass
return ""
# ── Markdown writer ───────────────────────────────────────────────────────────
def article_to_markdown(article: dict, full_text: str = "") -> str:
"""Convert fetched article to Markdown for corpus."""
title = article.get("title", "Unknown")
description = article.get("description", "")
extract = full_text or article.get("extract", "")
url = article.get("url", "")
source = article.get("source", "web")
today = datetime.date.today().isoformat()
# Truncate
if len(extract) > MAX_CHARS_PER_ARTICLE:
extract = extract[:MAX_CHARS_PER_ARTICLE] + "\n\n[... truncated for corpus size ...]"
md = f"""# {title}
> {description}
**Source**: [{url}]({url})
**Fetched**: {today}
**Domain**: {source}
---
{extract}
---
*Auto-fetched by SIDIX startup-fetch.py — {today}*
"""
return md
def slugify(title: str) -> str:
"""Convert title to safe filename."""
s = title.lower()
s = re.sub(r"[^\w\s-]", "", s)
s = re.sub(r"[\s_-]+", "-", s)
s = s.strip("-")
return s[:80]
def get_filename(title: str, url: str) -> str:
"""Generate corpus filename matching existing convention."""
slug = slugify(title)
url_hash = hashlib.md5(url.encode()).hexdigest()[:8]
source_domain = "wikipedia"
return f"{slug}-{source_domain}-{url_hash}.md"
# ── Main fetch loop ───────────────────────────────────────────────────────────
def run_fetch(topics: list[str], dry_run: bool = False, verbose: bool = True) -> int:
"""Fetch articles and save to corpus. Returns count of new articles."""
import urllib.parse # ensure available
CORPUS_DIR.mkdir(parents=True, exist_ok=True)
# Existing files (avoid re-fetch)
existing = {f.stem for f in CORPUS_DIR.glob("*.md")}
fetched = 0
skipped = 0
today = datetime.date.today().isoformat()
print(f"🔍 Fetching {len(topics)} topics → {CORPUS_DIR}")
print(f" Existing files: {len(list(CORPUS_DIR.glob('*.md')))}")
print()
for i, topic in enumerate(topics[:MAX_ARTICLES_PER_RUN]):
if verbose:
print(f"[{i+1}/{min(len(topics), MAX_ARTICLES_PER_RUN)}] {topic}...")
article = fetch_wikipedia(topic)
if not article:
print(f" ✗ Not found")
continue
# Check if already exists (by slug)
slug = slugify(article["title"])
already_fetched = any(slug in ex for ex in existing)
if already_fetched:
if verbose:
print(f" → Already in corpus, skip")
skipped += 1
time.sleep(0.5)
continue
# Get fuller content
full_text = fetch_wikipedia_sections(topic)
# Write to corpus
md_content = article_to_markdown(article, full_text)
filename = get_filename(article["title"], article["url"])
filepath = CORPUS_DIR / filename
if dry_run:
print(f" [DRY RUN] Would write: {filename}")
print(f" Preview: {md_content[:200]}...")
else:
filepath.write_text(md_content, encoding="utf-8")
print(f" ✓ Saved: {filename}")
existing.add(slug)
fetched += 1
time.sleep(REQUEST_DELAY)
print()
print(f"✅ Done: {fetched} new, {skipped} skipped, {len(topics) - fetched - skipped} failed")
return fetched
def run_reindex() -> None:
"""Trigger brain_qa index rebuild."""
import subprocess
python_exe = str(INDEX_CMD_VENV) if INDEX_CMD_VENV.exists() else INDEX_CMD_FALLBACK
cwd = str(INDEX_SUBDIR)
print(f"\n🔄 Reindexing corpus...")
try:
result = subprocess.run(
[python_exe, "-m", INDEX_MODULE, "index"],
cwd=cwd,
capture_output=True,
text=True,
timeout=120,
)
if result.returncode == 0:
print("✅ Reindex complete!")
else:
print(f"⚠ Reindex exited {result.returncode}")
if result.stderr:
print(result.stderr[:500])
except Exception as e:
print(f"⚠ Reindex failed: {e}")
# ── CLI ───────────────────────────────────────────────────────────────────────
def main():
import urllib.parse # late import for Windows compat
parser = argparse.ArgumentParser(description="SIDIX Auto-Knowledge Fetcher")
parser.add_argument(
"--topics",
nargs="*",
help="Custom topics to fetch (overrides default list)",
)
parser.add_argument(
"--category",
choices=list(TOPICS.keys()) + ["ALL"],
default="ALL",
help="Fetch only topics from a specific persona category",
)
parser.add_argument("--dry-run", action="store_true", help="Preview only, don't save")
parser.add_argument("--no-reindex", action="store_true", help="Skip reindex after fetch")
parser.add_argument("--max", type=int, default=MAX_ARTICLES_PER_RUN, help="Max articles to fetch")
args = parser.parse_args()
global MAX_ARTICLES_PER_RUN
MAX_ARTICLES_PER_RUN = args.max
# Determine topics
if args.topics:
topics = args.topics
elif args.category == "ALL":
topics = ALL_TOPICS
else:
topics = TOPICS[args.category]
print("=" * 60)
print(" SIDIX Knowledge Fetcher — Auto-Startup")
print(f" {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
print("=" * 60)
print()
fetched = run_fetch(topics, dry_run=args.dry_run)
if fetched > 0 and not args.dry_run and not args.no_reindex:
run_reindex()
elif fetched == 0:
print("ℹ No new articles fetched, skipping reindex.")
if __name__ == "__main__":
main()