File size: 2,281 Bytes
f346832
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Small text-only inference example for the self-contained FxAI checkpoint."""

from __future__ import annotations

import argparse
import json
import sys
from pathlib import Path

# Running the example inside an upload folder should not create *.pyc files.
sys.dont_write_bytecode = True

from futurix_decision import DecisionInput, load_model


def parse_option(value: str) -> tuple[str, str]:
    name, separator, description = value.partition("=")
    if not separator or not name.strip() or not description.strip():
        raise argparse.ArgumentTypeError("options must use NAME=DESCRIPTION")
    return name.strip(), description.strip()


def main() -> None:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--checkpoint", type=Path, default=Path(__file__).resolve().parent)
    parser.add_argument("--device", default="cuda:0")
    parser.add_argument(
        "--state",
        default="My integration keeps failing. Please help.",
        help="Text to classify.",
    )
    parser.add_argument(
        "--instructions",
        default="Which team should handle this request?",
        help="Question shown to the decision model.",
    )
    parser.add_argument(
        "--option",
        action="append",
        type=parse_option,
        metavar="NAME=DESCRIPTION",
        help="Candidate decision option; repeat for multiple options.",
    )
    parser.add_argument("--max-image-pixels", type=int, default=512 * 512)
    args = parser.parse_args()
    options = args.option or [
        ("billing", "Charges and refunds"),
        ("support", "Technical integration errors"),
        ("sales", "Questions about purchasing the product"),
    ]
    if len({name for name, _ in options}) != len(options):
        parser.error("option names must be unique")

    model = load_model(
        args.checkpoint,
        family="fxai",
        device=args.device,
        max_image_pixels=args.max_image_pixels,
    )
    row = DecisionInput(
        state=args.state,
        question=args.instructions,
        options=[name for name, _ in options],
        candidate_descriptions=[description for _, description in options],
    )
    print(json.dumps(model.decide(row).to_dict(), indent=2))


if __name__ == "__main__":
    main()