Repository navigation
Expand file tree
/
Copy pathextract.py
More file actions
61 lines (52 loc) · 2.24 KB
/
Copy pathextract.py
File metadata and controls
61 lines (52 loc) · 2.24 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
#!/usr/bin/env python3
"""
Extract clinical facts from parsed documents using LLM.
Usage: python -m scripts.extract --input data/interim/<paper>_parsed.json
"""
import argparse
import os
import sys
from pathlib import Path
# Add project root to Python path for imports
project_root = Path(__file__).parent.parent
if str(project_root) not in sys.path:
sys.path.insert(0, str(project_root))
from src.core.extract_llm import extract_pipeline
def main():
parser = argparse.ArgumentParser(description="Extract clinical facts from parsed documents")
parser.add_argument("--input", required=True, help="Path to parsed JSON file from Docling")
parser.add_argument("--output", help="Path for extracted facts JSON output (default: data/processed/extracted/{stem}_extracted.json)")
parser.add_argument("--model", default="gpt-4o", help="OpenAI model to use (display only; pipeline uses configured default)")
args = parser.parse_args()
input_path = Path(args.input)
if not input_path.exists():
print(f"❌ Input file not found: {args.input}")
print("💡 First run: python -m scripts.parse_doc --source <pdf_or_url>")
sys.exit(1)
if not args.output:
out_dir = Path("data/processed/extracted")
out_dir.mkdir(parents=True, exist_ok=True)
args.output = out_dir / f"{input_path.stem}_extracted.json"
if not os.getenv("OPENAI_API_KEY"):
print("❌ Please set OPENAI_API_KEY environment variable (.env supported)")
sys.exit(1)
print(f"🔍 Extracting facts from: {input_path}")
print(f"📝 Output will be saved to: {args.output}")
print(f"🤖 Using model: {args.model}")
print("-" * 50)
try:
triples = extract_pipeline(input_path, args.output)
print("-" * 50)
print(f"✅ Extraction complete! {len(triples)} facts")
print(f"💾 Saved: {args.output}")
print("\nNext:")
print(f" python -m scripts.validate --input \"{args.output}\" --show-details")
except KeyboardInterrupt:
print("\n⏹ Extraction cancelled by user")
sys.exit(1)
except Exception as e:
print(f"❌ Extraction failed: {e}")
import traceback; traceback.print_exc()
sys.exit(1)
if __name__ == "__main__":
main()