-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
407 lines (383 loc) · 12 KB
/
Copy pathmain.py
File metadata and controls
407 lines (383 loc) · 12 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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
import argparse
import argcomplete
import logging
from pathlib import Path
from src.cli import (
run_crawl,
run_translate,
run_batch_evaluate,
run_batch_answer,
)
from src.utils.restructure import restructure_directories
from src.conf.config import settings
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(
description="Stack Overflow Collector: Crawl, Translate, and Evaluate."
)
parser.add_argument(
"--verbose",
action="store_true",
help="Enable debug logging for troubleshooting.",
)
subparsers = parser.add_subparsers(
dest="command", required=True, help="Command to execute"
)
# Crawl Command
crawl_parser = subparsers.add_parser(
"crawl", help="Fetch questions from Stack Overflow."
)
crawl_parser.add_argument(
"--tag",
default="kubernetes",
help="Stack Overflow tag to fetch (default: kubernetes).",
)
crawl_parser.add_argument(
"--limit",
type=int,
default=5,
help="Maximum number of questions to fetch (default: 5).",
)
crawl_parser.add_argument(
"--out-dir",
default="data",
type=Path,
help="Directory to save fetched data (default: data).",
)
crawl_parser.add_argument(
"--page-size",
type=int,
default=50,
help="Page size for Stack Exchange API calls (max 100).",
)
crawl_parser.add_argument(
"--stack-key",
default=settings.STACK_API_KEY,
help="Stack Exchange API key for increased quota.",
)
crawl_parser.add_argument(
"--workers",
type=int,
default=4,
help="Number of concurrent worker threads (default: 4).",
)
crawl_parser.add_argument(
"--checkpoint-file",
type=Path,
default=None,
help="Path to the checkpoint file for resuming crawls.",
)
crawl_parser.add_argument(
"--output-csv",
type=Path,
default=None,
help="Path to output CSV file for crawled questions.",
)
crawl_parser.add_argument(
"--force",
action="store_true",
help="Force re-crawl of existing content/ignore checkpoints.",
)
# Translate Command
translate_parser = subparsers.add_parser(
"translate", help="Translate content to Chinese (Q&A and Answers)."
)
translate_parser.add_argument(
"--out-dir",
default=None,
type=Path,
help="Input directory containing fetched content (Required).",
)
translate_parser.add_argument(
"--base-url",
dest="model_url", # Map to model_url for internal function if needed, or change internal
default=settings.LOCAL_MODEL_URL,
help="Base URL for the translation model API.",
)
translate_parser.add_argument(
"--api-key",
default=settings.OPENAI_API_KEY or "test-key",
help="API key for the translation model.",
)
translate_parser.add_argument(
"--workers",
type=int,
default=2,
help="Number of concurrent worker threads (default: 2).",
)
translate_parser.add_argument(
"--force",
action="store_true",
help="Force re-translation of existing files.",
)
translate_parser.add_argument(
"--limit",
type=int,
default=None,
help="Limit the number of items to process.",
)
translate_parser.add_argument(
"--skip",
type=int,
default=0,
help="Skip the first N items.",
)
translate_parser.add_argument(
"--input-csv",
type=Path,
default=None,
help="Path to input CSV file containing questions (Alternative source).",
)
translate_parser.add_argument(
"--output-csv",
type=Path,
default=None,
help="Path to output CSV file for translation results.",
)
# Answer Command
answer_parser = subparsers.add_parser(
"answer", help="Generate answers for questions (Step 1)."
)
answer_parser.add_argument(
"--input-dir",
default=None,
type=Path,
help="Directory containing fetched content.",
)
answer_parser.add_argument(
"--model",
default=settings.DEFAULT_MODEL_ANSWER,
help="Model name to use for answer generation.",
)
answer_parser.add_argument(
"--base-url",
default=settings.OPENAI_BASE_URL,
help="Base URL for the model API.",
)
answer_parser.add_argument(
"--api-key",
default=settings.OPENAI_API_KEY,
help="API key for the model.",
)
answer_parser.add_argument(
"--workers",
type=int,
default=2,
help="Number of concurrent worker threads (default: 2).",
)
answer_parser.add_argument(
"--force",
action="store_true",
help="Force regeneration of existing answers.",
)
answer_parser.add_argument(
"--limit",
type=int,
default=None,
help="Limit the number of items to process.",
)
answer_parser.add_argument(
"--skip",
type=int,
default=0,
help="Skip the first N items.",
)
answer_parser.add_argument(
"--input-csv",
type=Path,
default=None,
help="Path to input CSV file containing questions (Alternative source).",
)
answer_parser.add_argument(
"--output-csv",
type=Path,
default=None,
help="Path to output CSV file for results (Default: out_dir/results.csv).",
)
# Evaluate Command
eval_parser = subparsers.add_parser(
"evaluate", help="Evaluate generated answers (Step 2)."
)
eval_parser.add_argument(
"--input-dir",
default=None,
type=Path,
help="Directory containing data (Required unless --input-csv is used).",
)
eval_parser.add_argument(
"--model",
default=settings.DEFAULT_MODEL_ANSWER,
help="Model name used for the answer to evaluate (also attempts to evaluate using this model).",
)
eval_parser.add_argument(
"--base-url",
default=settings.OPENAI_BASE_URL,
help="Base URL for the evaluation model API.",
)
eval_parser.add_argument(
"--api-key",
default=settings.OPENAI_API_KEY,
help="API key for the evaluation model.",
)
eval_parser.add_argument(
"--workers",
type=int,
default=2,
help="Number of concurrent worker threads (default: 2).",
)
eval_parser.add_argument(
"--force",
action="store_true",
help="Force re-evaluation of existing items.",
)
eval_parser.add_argument(
"--reverse",
action="store_true",
help="Process items in reverse order.",
)
eval_parser.add_argument(
"--no-reference",
action="store_true",
help="Compare LLM answers without using human answer as reference (only compare LLMs among themselves).",
)
eval_parser.add_argument(
"--limit",
type=int,
default=None,
help="Limit the number of items to process.",
)
eval_parser.add_argument(
"--skip",
type=int,
default=0,
help="Skip the first N items.",
)
eval_parser.add_argument(
"--input-csv",
type=Path,
default=None,
help="Path to input CSV file (for question context).",
)
eval_parser.add_argument(
"--output-csv",
type=Path,
default=None,
help="Path to output CSV file for results (Default: out_dir/results.csv).",
)
eval_parser.add_argument(
"--modules",
nargs="+",
choices=["lint", "coverage", "llm-eval", "compare", "all"],
default=["all"],
help="Evaluation modules to run. Options: lint, coverage, llm-eval, compare, all (default: all).",
)
# Restructure Command (Legacy/Utility)
restructure_parser = subparsers.add_parser(
"restructure", help="Restructure data directories"
)
restructure_parser.add_argument(
"--out-dir", default="data", type=Path, help="Directory to restructure."
)
restructure_parser.add_argument(
"--cleanup-old",
action="store_true",
help="Remove legacy files.",
)
argcomplete.autocomplete(parser)
return parser.parse_args()
def main() -> None:
args = parse_args()
# Setup colored logging
class ColoredFormatter(logging.Formatter):
"""Custom formatter with colors for different log levels."""
COLORS = {
"DEBUG": "\033[36m", # Cyan
"INFO": "\033[32m", # Green
"WARNING": "\033[33m", # Yellow
"ERROR": "\033[31m", # Red
"CRITICAL": "\033[35m", # Magenta
}
RESET = "\033[0m"
def format(self, record):
color = self.COLORS.get(record.levelname, self.RESET)
record.levelname = f"{color}{record.levelname}{self.RESET}"
record.name = f"\033[34m{record.name}{self.RESET}" # Blue for logger name
return super().format(record)
handler = logging.StreamHandler()
handler.setFormatter(ColoredFormatter("%(levelname)s %(name)s: %(message)s"))
root_logger = logging.getLogger()
root_logger.setLevel(logging.DEBUG if args.verbose else logging.INFO)
root_logger.addHandler(handler)
# Silence noisy libraries unless verbose
if not args.verbose:
logging.getLogger("httpx").setLevel(logging.WARNING)
logging.getLogger("httpcore").setLevel(logging.WARNING)
if args.command == "crawl":
run_crawl(
tag=args.tag,
limit=args.limit,
out_dir=args.out_dir,
stack_key=args.stack_key,
workers=args.workers,
page_size=args.page_size,
checkpoint_file=args.checkpoint_file,
output_csv=args.output_csv,
force=args.force,
)
elif args.command == "translate":
if not args.out_dir and not args.input_csv:
print("Error: must specify --out-dir or --input-csv.")
return
run_translate(
out_dir=args.out_dir or Path("data"),
model_url=args.model_url,
api_key=args.api_key,
workers=args.workers,
force=args.force,
limit=args.limit,
skip=args.skip,
input_csv=args.input_csv,
output_csv=args.output_csv,
)
elif args.command == "evaluate":
if not args.input_dir and not args.input_csv:
print("Error: must specify --input-dir or --input-csv.")
return
run_batch_evaluate(
out_dir=args.input_dir
or Path("data"), # Default to data ONLY if csv provided
model=args.model,
base_url=args.base_url,
api_key=args.api_key,
workers=args.workers,
force=args.force,
reverse=args.reverse,
limit=args.limit,
skip=args.skip,
input_csv=args.input_csv,
output_csv=args.output_csv,
modules=args.modules,
no_reference=args.no_reference,
)
elif args.command == "answer":
if not args.input_dir and not args.input_csv:
print("Error: must specify --input-dir or --input-csv.")
return
run_batch_answer(
input_dir=args.input_dir or Path("data"),
model=args.model,
base_url=args.base_url,
api_key=args.api_key,
workers=args.workers,
force=args.force,
limit=args.limit,
skip=args.skip,
input_csv=args.input_csv,
output_csv=args.output_csv,
)
elif args.command == "restructure":
restructure_directories(args.input_csv)
if args.cleanup_old:
from src.utils.restructure import cleanup_extra_files
cleanup_extra_files(args.input_csv)
if __name__ == "__main__":
main()