-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain1_test.py
160 lines (137 loc) · 5.27 KB
/
main1_test.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import os
import io
import openai
import requests
import numpy as np
import pandas as pd
from PIL import Image
from google.cloud import vision
from Preprocess import clean_img, scale_image, enhance_image
from analysis import string_similarity, minify_c_code
from test_cases import get_request_body_test_cases, get_test_cases, get_labels_and_prompts
from insensitive_checking import insensitive_checking_preprocessing
problem_number = 1
openai.api_key = os.getenv("OPENAPI_SECRET_KEY")
rootdir = f"/Users/cymmerjohnmaranga/Downloads/Problem_{problem_number}_Test copy"
file_paths = []
for subdir, dirs, files in os.walk(rootdir):
for file in files:
file_path = subdir + os.sep + file
file_paths.append(file_path)
scale_factor = 2
# Preprocess images
for i, file_path in enumerate(file_paths):
try:
image = Image.open(file_path)
np_image = np.array(image)
cleaned_image = clean_img(np_image)
img = Image.fromarray(np.uint8(cleaned_image))
scaled_img = scale_image(img, scale_factor)
enhanced_img = enhance_image(scaled_img)
enhanced_img.save(file_path)
except Exception as e:
print("ERROR: ", e)
file_paths.sort()
IDS = []
imageLinks = []
googleVisionAPIResults = []
openAIAPIResults = []
checkerStatuses = []
for i, file_path in enumerate(file_paths):
if file_path.split(".")[-1] not in ["png", "jpg"]:
continue
### GOOGLE CLOUD VISION ###
client = vision.ImageAnnotatorClient()
with io.open(file_path, "rb") as image_file:
content = image_file.read()
image = vision.Image(content=content)
response = client.text_detection(image=image)
detections = response.full_text_annotation
converted_text = detections.text
### OPEN AI ###
model = "gpt-3.5-turbo"
body = f"Make this C code snippet syntax-error free only. Do not add additional code that may change the overall logic of the code. \n\n{converted_text}"
try:
completion = openai.ChatCompletion.create(
model=model,
messages=[{"role": "user", "content": body}],
)
except (openai.APIError, openai.InvalidRequestError, openai.OpenAIError) as e:
print("OPEN AI ERROR: ", e)
pass
corrected_code = completion.choices[0].message.content
test_cases = get_request_body_test_cases(problem_number=problem_number, code=corrected_code)
response = requests.post(
"https://codechum-interactive-svjuxd2qva-de.a.run.app/v1/interactive/checker",
json=test_cases,
)
data = response.json()
is_correct = all(
regex_status["is_correct"] for regex_status in data["regex_statuses"]
)
if is_correct:
for i, submission in enumerate(data["submissions"]):
status = submission[list(submission.keys())[0]]["status"]
if status != 3:
is_correct = False
break
stdout = submission[list(submission.keys())[0]]["stdout"]
preprocessed_stdout = insensitive_checking_preprocessing(stdout)
preprocessed_test_case_stdout = insensitive_checking_preprocessing(
get_test_cases(problem_number=problem_number)[i]
)
if preprocessed_stdout != preprocessed_test_case_stdout:
print("PREPROCESSED_STDOUT: ", preprocessed_stdout)
print("PREPROCESSED_TEST_CASE_STDOUT", preprocessed_test_case_stdout)
is_correct = False
break
IDS.append(i)
imageLinks.append(file_path)
googleVisionAPIResults.append(converted_text)
openAIAPIResults.append(corrected_code)
checkerStatuses.append(is_correct)
# Excel Settings
excel_file_path = (
f"/Users/cymmerjohnmaranga/Downloads/ExcelDatasets_New/C_Problem_{problem_number}_Test.xlsx"
)
sheet_name = f"Problem{problem_number}"
with open(f"./problems/problem{problem_number}.c", "r") as f:
actual_problem_code = f.read()
problem_data = pd.DataFrame(
{
"ID": IDS,
"Image Link": imageLinks,
"Google Vision API Results": googleVisionAPIResults,
"Open AI Results": openAIAPIResults,
"Actual Codes": [code for code in openAIAPIResults],
"Google Vision API Results (MINIFIED)": [
minify_c_code(code) for code in googleVisionAPIResults
],
"Open AI Results (MINIFIED)": [
minify_c_code(code) for code in openAIAPIResults
],
"Actual Codes (MINIFIED)": [
"" for _ in range(len(openAIAPIResults))
],
"Google Vision API Result vs Corrected Code": [
string_similarity(
minify_c_code(googleVisionAPIResults[i]),
minify_c_code(openAIAPIResults[i]),
)
for i in range(len(openAIAPIResults))
],
"Corrected Code vs Actual Code": [
# string_similarity(
# minify_c_code(openAIAPIResults[i]), minify_c_code(actual_problem_code)
# )
0
for i in range(len(openAIAPIResults))
],
"Checker Status": checkerStatuses,
}
)
sheets = {f"Problem{problem_number}": problem_data}
writer = pd.ExcelWriter(excel_file_path, engine="xlsxwriter")
for sheet_name in sheets.keys():
sheets[sheet_name].to_excel(writer, sheet_name=sheet_name, index=False)
writer.save()