Spaces:
Sleeping
Sleeping
siddhm11 commited on
Commit ·
cb4a1c8
1
Parent(s): 7106701
Phase 6: Add arxiv_ids export script (IDs file on HF model repo)
Browse files- .gitignore +0 -0
- scripts/export_arxiv_ids.py +138 -0
.gitignore
CHANGED
|
Binary files a/.gitignore and b/.gitignore differ
|
|
|
scripts/export_arxiv_ids.py
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Export all arXiv IDs from Turso DB to arxiv_ids.txt.
|
| 3 |
+
|
| 4 |
+
Uses the same Turso HTTP pipeline API as turso_svc.py.
|
| 5 |
+
Paginates with LIMIT/OFFSET to handle 1.6M rows.
|
| 6 |
+
|
| 7 |
+
Usage:
|
| 8 |
+
set TURSO_URL=libsql://...
|
| 9 |
+
set TURSO_DB_TOKEN=...
|
| 10 |
+
python scripts/export_arxiv_ids.py
|
| 11 |
+
"""
|
| 12 |
+
import os
|
| 13 |
+
import sys
|
| 14 |
+
import time
|
| 15 |
+
import httpx
|
| 16 |
+
|
| 17 |
+
BATCH_SIZE = 50_000 # rows per query (Turso handles this fine)
|
| 18 |
+
OUTPUT_FILE = os.path.join(os.path.dirname(__file__), "..", "arxiv_ids.txt")
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def get_turso_config():
|
| 22 |
+
url = os.getenv("TURSO_URL", "")
|
| 23 |
+
token = os.getenv("TURSO_DB_TOKEN", "")
|
| 24 |
+
if not url or not token:
|
| 25 |
+
print("ERROR: Set TURSO_URL and TURSO_DB_TOKEN environment variables.")
|
| 26 |
+
print(" Example:")
|
| 27 |
+
print(" set TURSO_URL=libsql://your-db.turso.io")
|
| 28 |
+
print(" set TURSO_DB_TOKEN=your-token")
|
| 29 |
+
sys.exit(1)
|
| 30 |
+
|
| 31 |
+
# Convert to HTTPS
|
| 32 |
+
if url.startswith("libsql://"):
|
| 33 |
+
url = "https://" + url[len("libsql://"):]
|
| 34 |
+
elif not url.startswith("https://"):
|
| 35 |
+
url = "https://" + url
|
| 36 |
+
|
| 37 |
+
return url.rstrip("/"), token
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def turso_query(url: str, token: str, sql: str, args: list = None) -> list[list]:
|
| 41 |
+
"""Execute a query via Turso HTTP pipeline API. Returns list of rows."""
|
| 42 |
+
stmt = {"sql": sql}
|
| 43 |
+
if args:
|
| 44 |
+
stmt["args"] = args
|
| 45 |
+
|
| 46 |
+
payload = {
|
| 47 |
+
"requests": [
|
| 48 |
+
{"type": "execute", "stmt": stmt},
|
| 49 |
+
{"type": "close"},
|
| 50 |
+
]
|
| 51 |
+
}
|
| 52 |
+
headers = {
|
| 53 |
+
"Authorization": f"Bearer {token}",
|
| 54 |
+
"Content-Type": "application/json",
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
resp = httpx.post(
|
| 58 |
+
f"{url}/v2/pipeline",
|
| 59 |
+
json=payload,
|
| 60 |
+
headers=headers,
|
| 61 |
+
timeout=30,
|
| 62 |
+
)
|
| 63 |
+
resp.raise_for_status()
|
| 64 |
+
data = resp.json()
|
| 65 |
+
|
| 66 |
+
# Parse response
|
| 67 |
+
result = data.get("results", [])
|
| 68 |
+
if not result:
|
| 69 |
+
return []
|
| 70 |
+
|
| 71 |
+
execute_result = result[0]
|
| 72 |
+
if execute_result.get("type") == "error":
|
| 73 |
+
raise RuntimeError(f"Turso error: {execute_result.get('error')}")
|
| 74 |
+
|
| 75 |
+
response = execute_result.get("response", {})
|
| 76 |
+
result_data = response.get("result", {})
|
| 77 |
+
rows = result_data.get("rows", [])
|
| 78 |
+
|
| 79 |
+
# Each row is a list of {"type": "text", "value": "..."} dicts
|
| 80 |
+
return [[col.get("value") for col in row] for row in rows]
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
def main():
|
| 84 |
+
url, token = get_turso_config()
|
| 85 |
+
|
| 86 |
+
# First, get total count
|
| 87 |
+
print("[export] Counting papers in Turso...")
|
| 88 |
+
count_rows = turso_query(url, token, "SELECT COUNT(*) FROM papers")
|
| 89 |
+
total = int(count_rows[0][0]) if count_rows else 0
|
| 90 |
+
print(f"[export] Found {total:,} papers")
|
| 91 |
+
|
| 92 |
+
if total == 0:
|
| 93 |
+
print("ERROR: No papers found. Check your Turso connection.")
|
| 94 |
+
sys.exit(1)
|
| 95 |
+
|
| 96 |
+
# Paginate and collect all IDs
|
| 97 |
+
all_ids = []
|
| 98 |
+
offset = 0
|
| 99 |
+
t0 = time.perf_counter()
|
| 100 |
+
|
| 101 |
+
while offset < total:
|
| 102 |
+
batch_start = time.perf_counter()
|
| 103 |
+
rows = turso_query(
|
| 104 |
+
url, token,
|
| 105 |
+
f"SELECT arxiv_id FROM papers LIMIT {BATCH_SIZE} OFFSET {offset}"
|
| 106 |
+
)
|
| 107 |
+
batch_ms = (time.perf_counter() - batch_start) * 1000
|
| 108 |
+
|
| 109 |
+
batch_ids = [row[0] for row in rows if row[0]]
|
| 110 |
+
all_ids.extend(batch_ids)
|
| 111 |
+
offset += BATCH_SIZE
|
| 112 |
+
|
| 113 |
+
pct = min(100, offset * 100 / total)
|
| 114 |
+
print(f"[export] {len(all_ids):>10,} / {total:,} ({pct:.0f}%) "
|
| 115 |
+
f"batch: {len(batch_ids):,} in {batch_ms:.0f}ms")
|
| 116 |
+
|
| 117 |
+
if len(rows) < BATCH_SIZE:
|
| 118 |
+
break # No more rows
|
| 119 |
+
|
| 120 |
+
elapsed = time.perf_counter() - t0
|
| 121 |
+
print(f"\n[export] Collected {len(all_ids):,} arXiv IDs in {elapsed:.1f}s")
|
| 122 |
+
|
| 123 |
+
# Write to file
|
| 124 |
+
output_path = os.path.abspath(OUTPUT_FILE)
|
| 125 |
+
with open(output_path, "w", encoding="utf-8") as f:
|
| 126 |
+
for aid in all_ids:
|
| 127 |
+
f.write(aid + "\n")
|
| 128 |
+
|
| 129 |
+
file_size_mb = os.path.getsize(output_path) / (1024 * 1024)
|
| 130 |
+
print(f"[export] Written to: {output_path}")
|
| 131 |
+
print(f"[export] File size: {file_size_mb:.1f} MB")
|
| 132 |
+
print(f"[export] Lines: {len(all_ids):,}")
|
| 133 |
+
print(f"\n✅ Done! Feed this file to the ML Intern's Script 1:")
|
| 134 |
+
print(f" python 01_fetch_citation_edges.py --corpus-file arxiv_ids.txt")
|
| 135 |
+
|
| 136 |
+
|
| 137 |
+
if __name__ == "__main__":
|
| 138 |
+
main()
|