-
Notifications
You must be signed in to change notification settings - Fork 3
/
get_layout.py
50 lines (38 loc) · 1.25 KB
/
get_layout.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
from openai import OpenAI
import argparse
import httpx
import ast
import re
def update_template(prompt, template_file):
with open(template_file, 'r') as file:
lines = file.readlines()
index = len(lines) - 3
lines[index] = f"Caption: {prompt}\n"
updated_template = ''.join(lines)
return updated_template
def get_layout(user_prompt, api_key):
client = OpenAI(
base_url="xxx",
api_key=api_key,
http_client=httpx.Client(
base_url="xxx",
follow_redirects=True,
),
)
prompt = update_template(user_prompt, "template.txt")
completion = client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system", "content": prompt},
{"role": "user", "content": prompt}
]
)
output = completion.choices[0].message.content
output = str(output)
result = re.search(r"\[(.*?)\]\nPosition:\s*(\[(.*?)\])", output)
extracted_part = result.group(1)
obj = ast.literal_eval(extracted_part)
phrase = [item[0] for item in obj]
location = [item[1] for item in obj]
token_location = ast.literal_eval(result.group(2))
return phrase, location, token_location