Spaces:
Runtime error
Runtime error
Add application file
Browse files
app.py
ADDED
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
+
from transformers import AutoTokenizer, T5ForConditionalGeneration, AutoModelForSeq2SeqLM
|
4 |
+
import torch
|
5 |
+
|
6 |
+
tokenizer = AutoTokenizer.from_pretrained("microsoft/codereviewer")
|
7 |
+
|
8 |
+
tokenizer.special_dict = {
|
9 |
+
f"<e{i}>": tokenizer.get_vocab()[f"<e{i}>"] for i in range(99, -1, -1)
|
10 |
+
}
|
11 |
+
tokenizer.mask_id = tokenizer.get_vocab()["<mask>"]
|
12 |
+
tokenizer.bos_id = tokenizer.get_vocab()["<s>"]
|
13 |
+
tokenizer.pad_id = tokenizer.get_vocab()["<pad>"]
|
14 |
+
tokenizer.eos_id = tokenizer.get_vocab()["</s>"]
|
15 |
+
tokenizer.msg_id = tokenizer.get_vocab()["<msg>"]
|
16 |
+
tokenizer.keep_id = tokenizer.get_vocab()["<keep>"]
|
17 |
+
tokenizer.add_id = tokenizer.get_vocab()["<add>"]
|
18 |
+
tokenizer.del_id = tokenizer.get_vocab()["<del>"]
|
19 |
+
tokenizer.start_id = tokenizer.get_vocab()["<start>"]
|
20 |
+
tokenizer.end_id = tokenizer.get_vocab()["<end>"]
|
21 |
+
|
22 |
+
model = AutoModelForSeq2SeqLM.from_pretrained("microsoft/codereviewer")
|
23 |
+
|
24 |
+
model.eval()
|
25 |
+
|
26 |
+
MAX_SOURCE_LENGTH = 512
|
27 |
+
|
28 |
+
def pad_assert(tokenizer, source_ids):
|
29 |
+
source_ids = source_ids[:MAX_SOURCE_LENGTH - 2]
|
30 |
+
source_ids = [tokenizer.bos_id] + source_ids + [tokenizer.eos_id]
|
31 |
+
pad_len = MAX_SOURCE_LENGTH - len(source_ids)
|
32 |
+
source_ids += [tokenizer.pad_id] * pad_len
|
33 |
+
assert len(source_ids) == MAX_SOURCE_LENGTH, "Not equal length."
|
34 |
+
return source_ids
|
35 |
+
|
36 |
+
|
37 |
+
def encode_diff(tokenizer, diff, msg, source):
|
38 |
+
difflines = diff.split("\n")[1:] # remove start @@
|
39 |
+
difflines = [line for line in difflines if len(line.strip()) > 0]
|
40 |
+
map_dic = {"-": 0, "+": 1, " ": 2}
|
41 |
+
|
42 |
+
def f(s):
|
43 |
+
if s in map_dic:
|
44 |
+
return map_dic[s]
|
45 |
+
else:
|
46 |
+
return 2
|
47 |
+
|
48 |
+
labels = [f(line[0]) for line in difflines]
|
49 |
+
difflines = [line[1:].strip() for line in difflines]
|
50 |
+
inputstr = "<s>" + source + "</s>"
|
51 |
+
inputstr += "<msg>" + msg
|
52 |
+
for label, line in zip(labels, difflines):
|
53 |
+
if label == 1:
|
54 |
+
inputstr += "<add>" + line
|
55 |
+
elif label == 0:
|
56 |
+
inputstr += "<del>" + line
|
57 |
+
else:
|
58 |
+
inputstr += "<keep>" + line
|
59 |
+
source_ids = tokenizer.encode(inputstr, max_length=MAX_SOURCE_LENGTH, truncation=True)[1:-1]
|
60 |
+
source_ids = pad_assert(tokenizer, source_ids)
|
61 |
+
return source_ids
|
62 |
+
|
63 |
+
|
64 |
+
class FileDiffs(object):
|
65 |
+
def __init__(self, diff_string):
|
66 |
+
diff_array = diff_string.split("\n")
|
67 |
+
self.file_name = diff_array[0]
|
68 |
+
self.file_path = self.file_name.split("a/", 1)[1].rsplit("b/", 1)[0]
|
69 |
+
self.diffs = list()
|
70 |
+
for line in diff_array[4:]:
|
71 |
+
if line.startswith("@@"):
|
72 |
+
self.diffs.append(str())
|
73 |
+
self.diffs[-1] += "\n" + line
|
74 |
+
|
75 |
+
|
76 |
+
def review_commit(user, repository, commit):
|
77 |
+
commit_metadata = requests.get(F"https://api.github.com/repos/{user}/{repository}/commits/{commit}").json()
|
78 |
+
msg = commit_metadata["commit"]["message"]
|
79 |
+
|
80 |
+
diff_data = requests.get(F"https://api.github.com/repos/{user}/{repository}/commits/{commit}", headers={"Accept":"application/vnd.github.diff"})
|
81 |
+
code_diff = diff_data.text
|
82 |
+
|
83 |
+
files_diffs = list()
|
84 |
+
for file in code_diff.split("diff --git"):
|
85 |
+
if len(file) > 0:
|
86 |
+
fd = FileDiffs(file)
|
87 |
+
files_diffs.append(fd)
|
88 |
+
|
89 |
+
output = ""
|
90 |
+
for fd in files_diffs:
|
91 |
+
output += F"File:{fd.file_path}\n"
|
92 |
+
source = requests.get(F"https://raw.githubusercontent.com/{user}/{repository}/^{commit}/{fd.file_path}").text
|
93 |
+
|
94 |
+
for diff in fd.diffs:
|
95 |
+
inputs = torch.tensor([encode_diff(tokenizer, diff, msg, source)], dtype=torch.long).to("cpu")
|
96 |
+
inputs_mask = inputs.ne(tokenizer.pad_id)
|
97 |
+
preds = model.generate(inputs,
|
98 |
+
attention_mask=inputs_mask,
|
99 |
+
use_cache=True,
|
100 |
+
num_beams=5,
|
101 |
+
early_stopping=True,
|
102 |
+
max_length=100,
|
103 |
+
num_return_sequences=2
|
104 |
+
)
|
105 |
+
preds = list(preds.cpu().numpy())
|
106 |
+
pred_nls = [tokenizer.decode(id[2:], skip_special_tokens=True, clean_up_tokenization_spaces=False) for id in
|
107 |
+
preds]
|
108 |
+
output += diff + "\n#######\nComment:\n#######\n" + pred_nls[0] + "\n#######\n"
|
109 |
+
return output
|
110 |
+
|
111 |
+
|
112 |
+
iface = gr.Interface(fn=review_commit, inputs=["text", "text", "text"], outputs="text")
|
113 |
+
iface.launch()
|