-
Notifications
You must be signed in to change notification settings - Fork 3
/
completion.py
executable file
·79 lines (62 loc) · 2.45 KB
/
completion.py
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
72
73
74
75
76
77
78
79
import argparse
import openai
import asyncio
import json
import sys
from dotenv import load_dotenv
from utils import get_completions_from_input, fix_escaped_unicode
from constants import model_name_to_id
import os
import re
from pathlib import Path
load_dotenv()
openai.api_key = os.getenv("OPENAI_API_KEY")
logdir = str(Path(__file__).resolve().parent) + "/logs"
async def main():
parser = argparse.ArgumentParser()
# parser.add_argument('key', metavar='<key>', type=str, help='key to use for OpenAI')
parser.add_argument('modelName', metavar='<model_name>', type=str, help='model name to use for OpenAI (underline, highlight, emphasis)')
parser.add_argument('--tag', type=str, help='tag of the card', metavar="tag")
parser.add_argument('--bodyText', type=str, help='body text', metavar="bodyText")
parser.add_argument('--underlines', type=str, help='JSON formatted underlines', metavar="underlines", default=None)
parser.add_argument('--paragraphs', type=str, metavar="paragraphs", default="0")
args = parser.parse_args()
bodyText = args.bodyText
bodyText = re.sub(r"[\r\n]+", " ", bodyText)
bodyText = bodyText.replace('\n', ' ').replace('\r', ' ')
bodyText = bodyText.replace('\n', ' ').replace('\r', ' ')
bodyText = bodyText.replace('\\n', ' ').replace('\\r', ' ')
paragraphs = args.paragraphs.split(",")
paragraphs = [int(p) for p in paragraphs]
paragraphs = [p for p in paragraphs if p != 0]
with open(logdir + '/bodyText.log', 'w+') as f:
f.write(bodyText)
model = model_name_to_id[args.modelName]
# if args.underlines:
# args.underlines = fix_escaped_unicode(args.underlines)
# bodyText = fix_escaped_unicode(bodyText)
if args.modelName != "underline":
underlines = args.underlines.split(", ")
else:
underlines = None
output = await get_completions_from_input(
args.tag,
bodyText,
model,
underlines=json.dumps(underlines) if underlines is not None else None,
debug=False,
paragraphs=paragraphs
)
if output is None:
print("No output")
return
output_str, loc = output
with open(logdir + '/completion2.log', 'w+') as f:
f.write(str(output_str))
print(loc)
if __name__ == '__main__':
os.makedirs(logdir, exist_ok=True)
# log arguments string to file for debugging
with open(logdir + '/completion.log', 'w+') as f:
f.write(sys.argv[0] + " " + sys.argv[1] + " " + sys.argv[2] + " " + sys.argv[3] + " " + sys.argv[4] + " " + sys.argv[5])
asyncio.run(main())