-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeepml_cli.py
More file actions
284 lines (221 loc) · 9.07 KB
/
deepml_cli.py
File metadata and controls
284 lines (221 loc) · 9.07 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
#!/usr/bin/env python3
"""
Deep-ML CLI - Fetch all problems from api.deep-ml.com
Usage:
python deepml_cli.py [--output OUTPUT_DIR] [--start START_ID] [--delay SECONDS]
"""
import argparse
import base64
import json
import os
import sys
import time
from pathlib import Path
from typing import Optional
import requests
API_BASE_URL = "https://api.deep-ml.com"
DEFAULT_OUTPUT_DIR = "problems"
DEFAULT_DELAY = 0.5 # Delay between requests to be polite
class DeepMLCLI:
"""CLI tool to fetch problems from deep-ml.com API."""
def __init__(self, output_dir: str = DEFAULT_OUTPUT_DIR, delay: float = DEFAULT_DELAY):
self.output_dir = Path(output_dir)
self.delay = delay
self.session = requests.Session()
self.session.headers.update({
"Accept": "application/json",
"User-Agent": "DeepML-CLI/1.0"
})
# Create output directory if it doesn't exist
self.output_dir.mkdir(parents=True, exist_ok=True)
def fetch_problem(self, problem_id: int) -> Optional[dict]:
"""
Fetch a single problem by ID.
Returns:
dict: Problem data if found
None: If problem doesn't exist or request failed
"""
url = f"{API_BASE_URL}/fetch-problem"
params = {"problem_id": problem_id}
try:
response = self.session.get(url, params=params, timeout=30)
if response.status_code == 404:
return None
if response.status_code != 200:
print(f" [!] Unexpected status code: {response.status_code}")
return None
data = response.json()
# Empty JSON means problem doesn't exist
if not data or data == {}:
return None
return data
except requests.exceptions.RequestException as e:
print(f" [!] Request error: {e}")
return None
except json.JSONDecodeError as e:
print(f" [!] JSON decode error: {e}")
return None
def decode_base64_fields(self, problem: dict) -> dict:
"""Decode base64 encoded fields in the problem data."""
decoded = problem.copy()
# Fields that are typically base64 encoded
base64_fields = ["description", "learn_section", "solution", "tinygrad_starter_code"]
for field in base64_fields:
if field in decoded and decoded[field]:
try:
decoded[f"{field}_decoded"] = base64.b64decode(decoded[field]).decode("utf-8")
except Exception:
pass # Keep original if decoding fails
return decoded
def save_problem(self, problem: dict, problem_id: int) -> None:
"""Save problem to a JSON file."""
# Decode base64 fields for readability
decoded_problem = self.decode_base64_fields(problem)
filepath = self.output_dir / f"problem_{problem_id:04d}.json"
with open(filepath, "w", encoding="utf-8") as f:
json.dump(decoded_problem, f, indent=2, ensure_ascii=False)
def save_summary(self, problems: list[dict]) -> None:
"""Save a summary of all problems."""
summary = []
for p in problems:
summary.append({
"id": p.get("id"),
"title": p.get("title"),
"category": p.get("category"),
"difficulty": p.get("difficulty"),
})
filepath = self.output_dir / "problems_summary.json"
with open(filepath, "w", encoding="utf-8") as f:
json.dump(summary, f, indent=2, ensure_ascii=False)
print(f"\n[+] Saved summary to {filepath}")
def fetch_all_problems(self, start_id: int = 1) -> list[dict]:
"""
Fetch all problems starting from start_id.
Stops when encountering an empty response (problem doesn't exist).
"""
problems = []
current_id = start_id
consecutive_empty = 0
max_consecutive_empty = 3 # Stop after 3 consecutive empty responses
print(f"[*] Starting to fetch problems from ID {start_id}...")
print(f"[*] Output directory: {self.output_dir.absolute()}")
print("-" * 60)
while True:
print(f"[{current_id}] Fetching problem...", end=" ", flush=True)
problem = self.fetch_problem(current_id)
if problem is None:
print("NOT FOUND")
consecutive_empty += 1
if consecutive_empty >= max_consecutive_empty:
print(f"\n[!] {max_consecutive_empty} consecutive empty responses. Stopping.")
break
else:
consecutive_empty = 0
title = problem.get("title", "Unknown")
category = problem.get("category", "Unknown")
difficulty = problem.get("difficulty", "Unknown")
print(f"OK - {title[:40]}... [{category}] [{difficulty}]")
self.save_problem(problem, current_id)
problems.append(problem)
current_id += 1
# Be polite to the server
if self.delay > 0:
time.sleep(self.delay)
print("-" * 60)
print(f"[+] Fetched {len(problems)} problems total")
return problems
def display_problem(self, problem: dict) -> None:
"""Display a problem in a readable format."""
decoded = self.decode_base64_fields(problem)
print("\n" + "=" * 70)
print(f"Problem #{decoded.get('id', 'N/A')}: {decoded.get('title', 'Unknown')}")
print("=" * 70)
print(f"Category: {decoded.get('category', 'N/A')}")
print(f"Difficulty: {decoded.get('difficulty', 'N/A')}")
print(f"Likes: {decoded.get('likes', 0)} | Dislikes: {decoded.get('dislikes', 0)}")
print("-" * 70)
if "description_decoded" in decoded:
print("\nDescription:")
print(decoded["description_decoded"])
if "example" in decoded:
example = decoded["example"]
print("\nExample:")
print(f" Input: {example.get('input', 'N/A')}")
print(f" Output: {example.get('output', 'N/A')}")
if "reasoning" in example:
print(f" Reasoning: {example.get('reasoning', 'N/A')}")
if "starter_code" in decoded:
print("\nStarter Code:")
print("-" * 40)
print(decoded["starter_code"])
print("-" * 40)
print()
def main():
parser = argparse.ArgumentParser(
description="Deep-ML CLI - Fetch problems from api.deep-ml.com",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
# Fetch all problems starting from ID 1
python deepml_cli.py
# Fetch problems starting from ID 50
python deepml_cli.py --start 50
# Save to custom directory with faster polling
python deepml_cli.py --output my_problems --delay 0.2
# Fetch a single problem and display it
python deepml_cli.py --id 101 --display
"""
)
parser.add_argument(
"--output", "-o",
default=DEFAULT_OUTPUT_DIR,
help=f"Output directory for problem files (default: {DEFAULT_OUTPUT_DIR})"
)
parser.add_argument(
"--start", "-s",
type=int,
default=1,
help="Starting problem ID (default: 1)"
)
parser.add_argument(
"--delay", "-d",
type=float,
default=DEFAULT_DELAY,
help=f"Delay between requests in seconds (default: {DEFAULT_DELAY})"
)
parser.add_argument(
"--id", "-i",
type=int,
help="Fetch a single problem by ID"
)
parser.add_argument(
"--display",
action="store_true",
help="Display problem details (use with --id)"
)
args = parser.parse_args()
cli = DeepMLCLI(output_dir=args.output, delay=args.delay)
if args.id:
# Fetch a single problem
print(f"[*] Fetching problem #{args.id}...")
problem = cli.fetch_problem(args.id)
if problem:
if args.display:
cli.display_problem(problem)
else:
cli.save_problem(problem, args.id)
print(f"[+] Saved to {cli.output_dir / f'problem_{args.id:04d}.json'}")
else:
print(f"[!] Problem #{args.id} not found")
sys.exit(1)
else:
# Fetch all problems
problems = cli.fetch_all_problems(start_id=args.start)
if problems:
cli.save_summary(problems)
print(f"[+] All problems saved to {cli.output_dir.absolute()}")
else:
print("[!] No problems fetched")
sys.exit(1)
if __name__ == "__main__":
main()