-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup_data.py
More file actions
97 lines (79 loc) · 2.66 KB
/
Copy pathsetup_data.py
File metadata and controls
97 lines (79 loc) · 2.66 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
#!/usr/bin/env python3
"""
PhoneSafety: One-click data setup.
Downloads the dataset from Hugging Face and organizes it into the expected structure.
Usage:
python3 setup_data.py
"""
import os
import sys
import zipfile
import urllib.request
from pathlib import Path
HF_REPO = "phonesafety-anon/PhoneSafety_Data"
HF_BASE = f"https://huggingface.co/datasets/{HF_REPO}/resolve/main"
FILES = [
"phonesafety_700.jsonl",
"phonesafety_700_minimal_protocol.jsonl",
"screenshots.zip",
]
DATA_DIR = Path(__file__).resolve().parent / "data"
SCREENSHOTS_DIR = DATA_DIR / "screenshots"
def download_file(url, dest):
"""Download a file with progress."""
print(f" Downloading: {dest.name}...", end=" ", flush=True)
try:
urllib.request.urlretrieve(url, dest)
size_mb = dest.stat().st_size / 1024 / 1024
print(f"OK ({size_mb:.1f} MB)")
except Exception as e:
print(f"FAILED: {e}")
sys.exit(1)
def main():
print("=" * 60)
print("PhoneSafety: Setting up dataset")
print("=" * 60)
# Create directories
DATA_DIR.mkdir(exist_ok=True)
SCREENSHOTS_DIR.mkdir(exist_ok=True)
# Download files
print(f"\nDownloading from: {HF_BASE}")
print(f"Saving to: {DATA_DIR}\n")
for fname in FILES:
dest = DATA_DIR / fname
if dest.exists():
print(f" Skipping (already exists): {fname}")
continue
url = f"{HF_BASE}/{fname}"
download_file(url, dest)
# Unzip screenshots
zip_path = DATA_DIR / "screenshots.zip"
if zip_path.exists() and not any(SCREENSHOTS_DIR.iterdir()):
print(f"\n Extracting screenshots...", end=" ", flush=True)
with zipfile.ZipFile(zip_path, "r") as zf:
zf.extractall(SCREENSHOTS_DIR)
num_files = len(list(SCREENSHOTS_DIR.iterdir()))
print(f"OK ({num_files} files)")
# Remove zip after extraction
zip_path.unlink()
print(f" Removed: screenshots.zip")
elif any(SCREENSHOTS_DIR.iterdir()):
print(f"\n Screenshots already extracted ({len(list(SCREENSHOTS_DIR.iterdir()))} files)")
# Verify
print("\n" + "=" * 60)
jsonl_path = DATA_DIR / "phonesafety_700.jsonl"
if jsonl_path.exists():
import json
with open(jsonl_path) as f:
count = sum(1 for line in f if line.strip())
num_imgs = len(list(SCREENSHOTS_DIR.iterdir()))
print(f"Setup complete!")
print(f" Cases: {count}")
print(f" Screenshots: {num_imgs}")
print(f" Data dir: {DATA_DIR}")
else:
print("ERROR: Setup failed - jsonl not found")
sys.exit(1)
print("=" * 60)
if __name__ == "__main__":
main()