File size: 6,474 Bytes
4379b25
 
 
 
 
 
 
 
 
9e17db1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4379b25
 
 
 
 
9e17db1
 
 
4379b25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9887d83
 
 
 
 
4379b25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9887d83
 
 
 
 
 
 
 
 
4379b25
 
 
 
 
 
 
 
 
 
 
8d0fd38
 
4379b25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9887d83
4379b25
 
 
 
 
 
 
 
 
 
 
 
8d0fd38
4379b25
 
 
 
9887d83
4379b25
 
9887d83
 
4379b25
 
 
 
9887d83
4379b25
 
 
9887d83
4379b25
 
 
 
9887d83
4379b25
 
 
9887d83
4379b25
 
 
 
9887d83
4379b25
9887d83
4379b25
 
 
 
 
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
import argparse
import os
import re
import sys
from typing import List, Optional

from agent import BIDSifierAgent


def _read_pdf(path: str) -> str:
    """Extract text from a PDF file using pypdf."""
    try:
        from pypdf import PdfReader
    except ImportError as e:
        raise RuntimeError(
            "Reading PDFs requires the 'pypdf' package. Install it with: pip install pypdf"
        ) from e
    text_parts: List[str] = []
    with open(path, "rb") as f:
        reader = PdfReader(f)
        for i, page in enumerate(reader.pages):
            try:
                text = page.extract_text() or ""
            except Exception:
                text = ""
            if text.strip():
                # Add lightweight page markers to help the LLM
                text_parts.append(f"\n\n=== Page {i+1} ===\n{text.strip()}")
    return "\n".join(text_parts).strip()

def _read_optional(path: Optional[str]) -> Optional[str]:
    if not path:
        return None
    if not os.path.isfile(path):
        raise FileNotFoundError(f"File not found: {path}")
    ext = os.path.splitext(path)[1].lower()
    if ext == ".pdf":
        return _read_pdf(path)
    with open(path, "r", encoding="utf-8", errors="ignore") as f:
        return f.read()


def parse_commands_from_markdown(markdown: str) -> List[str]:
    """Extract the first bash/sh fenced code block and return one command per line."""
    pattern = re.compile(r"```(?:bash|sh)\n(.*?)```", re.DOTALL | re.IGNORECASE)
    m = pattern.search(markdown)
    if not m:
        return []
    block = m.group(1)
    commands: List[str] = []
    for raw in block.splitlines():
        line = raw.strip()
        if not line or line.startswith("#"):
            continue
        commands.append(line)
    return commands


def _print_commands(commands: List[str]) -> None:
    if not commands:
        print("(No commands detected in fenced bash block.)")
        return
    print("-----"*10)

    print("COMMANDS TO EXECUTE:")
    
    print("-----"*10)
    for c in commands:
        print(f"  {c}")


def prompt_yes_no(question: str, default: bool = False) -> bool:
    suffix = "[Y/n]" if default else "[y/N]"
    ans = input(f"{question} {suffix} ").strip().lower()
    if not ans:
        return default
    return ans in {"y", "yes"}


def short_divider(title: str) -> None:
    print("\n" + "=" * 80)
    print(title)
    print("=" * 80 + "\n")
    
def enter_feedback_loop(agent: BIDSifierAgent, context: dict) -> dict:
    feedback = input("\nAny comments or corrections to the summary? (press Enter to skip): ").strip()
    while feedback:
        context["user_feedback"] += feedback
        agent_response = agent.run_query(feedback)
        print(agent_response)
        feedback = input("\nAny additional comments or corrections? (press Enter to skip): ").strip()
    return context

def main(argv: Optional[List[str]] = None) -> int:
    parser = argparse.ArgumentParser(
        prog="bidsifier",
        description="Interactive LLM assistant to convert a dataset into BIDS via stepwise shell commands.",
        formatter_class=argparse.ArgumentDefaultsHelpFormatter,
    )
    parser.add_argument("--dataset-xml", dest="dataset_xml_path", help="Path to dataset structure XML", required=False)
    parser.add_argument("--readme", dest="readme_path", help="Path to dataset README file", required=False)
    parser.add_argument("--publication", dest="publication_path", help="Path to a publication/notes file", required=False)
    parser.add_argument("--output-root", dest="output_root", help="Target BIDS root directory", required=True)
    parser.add_argument("--provider", dest="provider", help="Provider name or identifier, default OpeanAI", required=False, default="openai")
    parser.add_argument("--model", dest="model", help="Model name to use", default=os.getenv("BIDSIFIER_MODEL", "gpt-4o-mini"))
    # Execution is intentionally disabled; we only display commands.
    # Keeping --dry-run for backward compatibility (no effect other than display).
    parser.add_argument("--dry-run", dest="dry_run", help="Display-only (default behavior)", action="store_true")

    args = parser.parse_args(argv)

    dataset_xml = _read_optional(args.dataset_xml_path)
    readme_text = _read_optional(args.readme_path)
    publication_text = _read_optional(args.publication_path)

    context = {
        "dataset_xml": dataset_xml,
        "readme_text": readme_text,
        "publication_text": publication_text,
        "output_root": args.output_root,
        "user_feedback": "",
    }

    command_env = {
        "OUTPUT_ROOT": args.output_root,
    }
    if args.dataset_xml_path:
        command_env["DATASET_XML_PATH"] = os.path.abspath(args.dataset_xml_path)
    if args.readme_path:
        command_env["README_PATH"] = os.path.abspath(args.readme_path)
    if args.publication_path:
        command_env["PUBLICATION_PATH"] = os.path.abspath(args.publication_path)

    agent = BIDSifierAgent(provider=args.provider, model=args.model)

    short_divider("Step 1: Understand dataset")
    summary = agent.run_step("summary", context)
    print(summary)
    context = enter_feedback_loop(agent, context)
    if not prompt_yes_no("Proceed to create BIDS root?", default=True):
        return 0
    
    short_divider("Step 2: Propose commands to create metadata files")
    meta_plan = agent.run_step("create_metadata", context)
    print(meta_plan)
    cmds = parse_commands_from_markdown(meta_plan)
    _print_commands(cmds)
    context = enter_feedback_loop(agent, context)
    if not prompt_yes_no("Proceed to create empty BIDS structure?", default=True):
        return 0

    short_divider("Step 3: Propose commands to create dataset structure")
    struct_plan = agent.run_step("create_structure", context)
    print(struct_plan)
    cmds = parse_commands_from_markdown(struct_plan)
    _print_commands(cmds)
    context = enter_feedback_loop(agent, context)
    if not prompt_yes_no("Proceed to propose renaming/moving?", default=True):
        return 0

    short_divider("Step 4: Propose commands to rename/move files")
    move_plan = agent.run_step("rename_move", context)
    print(move_plan)
    cmds = parse_commands_from_markdown(move_plan)
    _print_commands(cmds)
    context = enter_feedback_loop(agent, context)

    print("\nAll steps completed. Commands were only displayed - use them manually")
    return 0


if __name__ == "__main__":
    sys.exit(main())