<<
path:
root/public/gist.git/html/python/ml_grammar.py
blob: 88cddefe49e82146bfa49aae47a22658fc085556
[raw]
[clear marker]
2from dataclasses import dataclass
3from shutil import get_terminal_size
4from sys import exit, argv
5from subprocess import run
6from pathlib import Path
10MODEL_PATH = "/drives/drive3/ML/LLama-3-8B-grammar-correction/Llama-3-8B-grammar-correction.Q6_K.gguf"
11TEMPERATURE = 0.1 # Recommendation by model
12TOKENS_TO_PREDICT = 256
13GPU_LAYERS = 99 # Also possible: "auto", "all"
16LLAMA_TOOL = "llama-completion"
18SYSTEM_PROMPT = "Correct the grammar and spelling of the following text, " \
19 "but omit what was already correct."
30 parsed_args = arg_parse()
32 if not parsed_args.debug:
33 fp = Path(parsed_args.fp)
34 fp_abs = fp.absolute()
37 print("Error: File path does not exist:", fp_abs)
41 run_model(parsed_args, fp_abs)
49 if len(args) == 1 or len(args) > max_args:
50 print("Insufficient arguments. Expecting a path to a text file")
53 if args[1] == "-debug":
54 parsed_args.debug = True
56 parsed_args.fp = args[1]
61def run_model(parsed_args, fp):
65 "-ngl", str(GPU_LAYERS),
66 "--single-turn", # Exits llama after one prompt
67 "--temp", str(TEMPERATURE),
68 "--n-predict", str(TOKENS_TO_PREDICT),
69 "--no-display-prompt",
70 "--system-prompt", SYSTEM_PROMPT,
79 result = run(cmd, capture_output=True, text=True)
80 except FileNotFoundError:
81 print(f"Could not find `{LLAMA_TOOL}` in $PATH")
83 except Exception as e:
87 output_label = "-- OUTPUT "
88 term_width = get_terminal_size(fallback=(80, 24)).columns
89 width_remainder = abs(term_width - len(output_label))
92 print(f"{output_label}{'-' * width_remainder}")
94 if result.returncode != 0 and result.stderr:
95 print("*** *** ERROR *** ***")
98 exit(result.returncode)
101if __name__ == "__main__":
104 except KeyboardInterrupt:
105 print("Terminated by user.")