-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.py
More file actions
431 lines (350 loc) · 13.8 KB
/
main.py
File metadata and controls
431 lines (350 loc) · 13.8 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
"""Provides driver functionality for running the GitHub extractor."""
import argparse
from datetime import datetime
import glob
import os
import pickle
import sys
from openai import OpenAI
import pandas as pd
import src as CoreEngine # change to `import CoreEngine.src as CoreEngine` in submodules
from dotenv import load_dotenv
from __init__ import __version__
def main():
"""Driver function for GitHub Repo Extractor. Used to TRAIN models"""
print("Connecting to database...")
load_dotenv()
init_db()
cfg_path, skip_train = get_cli_args()
cfg_dict = CoreEngine.utils.read_jsonfile_into_dict(cfg_path)
cfg_obj = CoreEngine.repo_extractor.conf.Cfg(
cfg_dict, CoreEngine.repo_extractor.schema.cfg_schema
)
db = CoreEngine.DatabaseManager()
repo = db.allocate_repo(cfg_dict["repo"])
gh_ext = CoreEngine.repo_extractor.extractor.Extractor(cfg_obj, db)
prs = gh_ext.get_repo_issues_data(db)
api_labels = CoreEngine.utils.read_jsonfile_into_dict(
cfg_obj.get_cfg_val("api_domain_label_listing")
)
sub_labels = CoreEngine.utils.read_jsonfile_into_dict(
cfg_obj.get_cfg_val("api_subdomain_label_listing")
)
ai = CoreEngine.AICachedClassifier(api_labels, sub_labels, db)
print("Classifying APIs in files")
for pr in prs:
print(f"\tClassifying files from PR {pr} for predictions training ")
# Here is where ASTs and classification are done;
# all the "heavy lifting" of the core engine
CoreEngine.process_files(ai, db, pr, repo)
# CoreEngine.process_files(ai, db) <-- Run this to process PRs from any repo!
db.save()
if skip_train:
print("\nSkipping Model Training. \nDone.")
sys.exit()
method = cfg_dict["clf_method"]
print("\nPreparing data frame")
# this gets data from a specific PR from a specific Repository
df = get_prs_df(db, prs, repo)
# Instead, you can use this below to get ALL data from all PRs and Repos stored
# df = get_all_data(db)
# df.to_csv("output/all_data.csv")
print("Getting data from all extracted repositories:\n")
repos_processed = db.get_all_repos()
for repo_process in repos_processed:
prs_num = len(db.get_prs_of_repo(repo_process))
print(
f"\tIncluding data for: {repo_process.owner}/{repo_process.name} PRs: {prs_num}"
)
print()
# Here is where you can do processing with the dataframe to isolate/manipulate it.
# Or, put it in the below if statements for gpt or random forest...
print(f"Training... {method} model")
if method == "gpt":
json_open = cfg_obj.get_cfg_val("gpt_jsonl_path")
if len(prs) < 10:
print("Too Few PRs to train! Quitting")
exit()
domain_message, subdomain_message = (
CoreEngine.classifier.generate_system_message(api_labels, sub_labels, df)
)
CoreEngine.classifier.generate_gpt_messages(
domain_message, subdomain_message, df, json_open
)
llm_classifier = CoreEngine.classifier.fine_tune_gpt(json_open)
if llm_classifier is None:
print(
"Error training! See https://platform.openai.com/finetune/ for details. Exiting.."
)
exit()
# Save Model
with open(cfg_dict["clf_model_out_path"], "wb") as f:
dat = {
"time_saved": datetime.now(),
"model": llm_classifier,
"type": "gpt",
"save_version": __version__,
}
pickle.dump(dat, f)
# classifier.save_model(llm_classifier)
print(f"Your model has been saved {llm_classifier}")
if method == "gpt-combined":
print(
"Warning: gpt-combined requires lots of data! If you get an error, try using more data!"
)
print(
"Delete checkpoint file in output/gpt-combined-checkpoint.pkl on change of dataset"
)
json_open = cfg_obj.get_cfg_val("gpt_jsonl_path")
check = {"status": []}
try:
checkpoint_file = open("output/gpt-combined-checkpoint.pkl", "rb")
check = pickle.load(checkpoint_file)
checkpoint_file.close()
except:
pass
if "format_labels" not in check["status"]:
# Format labels
formatted_domains = CoreEngine.classifier.format_domain_labels(
api_labels, sub_labels
)
formatted_domains, df = CoreEngine.classifier.drop_rare_domains(
df, formatted_domains
)
formatted_domains, df = CoreEngine.classifier.drop_rare_subdomains(
df, formatted_domains
)
if len(df) == 0:
print("Too little data! Try more prs!")
return
checkpoint_file = open("output/gpt-combined-checkpoint.pkl", "wb")
check = {
"status": ["format_labels"],
"f_domains": formatted_domains,
"df": df,
}
pickle.dump(check, checkpoint_file)
checkpoint_file.close()
else:
formatted_domains = check["f_domains"]
df = check["df"]
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
if "data_pre" not in check["status"]:
# Create dataframe for each domain and set of subdomains
dataframe_dictionary = CoreEngine.classifier.populate_dataframe_dictionary(
formatted_domains, df, client
)
subdomain_dictionary = CoreEngine.classifier.populate_subdomain_dictionary(
formatted_domains, df, client
)
# Split dataframes
dataframe_dictionary = CoreEngine.classifier.split_domain_dataframes(
dataframe_dictionary
)
subdomain_dictionary = CoreEngine.classifier.split_subdomain_dataframes(
subdomain_dictionary
)
# Generate messages for finetune
dataframe_dictionary = CoreEngine.classifier.generate_domain_messages(
dataframe_dictionary
)
subdomain_dictionary = CoreEngine.classifier.generate_subdomain_messages(
subdomain_dictionary, formatted_domains
)
checkpoint_file = open("output/gpt-combined-checkpoint.pkl", "wb")
check = {
"status": ["format_labels", "data_pre"],
"f_domains": formatted_domains,
"df": df,
"df_dict": dataframe_dictionary,
"subdomain_dict": subdomain_dictionary,
}
pickle.dump(check, checkpoint_file)
checkpoint_file.close()
else:
formatted_domains = check["f_domains"]
df = check["df"]
dataframe_dictionary = check["df_dict"]
subdomain_dictionary = check["subdomain_dict"]
if "train" not in check["status"]:
# Fine tune models - pass checkpoint.
dataframe_dictionary = CoreEngine.classifier.get_domain_models(
dataframe_dictionary, client, check
)
subdomain_dictionary = CoreEngine.classifier.get_subdomain_models(
subdomain_dictionary, client, check
)
check["status"].append("train")
checkpoint_file = open("output/gpt-combined-checkpoint.pkl", "wb")
pickle.dump(check, checkpoint_file)
checkpoint_file.close()
if "eval" not in check["status"]:
# Evaluate models and produce metrics csv
CoreEngine.classifier.produce_domain_csv(
dataframe_dictionary,
os.getenv("OPENAI_API_KEY"),
"output/domain_results.csv",
)
CoreEngine.classifier.produce_subdomain_csv(
subdomain_dictionary,
formatted_domains,
os.getenv("OPENAI_API_KEY"),
"output/subdomain_results.csv",
)
check["status"].append("eval")
checkpoint_file = open("output/gpt-combined-checkpoint.pkl", "wb")
pickle.dump(check, checkpoint_file)
checkpoint_file.close()
model_table = CoreEngine.classifier.get_model_json(
dataframe_dictionary, subdomain_dictionary
)
# Save Model ... keep this part.
with open(cfg_dict["clf_model_out_path"], "wb") as f:
dat = {
"time_saved": datetime.now(),
"model_table": model_table,
"type": "gpt-combined",
"save_version": __version__,
}
pickle.dump(dat, f)
# classifier.save_model(llm_classifier)
print(f"Your model gpt-combined has been saved")
if method == "rf":
df = df.drop(
columns=[
"Repo Name",
"PR #",
"Pull Request",
"created_at",
"closed_at",
"userlogin",
"author_name",
"most_recent_commit",
"filename",
"file_commit",
"api",
"function_name",
"api_domain",
"subdomain",
]
)
df = df.dropna()
print("\nTraining Model...")
x_text_features, vx = CoreEngine.classifier.extract_text_features(df)
# Transform labels
y_df, _ = CoreEngine.classifier.transform_labels(df)
# Combine features
x_combined = CoreEngine.classifier.create_combined_features(x_text_features)
# Get class distribution before MLSMOTE
print("\nClass distribution before MLSMOTE:")
class_distribution_before = y_df.sum(axis=0)
print(class_distribution_before)
# Perform MLSMOTE to augment the data
if len(prs) < 3:
print("Too Few PRs to train! Quitting")
return
print("\nbalancing classes...")
x_augmented, y_augmented = CoreEngine.classifier.perform_mlsmote(
x_combined, y_df, n_sample=500
)
# Get class distribution after MLSMOTE
print("\nClass distribution after MLSMOTE:")
y_combined = pd.concat([y_df, y_augmented], axis=0)
class_distribution_after = y_combined.sum(axis=0)
print(class_distribution_after)
print("\nTraining RF model...")
x_combined = pd.concat([x_combined, x_augmented], axis=0)
y_combined = pd.concat([y_df, y_augmented], axis=0)
# Train
clf = CoreEngine.classifier.train_random_forest(x_combined, y_combined)
# Save Model
with open(cfg_dict["clf_model_out_path"], "wb") as f:
dat = {
"time_saved": datetime.now(),
"model": clf,
"vectorizer": vx,
"labels": y_df,
"type": "rf",
"save_version": __version__,
}
pickle.dump(dat, f)
print(f"Your model has been saved {clf}")
db.close()
sys.exit()
def init_db():
"""TODO."""
base_path: str = "./output/"
downloads_path: str = base_path + "downloaded_files/"
for file in glob.glob(downloads_path + "*"):
os.remove(file)
os.makedirs(downloads_path, exist_ok=True)
CoreEngine.database_init.start()
# def setup_db():
# """TODO."""
# database_init.populate_db_with_mining_data()
# database_init.setup_caches()
def get_cli_args() -> tuple[str, bool]:
"""
Get initializing arguments from CLI.
Returns:
str: path to file with arguments to program
bool: skip training flag
"""
# establish positional argument capability
arg_parser = argparse.ArgumentParser(
description="Mines data from GitHub repositories",
)
# add repo input CLI arg
arg_parser.add_argument(
"extractor_cfg_file",
help="Path to JSON configuration file",
)
arg_parser.add_argument(
"-s",
action="store_true",
help="Skip training of model",
)
args = arg_parser.parse_args()
return args.extractor_cfg_file, args.s
def get_all_data(db: CoreEngine.DatabaseManager) -> pd.DataFrame:
"""Get all extracted data from all Repos/PRs
Can be used for training
Args:
db (CoreEngine.DatabaseManager): _description_
Returns:
_type_: _description_
"""
df = db.get_df_all()
columns_to_convert = df.columns[16:]
df[columns_to_convert] = df[columns_to_convert].map(lambda x: 1 if x > 0 else 0)
df["issue text"] = df["issue text"].apply(CoreEngine.classifier.clean_text)
df["issue description"] = df["issue description"].apply(
CoreEngine.classifier.clean_text
)
# df = CoreEngine.classifier.filter_domains(df) # No filtering on get_all_data
return df
def get_prs_df(
db: CoreEngine.DatabaseManager,
prs: list[int],
repo: CoreEngine.database_manager.Repository,
):
"""Get extracted data for a specific series of PRs from a given repository
Args:
db (CoreEngine.DatabaseManager): Database connector
prs (list[int]): List of PR numbers to pull from
repo (Repository): Repository to pull from
Returns:
pd.Dataframe: Dataframe of the extracted data.
"""
df = db.get_df(prs, repo)
columns_to_convert = df.columns[16:]
df[columns_to_convert] = df[columns_to_convert].map(lambda x: 1 if x > 0 else 0)
df["issue text"] = df["issue text"].apply(CoreEngine.classifier.clean_text)
df["issue description"] = df["issue description"].apply(
CoreEngine.classifier.clean_text
)
# Filter to reduce domains returned.
# df = CoreEngine.classifier.filter_domains(df)
return df
if __name__ == "__main__":
main()