From de459a66d75bcd8bdb9fa8691bc4d14c521f77e6 Mon Sep 17 00:00:00 2001 From: Kosma Dunikowski Date: Sat, 12 Oct 2024 11:27:21 +0200 Subject: [PATCH] significantly improve CLI --- src/quizgen/quizgen.py | 154 +++++------------- .../scripts/convert_questions_batch.py | 4 +- .../scripts/generate_embeddings_batch.py | 4 +- .../scripts/generate_questions_batch.py | 4 +- 4 files changed, 54 insertions(+), 112 deletions(-) diff --git a/src/quizgen/quizgen.py b/src/quizgen/quizgen.py index 2bd20e0..c38e785 100644 --- a/src/quizgen/quizgen.py +++ b/src/quizgen/quizgen.py @@ -1,128 +1,64 @@ -import argparse import subprocess import sys import os -def main(): - parser = argparse.ArgumentParser(description="AI-Powered Question Generation Tool") - subparsers = parser.add_subparsers(dest="command") - - # Generate Questions - generate_parser = subparsers.add_parser( - "generate", help="Generate questions from educational content" - ) - generate_parser.add_argument("--chapter-path", help="Path to the chapter file") - generate_parser.add_argument("--title", help="Title of the course/chapter") - generate_parser.add_argument("--output", help="Path to the output JSON file") - generate_parser.add_argument( - "--batch", action="store_true", help="Process multiple chapters in batch mode" - ) - generate_parser.add_argument( - "--input-dir", help="Path to the directory containing source files (batch mode)" - ) - generate_parser.add_argument( - "--output-dir", help="Path to the output directory for JSON files (batch mode)" - ) - generate_parser.add_argument( - "--file-filter", - default="*.md", - help='File filter for batch processing (e.g., "*.md")', +def print_help(): + print( + "Please provide a command. Available commands: generate, convert, embeddings, thin, anki" + ) + print("\nUsage:") + print(" quizgen [--batch] [subscript arguments]") + print("\nCommands:") + print(" generate Generate questions from educational content") + print(" convert Convert questions from JSON to CSV format") + print(" embeddings Generate embeddings for question comparison") + print(" thin Reduce similar questions using embeddings") + print(" anki Import questions into Anki") + print("\nOptions:") + print( + " --batch Enable batch processing mode for supported commands (generate, convert, embeddings)" ) - # Convert Questions - convert_parser = subparsers.add_parser( - "convert", help="Convert questions from JSON to CSV format" - ) - convert_parser.add_argument("--input-path", help="Path to the input JSON file") - convert_parser.add_argument("--output-path", help="Path to the output CSV file") - convert_parser.add_argument( - "--batch", action="store_true", help="Convert multiple JSON files in batch mode" - ) - convert_parser.add_argument( - "--input-dir", help="Path to the directory containing JSON files (batch mode)" - ) - convert_parser.add_argument( - "--output-dir", help="Path to the output directory for CSV files (batch mode)" - ) - # Generate Embeddings - embeddings_parser = subparsers.add_parser( - "embeddings", help="Generate embeddings for question comparison" - ) - embeddings_parser.add_argument("--input-path", help="Path to the input JSON file") - embeddings_parser.add_argument( - "--output-path", help="Path to the output embeddings file" - ) - embeddings_parser.add_argument( - "--batch", - action="store_true", - help="Generate embeddings for multiple files in batch mode", - ) - embeddings_parser.add_argument( - "--input-dir", help="Path to the directory containing JSON files (batch mode)" - ) - embeddings_parser.add_argument( - "--output-dir", - help="Path to the output directory for embeddings files (batch mode)", - ) - - # Thin Out Questions - thin_parser = subparsers.add_parser( - "thin", help="Reduce similar questions using embeddings" - ) - thin_parser.add_argument( - "--csv-dir", help="Path to the directory containing CSV files" - ) - thin_parser.add_argument( - "--embeddings-dir", help="Path to the directory containing embeddings" - ) - thin_parser.add_argument("--output-dir", help="Path to the output directory") - thin_parser.add_argument( - "--T", type=int, help="Target number of questions to retain" - ) - - # Import to Anki - anki_parser = subparsers.add_parser("anki", help="Import questions into Anki") - anki_parser.add_argument( - "--input-dir", help="Path to the directory containing CSV files" - ) - anki_parser.add_argument("--root-deck-name", help="Name of the root deck in Anki") - - args = parser.parse_args() +def main(): + if len(sys.argv) < 2: + print_help() + return + command = sys.argv[1] script_dir = os.path.join(os.path.dirname(__file__), "scripts") - if args.command == "generate": - if args.batch: - script_path = os.path.join(script_dir, "generate_questions_batch.py") - else: - script_path = os.path.join(script_dir, "generate_questions.py") - elif args.command == "convert": - if args.batch: - script_path = os.path.join(script_dir, "convert_questions_batch.py") - else: - script_path = os.path.join(script_dir, "convert_questions.py") - elif args.command == "embeddings": - if args.batch: - script_path = os.path.join(script_dir, "generate_embeddings_batch.py") - else: - script_path = os.path.join(script_dir, "generate_embeddings.py") - elif args.command == "thin": + if command == "generate": + script_path = ( + os.path.join(script_dir, "generate_questions_batch.py") + if "--batch" in sys.argv + else os.path.join(script_dir, "generate_questions.py") + ) + elif command == "convert": + script_path = ( + os.path.join(script_dir, "convert_questions_batch.py") + if "--batch" in sys.argv + else os.path.join(script_dir, "convert_questions.py") + ) + elif command == "embeddings": + script_path = ( + os.path.join(script_dir, "generate_embeddings_batch.py") + if "--batch" in sys.argv + else os.path.join(script_dir, "generate_embeddings.py") + ) + elif command == "thin": script_path = os.path.join(script_dir, "thin_out_questions.py") - elif args.command == "anki": + elif command == "anki": script_path = os.path.join(script_dir, "import_to_anki.py") else: - parser.print_help() + print_help() return - # Construct the command - cmd = [sys.executable, script_path] - for arg, value in vars(args).items(): - if ( - value is not None and arg != "command" and arg != "batch" - ): # Exclude command and batch - cmd.extend([f'--{arg.replace("_", "-")}', str(value)]) + # Construct the command with ALL arguments, except --batch + cmd = [sys.executable, script_path] + [ + arg for arg in sys.argv[2:] if arg != "--batch" + ] # Execute the subprocess subprocess.run(cmd, check=True) diff --git a/src/quizgen/scripts/convert_questions_batch.py b/src/quizgen/scripts/convert_questions_batch.py index c5dfbb4..4f3583d 100644 --- a/src/quizgen/scripts/convert_questions_batch.py +++ b/src/quizgen/scripts/convert_questions_batch.py @@ -2,6 +2,7 @@ import pathlib import subprocess import sys +import os from tqdm import tqdm @@ -24,8 +25,9 @@ def convert_questions(input_dir, output_dir): continue print(f"Processing file: {relative_path}") + script_path = os.path.join(os.path.dirname(__file__), "convert_questions.py") subprocess.run( - [sys.executable, "convert_questions.py", str(json_file), str(output_path)], + [sys.executable, script_path, str(json_file), str(output_path)], check=True, ) print(f"Finished processing file: {relative_path}") diff --git a/src/quizgen/scripts/generate_embeddings_batch.py b/src/quizgen/scripts/generate_embeddings_batch.py index c4635d3..5ba08b7 100644 --- a/src/quizgen/scripts/generate_embeddings_batch.py +++ b/src/quizgen/scripts/generate_embeddings_batch.py @@ -2,6 +2,7 @@ import pathlib import subprocess import sys +import os from tqdm import tqdm @@ -24,10 +25,11 @@ def generate_embeddings(input_dir, output_dir): continue print(f"Processing file: {relative_path}") + script_path = os.path.join(os.path.dirname(__file__), "generate_embeddings.py") subprocess.run( [ sys.executable, - "generate_embeddings.py", + script_path, "--input-path", str(json_file), "--output-path", diff --git a/src/quizgen/scripts/generate_questions_batch.py b/src/quizgen/scripts/generate_questions_batch.py index 5c1fbc3..2cc887d 100644 --- a/src/quizgen/scripts/generate_questions_batch.py +++ b/src/quizgen/scripts/generate_questions_batch.py @@ -2,6 +2,7 @@ import pathlib import subprocess import sys +import os from tqdm import tqdm @@ -26,10 +27,11 @@ def generate_questions( continue print(f"Processing chapter: {relative_path}") + script_path = os.path.join(os.path.dirname(__file__), "generate_questions.py") subprocess.run( [ sys.executable, - "generate_questions.py", + script_path, "--title", title, "--chapter-path",