fassabilf commited on
Commit
9b32004
·
verified ·
1 Parent(s): 24f1bf1

add build_parquet_upload.py

Browse files
Files changed (1) hide show
  1. build_parquet_upload.py +137 -0
build_parquet_upload.py ADDED
@@ -0,0 +1,137 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Build parquet dataset + upload semua ke HF Hub (folder upload, no rate limit).
3
+ """
4
+
5
+ import json
6
+ import os
7
+ import sys
8
+ from pathlib import Path
9
+
10
+ import pandas as pd
11
+ from huggingface_hub import HfApi, create_repo, repo_exists
12
+
13
+ REPO_ID = "fassabilf/ir2025-papers-text"
14
+ TEXTS_DIR = Path("texts_2025")
15
+ META_FILES = ["papers_metadata.json", "papers_metadata_extra.json"]
16
+ PARQUET_PATH = "ir2025_papers.parquet"
17
+
18
+
19
+ def build_parquet():
20
+ """Gabung metadata + full text jadi satu parquet."""
21
+ print("Building parquet...")
22
+
23
+ # Load all metadata
24
+ all_meta = []
25
+ for mf in META_FILES:
26
+ if Path(mf).exists():
27
+ with open(mf) as f:
28
+ all_meta.extend(json.load(f))
29
+
30
+ print(f" Metadata: {len(all_meta)} papers")
31
+
32
+ # Build text lookup: (venue, slug) -> full_text
33
+ # PDF filename format dari download.py: {idx:03d}_{slug}.pdf
34
+ # Txt filename: {idx:03d}_{slug}.txt (sama, cuma ekstensi beda)
35
+ import re as _re
36
+ texts_map = {} # (venue, slug) -> text
37
+ txt_count = 0
38
+ for txt_path in sorted(TEXTS_DIR.rglob("*.txt")):
39
+ if txt_path.name == "all_papers.jsonl":
40
+ continue
41
+ try:
42
+ text = txt_path.read_text(encoding="utf-8")
43
+ venue = txt_path.parent.name
44
+ # Extract slug from filename: 001_some_title.txt -> some_title
45
+ name = txt_path.stem # tanpa .txt
46
+ slug = _re.sub(r'^\d{3}_', '', name) # hapus prefix 001_
47
+ texts_map[(venue, slug)] = text
48
+ txt_count += 1
49
+ except Exception:
50
+ pass
51
+
52
+ print(f" Texts : {txt_count} .txt files loaded")
53
+
54
+ # Build rows — match metadata ke text via slug
55
+ rows = []
56
+ matched = 0
57
+ for m in all_meta:
58
+ title = m.get("title", "")
59
+ venue = m.get("venue", "")
60
+ # Generate slug sama persis kaya download.py safe_filename
61
+ clean = "".join(c if c.isalnum() or c in " -_" else "" for c in title)
62
+ slug = clean.strip().replace(" ", "_")[:60]
63
+ full_text = texts_map.get((venue, slug), "")
64
+ if full_text:
65
+ matched += 1
66
+
67
+ rows.append({
68
+ "venue": venue,
69
+ "title": title,
70
+ "authors": "; ".join(m.get("authors", [])) if isinstance(m.get("authors"), list) else m.get("authors", ""),
71
+ "year": m.get("year", 2025),
72
+ "doi": m.get("doi", ""),
73
+ "source": m.get("source", ""),
74
+ "pdf_url": m.get("pdf_url", ""),
75
+ "abstract": m.get("abstract", ""),
76
+ "full_text": full_text,
77
+ })
78
+
79
+ print(f" Matched : {matched} texts → metadata")
80
+
81
+ df = pd.DataFrame(rows)
82
+ df.to_parquet(PARQUET_PATH, compression="zstd", index=False)
83
+
84
+ size_mb = Path(PARQUET_PATH).stat().st_size / (1024 * 1024)
85
+ print(f" Parquet : {PARQUET_PATH} ({size_mb:.1f} MB, {len(df)} rows)")
86
+ print(f" Columns : {list(df.columns)}")
87
+ print(f" With text: {(df['full_text'].str.len() > 0).sum()} papers")
88
+ return df
89
+
90
+
91
+ def upload_all():
92
+ """Upload parquet + texts folder + metadata ke HF Hub."""
93
+ token = os.environ.get("HF_TOKEN") or os.environ.get("HUGGINGFACE_HUB_TOKEN")
94
+ if not token:
95
+ print("[ERROR] HF_TOKEN not set")
96
+ sys.exit(1)
97
+
98
+ api = HfApi()
99
+
100
+ # Buat repo kalo belum ada
101
+ if not repo_exists(REPO_ID, repo_type="dataset", token=token):
102
+ create_repo(REPO_ID, repo_type="dataset", token=token)
103
+ print(f"Repo created: {REPO_ID}")
104
+
105
+ # 1. Upload parquet file
106
+ if Path(PARQUET_PATH).exists():
107
+ print(f"\nUploading {PARQUET_PATH}...")
108
+ api.upload_file(
109
+ path_or_fileobj=PARQUET_PATH,
110
+ path_in_repo=PARQUET_PATH,
111
+ repo_id=REPO_ID,
112
+ repo_type="dataset",
113
+ token=token,
114
+ commit_message="add parquet dataset",
115
+ )
116
+ print(" ✓ parquet uploaded")
117
+
118
+ # 2. Upload metadata files saja (parquet udah include everything)
119
+ for mf in META_FILES:
120
+ if Path(mf).exists():
121
+ print(f"\nUploading {mf}...")
122
+ api.upload_file(
123
+ path_or_fileobj=mf,
124
+ path_in_repo=mf,
125
+ repo_id=REPO_ID,
126
+ repo_type="dataset",
127
+ token=token,
128
+ commit_message=f"add {mf}",
129
+ )
130
+ print(f" ✓ {mf} uploaded")
131
+
132
+ print(f"\nDone: https://huggingface.co/datasets/{REPO_ID}")
133
+
134
+
135
+ if __name__ == "__main__":
136
+ build_parquet()
137
+ upload_all()