Skip to content

Commit

Permalink
Merge pull request #4 from kdunee/fix-cli
Browse files Browse the repository at this point in the history
significantly improve CLI
  • Loading branch information
kdunee authored Oct 12, 2024
2 parents ce12918 + de459a6 commit ef13460
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 112 deletions.
154 changes: 45 additions & 109 deletions src/quizgen/quizgen.py
Original file line number Diff line number Diff line change
@@ -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 <command> [--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)
Expand Down
4 changes: 3 additions & 1 deletion src/quizgen/scripts/convert_questions_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import pathlib
import subprocess
import sys
import os

from tqdm import tqdm

Expand All @@ -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}")
Expand Down
4 changes: 3 additions & 1 deletion src/quizgen/scripts/generate_embeddings_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import pathlib
import subprocess
import sys
import os

from tqdm import tqdm

Expand All @@ -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",
Expand Down
4 changes: 3 additions & 1 deletion src/quizgen/scripts/generate_questions_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import pathlib
import subprocess
import sys
import os

from tqdm import tqdm

Expand All @@ -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",
Expand Down

0 comments on commit ef13460

Please sign in to comment.