Skip to content

Commit

Permalink
Interactive predictions from the command-line
Browse files Browse the repository at this point in the history
  • Loading branch information
avaucher committed Oct 17, 2023
1 parent 84af0f2 commit ad493f1
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,11 @@ paragraph2actions-translate -s src-test.txt -o pred-organic.txt -p sp_model.m
paragraph2actions-translate -s src-test.txt -o pred-pretrained.txt -p sp_model.model -t pretrained.pt
```

It is also possible to do interactive predictions from the command-line with the following:
```bash
paragraph2actions-translate -s sp_model.model -m sac.pt
```

## Metrics

To compute the metrics and produce a CSV comparing the models, execute the following:
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ console_scripts =
sac-augment-annotations = sac_action_extraction.create_annotation_splits:main
sac-create-annotation-splits = sac_action_extraction.create_annotation_splits:main
sac-concatenate-annotations = sac_action_extraction.concatenate_annotation_splits:main
sac-interactive-extraction = sac_action_extraction.interactive_predictions:main
sac-metrics-grid = sac_action_extraction.metrics_grid:main

[flake8]
Expand Down
65 changes: 65 additions & 0 deletions src/sac_action_extraction/interactive_predictions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import sys
from pathlib import Path
from typing import TextIO, Tuple

import click
from paragraph2actions.default_converters import default_action_converters
from paragraph2actions.paragraph_translator import ParagraphTranslator
from paragraph2actions.readable_converter import ReadableConverter

from .sac_converters import default_sac_converters


@click.command()
@click.option(
"--models",
"-m",
multiple=True,
type=click.Path(exists=True, dir_okay=False, path_type=Path),
help="Translation model file(s). If multiple are given, will be an ensemble model.",
)
@click.option(
"--sentencepiece_model",
"-s",
required=True,
type=click.Path(exists=True, dir_okay=False, path_type=Path),
help="SentencePiece model file (typically ``sp_model.model``)",
)
@click.argument("input_file", type=click.File(mode="r"), default=sys.stdin)
@click.argument("output_file", type=click.File(mode="w"), default=sys.stdout)
def main(
models: Tuple[Path, ...],
sentencepiece_model: Path,
input_file: TextIO,
output_file: TextIO,
) -> None:
"""Interactive extraction of SAC synthesis actions.
Input and output can be either stdin/stdout, or input file and output file.
"""

single_action_converters = default_sac_converters() + default_action_converters()
converter = ReadableConverter(single_action_converters=single_action_converters)

paragraph_translator = ParagraphTranslator(
translation_model=[str(m) for m in models],
sentencepiece_model=str(sentencepiece_model),
action_string_converter=converter,
)

for line in input_file:
line = line.strip()

# Canonicalize the SMILES, handle exception if needed
try:
result = paragraph_translator.extract_paragraph(line)
output_file.write("Extracted actions as Python objects:\n")
output_file.write(f"{result.actions}\n")
output_file.write("Extracted actions in readable format:\n")
output_file.write(f"{converter.actions_to_string(result.actions)}\n\n")
except Exception as e:
output_file.write(f"\nError: {e}\n")


if __name__ == "__main__":
main()

0 comments on commit ad493f1

Please sign in to comment.