-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsonToCode.py
51 lines (40 loc) · 1.07 KB
/
jsonToCode.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
import json
import os
import openai
data = json.load(open('tmp/output.json'))
# code = data['responses'][0]['fullTextAnnotation']['text']
code = data['fullTextAnnotation']['text']
"""lines = code.split('\n')
for i in range(len(lines)):
if lines[i] and lines[i][-1] in ',.:':
lines[i] = lines[i][:-1] + ';'
elif lines[i] and lines[i][-1] != ';':
lines[i] += ';'"""
with open('gpt_key.txt', 'r') as file:
openai.api_key = file.readlines()[0]
response = openai.Completion.create(
model="text-davinci-003",
prompt=f"can you make this code compile, but don't fix runtime errors%n{code}%ndon't say anything before or after the fixed code",
temperature=0,
max_tokens=256,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)
lines = str(response['choices'][0]['text'])
if os.path.isfile('tmp/Code.java'):
os.remove('tmp/Code.java')
f = open('tmp/Code.java', 'a')
pre = """
package tmp;
class Code {
public static void main(String[] args) {
"""
post = """
}
}
"""
f.writelines(pre)
f.writelines(lines)
f.writelines(post)
f.close()