-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
182 lines (143 loc) · 5.79 KB
/
main.py
File metadata and controls
182 lines (143 loc) · 5.79 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import argparse
import os
from evaluator.chromadb import RagDB
from evaluator.utils.code_loader import CodeLoader
from evaluator.utils.prompt import Prompt
from evaluator.utils.extract_return import extract_return_value
from evaluator.metrics import calculate_bleu, calculate_meteor, calculate_rouge
from evaluator.models.model import Model
from benchmark.generate_from_template import generate_from_template
import nltk
nltk.download("wordnet")
VALID_MODELS = [
"codellama-7b",
"codellama-13b",
"llama-3.1-8b",
"deepseek-coder-v2",
"test",
]
VALID_LIBRARIES = [
"generic",
"priceline_pcln-design-system",
"themesberg_flowbite-react",
"shopify_polaris",
"carbon-design-system_carbon",
]
POSTPROCESSOR_SCRIPT = "run-linter.js"
EXTRACT_RETURN_REGEX2 = "return \(\s*<([\s\S]*?)>\s*\)"
EXTRACT_RETURN_REGEX = "\((?:[^()]*|(?R))*\)"
def parse_args():
parser = argparse.ArgumentParser(description=".")
parser.add_argument(
"--model",
type=str,
required=True,
help="The name of the model to be used",
)
parser.add_argument(
"--allowStories",
type=str,
default="False",
help="Allow StorybookJS files in the RAG DB",
)
parser.add_argument(
"--allowImpl",
type=str,
default="False",
help="Allow component implementation files in the RAG DB",
)
parser.add_argument(
"--trialName",
type=str,
required=True,
help="The name of this trial",
)
args = parser.parse_args()
args.allowStories = args.allowStories.lower() == "true"
args.allowImpl = args.allowImpl.lower() == "true"
# if not args.model in VALID_MODELS:
# raise ValueError(
# f"Model {args.model} not found in list of acceptable models: [{', '.join(VALID_MODELS)}]"
# )
return args
def main():
args = parse_args()
print(args)
# Craete directory for this trial's output
output_folder = f"generated/{args.trialName}"
while os.path.isdir(output_folder):
output_folder += "_1"
os.makedirs(output_folder)
# Load the LLM
model = Model(args.model)
for library in VALID_LIBRARIES:
# Generate the ground truths, format, and delete the raw files
os.makedirs(f"{output_folder}/gt-raw")
os.makedirs(f"{output_folder}/gt/{library}")
generate_from_template(
library, f"{output_folder}/gt-raw", path_prefix="benchmark/"
)
os.system(
f"cd ./postprocessor/linter; node {POSTPROCESSOR_SCRIPT} inputDir=../../{output_folder}/gt-raw/{library} outputDir=../../{output_folder}/gt/{library}"
)
os.system(f"rm -rf {output_folder}/gt-raw")
# Initialize RAG DB and add library to it
print("Creating RAG DB...", flush=True)
os.makedirs(f"{output_folder}/rag_db/{library}")
rag_db = RagDB(persist_dir=f"{output_folder}/rag_db/{library}")
rag_db.add_dir_to_chromadb(
f"libraries/{library}",
allow_stories=args.allowStories,
allow_impl=args.allowImpl,
)
print("RAG DB created.", flush=True)
# Create directory for this library's output
os.makedirs(f"{output_folder}/prediction-raw/{library}")
prompter = Prompt(library=library, generated_path=output_folder)
while prompter.has_next():
# Get the next prompt
prompt, filename = prompter.get_next()
# Retrieve relevant code from RAG DB
retrieval = rag_db.retrieve_relevant_code(prompt)
# Generate the processed input prompt
prompt = prompter.process_prompt(prompt, retrieval)
# print("-------------------------------------------------------------------------")
# print(f"Prompt: {prompt}")
# print("-------------------------------------------------------------------------\n\n\n\n\n\n\n")
# Generate the code using the model
generated = model.generate(prompt)
# Write the generated code to the output directory
with open(
f"{output_folder}/prediction-raw/{library}/{filename}.jsx", "w"
) as file:
generated = extract_return_value(generated)
file.write(generated)
# Run postprocessor on the generated code and delete the raw files
os.makedirs(f"{output_folder}/prediction/{library}")
os.system(
f"cd ./postprocessor/linter;node {POSTPROCESSOR_SCRIPT} inputDir=../../{output_folder}/prediction-raw/{library} outputDir=../../{output_folder}/prediction/{library}"
)
# os.system(f"rm -rf {output_folder}/prediction-raw/{library}")
rag_db.cleanup()
os.system(f"rm -rf {output_folder}/prediction-raw")
# Run the evaluator on the generated code
code_loader = CodeLoader(generated_path=output_folder)
# Create a file for the results and write the headers
with open(f"{output_folder}/results.csv", "w") as result_file:
result_file.write(
"filename,library,bleu,meteor,rouge1f,rouge1p,rouge1r,rouge2f,rouge2p,rouge2r\n"
)
# Iterate through generated code
while code_loader.has_next():
gt, prediction, filename, library = code_loader.get_next()
# Calculate metrics
bleu = calculate_bleu(gt, prediction)
meteor = calculate_meteor(gt, prediction)
rouge = calculate_rouge(gt, prediction)
rouge1 = rouge.get("rouge1")
rouge2 = rouge.get("rouge2")
# Write the results to the CSV file
result_file.write(
f"{filename},{library},{bleu},{meteor},{rouge1.fmeasure},{rouge1.precision},{rouge1.recall},{rouge2.fmeasure},{rouge2.precision},{rouge2.recall}\n"
)
main()