-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuse_local_model.py
More file actions
executable file
·49 lines (34 loc) · 1.28 KB
/
use_local_model.py
File metadata and controls
executable file
·49 lines (34 loc) · 1.28 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
#!/usr/bin/env python
"""
Small script to show an example of using a local model
"""
import argparse
import pprint
import typing
import pathlib
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
ModelResult = typing.Tuple[AutoModelForCausalLM, AutoTokenizer]
def load_model(model_path: pathlib.Path):
"""
Could drop this into a notebook to download the model in a specific
environment (eg AWS sagemaker with inferentia)
"""
assert model_path.exists()
tokenizer = AutoTokenizer.from_pretrained(str(model_path))
model = AutoModelForCausalLM.from_pretrained(str(model_path))
return (tokenizer, model,)
def prompt(model_path: pathlib.Path, prompt: str):
tokenizer, model = load_model(model_path)
p = pipeline('text-generation', model=model, tokenizer=tokenizer)
return p(prompt)
ArgsType = typing.Optional[typing.List[str]]
def parse_args(args: ArgsType=None) -> argparse.Namespace:
p = argparse.ArgumentParser()
p.add_argument('model_path', type=pathlib.Path, help='which model to use')
p.add_argument('prompt', help='the prompt to submit')
return p.parse_args(args)
def main(args: ArgsType=None):
args = parse_args(args)
pprint.pprint(prompt(args.model_path, args.prompt))
if __name__ == '__main__':
main()