-
Notifications
You must be signed in to change notification settings - Fork 5
/
ieltsGPT.py
60 lines (52 loc) · 1.96 KB
/
ieltsGPT.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
from openai import OpenAI # type: ignore
with open("key.txt", 'r', encoding='utf-8') as f:
key = [i.strip() for i in f.readlines()]
client = OpenAI(api_key=key[0])
def ask_llm(prompt):
resp = client.chat.completions.create(
model="gpt-4-turbo",
messages=[{"role": "user",
"content": prompt},
]
)
answer = resp.choices[0].message.content
return answer.strip()
def get_essay():
with open("data/essay.txt", 'r', encoding='utf-8') as f:
text = f.read().strip()
return text
def load_prompt(task=1):
aspects = ['ta','cc','lr','gra']
text = []
for aspect in aspects:
with open("prompts/ielts/ielts_writing_"+str(task)+"_prompt_"+aspect+".txt", 'r', encoding='utf-8') as f:
text.append((aspect,f.read().strip()))
return text
def clean(text):
text = text.replace('<br>',"\n")
return text
def overall_assess():
short_names = {'ta':'Task Achievement','cc':'Coherence and Cohesion','lr': 'Lexical Resource', 'gra':'Grammatical Range and Accuracy'}
overall_assessments = []
prompts = load_prompt(task=2)
text = get_essay()
done = []
while True:
for prompt in prompts:
if prompt[0] in done:
continue
query = prompt[1] + "\n" + text
try:
overall_assessments.append(clean(ask_llm(query)))
done.append(prompt[0])
except:
continue
print(len(done))
if len(done) == 4:
break
with open("data/feedback.md", 'w', encoding='utf-8') as f:
f.write("## Task Description & My Writing:\n" + text.replace("\n","\n\n") + "\n\n---\n\n")
for short_name,feedback in zip(done,overall_assessments):
f.write("## "+short_names[short_name]+"\n" + feedback+"\n\n")
if __name__ == '__main__':
overall_assess()