This repository has been archived by the owner on Jan 31, 2022. It is now read-only.
/
issue_label_predictor.py
166 lines (127 loc) · 5.35 KB
/
issue_label_predictor.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
161
162
163
164
165
import logging
import os
from code_intelligence import embeddings
from label_microservice import combined_model
from label_microservice import repo_specific_model
from label_microservice import universal_kind_label_model as universal_model
UNIVERSAL_MODEL_NAME = "universal"
def _combined_model_name(org, repo):
"""Return the name of the combined model for a repo"""
return f"{org}/{repo}_combined"
def _dict_has_keys(d, keys):
for k in keys:
if not k in d:
return False
return True
class IssueLabelPredictor:
"""Predict labels for an issue.
This class combines various model classes with logic to fetch information
about the issue.
This class doesn't attach the labels to the issues.
"""
def __init__(self):
self._models = {}
self._load_models()
def _load_models(self):
logging.info("Loading the universal model")
self._models[UNIVERSAL_MODEL_NAME] = universal_model.UniversalKindLabelModel()
# TODO(jlewi): How should we get a list of all models for which we
# have repo specific models. mlbot is doing this based on a config
# file; https://github.com/machine-learning-apps/Issue-Label-Bot/blob/26d8fb65be3b39de244c4be9e32b2838111dac10/flask_app/forward_utils.py#L5
for org_and_repo in [("kubeflow", "kubeflow")]:
org = org_and_repo[0]
repo = org_and_repo[1]
logging.info(f"Loading model for repo {org}/{repo}")
repo_model = repo_specific_model.RepoSpecificLabelModel.from_repo(
org, repo,
embedding_api_endpoint=os.environ.get("ISSUE_EMBEDDING_SERVICE"))
self._models[f"{org}/{repo}"] = repo_model
combined = combined_model.CombinedLabelModels(
models=[self._models["universal"], repo_model])
self._models[_combined_model_name(org, repo)] = combined
def predict_labels_for_data(self, model_name, title, body, context=None):
"""Generate label predictions for the specified data.
Args:
model_name: Which model to use
title: Title for the issue
body: body of the issue
Returns
dict: str -> float; dictionary mapping labels to their probability
"""
if not model_name in self._models:
raise ValueError(f"No model named {model_name}")
model = self._models[model_name]
logging.info(f"Generating predictions for title={title} text={body}")
predictions = model.predict_issue_labels(title, body, context=context)
return predictions
def predict_labels_for_issue(self, org, repo, issue_number, model_name=None):
"""Generate label predictions for a github issue.
The function contacts GitHub to collect the required data.
Args:
org: The GitHub organization
repo: The repo that owns the issue
number: The github issue number
model_name: (Optional) the name of the model to use to generate
predictions. if not supplied it is inferred based on the repository.
Returns
dict: str -> float; dictionary mapping labels to their probability
"""
if not model_name:
repo_model = _combined_model_name(org, repo)
if repo_model in self._models:
model_name = repo_model
else:
model_name = UNIVERSAL_MODEL_NAME
logging.info(f"Predict labels for "
f"{org}/{repo}#{issue_number} using "
f"model {model_name}")
data = embeddings.get_issue_text(issue_number, None, org, repo)
if not data.get("title"):
logging.warning(f"Got empty title for {org}/{repo}#{issue_number}")
if not data.get("body"):
logging.warning(f"Got empty title for {org}/{repo}#{issue_number}")
context={
"repo_owner": org,
"repo_name": repo,
"issue_num": issue_number,
}
predictions = self.predict_labels_for_data(
model_name, data.get("title"), data.get("body"), context=context)
return predictions
def predict(self, data):
"""Generate predictions for the specified payload.
Args: data a dictionary containing the data to generate predictions for.
The payload can either look like
{
"title": "some issue title"
"text": "text for some issue
"model_name": Name of model to use
...
}
in this case predictions will be generated for this title and text.
or
{
"repo_owner": <GitHub owner of the issue>
"repo_name": <GitHub repo>
"issue_num": <Issue number>
"model_name": (optional) name of the model to use
...
}
"""
text_keys = ["title", "text", "model_name"]
issue_keys = ["repo_owner", "repo_name", "issue_num"]
if _dict_has_keys(data, text_keys):
return self.predict_labels_for_data(data["model_name"], data["title"],
data["text"])
elif _dict_has_keys(data, issue_keys):
return self.predict_labels_for_issue(data["repo_owner"],
data["repo_name"],
data["issue_num"],
model_name=data.get("model_name"))
else:
actual = ",".join(data.keys())
text_str = ",".join(text_keys)
issue_str = ",".join(issue_keys)
want = f"[{text_str}] or [{issue_str}]"
logging.error(f"Data is missing required keys; got {actual}; want {want}")
raise ValueError(f"Data is missing required keys; got {actual}; want {want}")