p4vv37 commited on
Commit
ed03b25
1 Parent(s): b876599
Files changed (1) hide show
  1. app.py +46 -25
app.py CHANGED
@@ -3,28 +3,33 @@ 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]
@@ -73,19 +78,24 @@ class FileDiffs(object):
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"
@@ -103,11 +113,22 @@ def review_commit(user, repository, commit):
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()
 
3
  from transformers import AutoTokenizer, T5ForConditionalGeneration, AutoModelForSeq2SeqLM
4
  import torch
5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
  MAX_SOURCE_LENGTH = 512
8
 
9
+
10
+ def prepare_models():
11
+ tokenizer = AutoTokenizer.from_pretrained("microsoft/codereviewer")
12
+
13
+ tokenizer.special_dict = {
14
+ f"<e{i}>": tokenizer.get_vocab()[f"<e{i}>"] for i in range(99, -1, -1)
15
+ }
16
+ tokenizer.mask_id = tokenizer.get_vocab()["<mask>"]
17
+ tokenizer.bos_id = tokenizer.get_vocab()["<s>"]
18
+ tokenizer.pad_id = tokenizer.get_vocab()["<pad>"]
19
+ tokenizer.eos_id = tokenizer.get_vocab()["</s>"]
20
+ tokenizer.msg_id = tokenizer.get_vocab()["<msg>"]
21
+ tokenizer.keep_id = tokenizer.get_vocab()["<keep>"]
22
+ tokenizer.add_id = tokenizer.get_vocab()["<add>"]
23
+ tokenizer.del_id = tokenizer.get_vocab()["<del>"]
24
+ tokenizer.start_id = tokenizer.get_vocab()["<start>"]
25
+ tokenizer.end_id = tokenizer.get_vocab()["<end>"]
26
+
27
+ model = AutoModelForSeq2SeqLM.from_pretrained("microsoft/codereviewer")
28
+
29
+ model.eval()
30
+ return tokenizer, model
31
+
32
+
33
  def pad_assert(tokenizer, source_ids):
34
  source_ids = source_ids[:MAX_SOURCE_LENGTH - 2]
35
  source_ids = [tokenizer.bos_id] + source_ids + [tokenizer.eos_id]
 
78
  self.diffs[-1] += "\n" + line
79
 
80
 
81
+ def review_commit(user="p4vv37", repository="ueflow", commit="610a8c7b02b946bc9e5e26e6dacbba0e2abba259"):
82
+ tokenizer, model = prepare_models()
83
+
84
+ # Get diff and commit metadata from GitHub API
85
  commit_metadata = requests.get(F"https://api.github.com/repos/{user}/{repository}/commits/{commit}").json()
86
  msg = commit_metadata["commit"]["message"]
87
+ diff_data = requests.get(F"https://api.github.com/repos/{user}/{repository}/commits/{commit}",
88
+ headers={"Accept": "application/vnd.github.diff"})
89
  code_diff = diff_data.text
90
 
91
+ # Parse diff into FileDiffs objects
92
  files_diffs = list()
93
  for file in code_diff.split("diff --git"):
94
  if len(file) > 0:
95
  fd = FileDiffs(file)
96
  files_diffs.append(fd)
97
 
98
+ # Generate comments for each diff
99
  output = ""
100
  for fd in files_diffs:
101
  output += F"File:{fd.file_path}\n"
 
113
  num_return_sequences=2
114
  )
115
  preds = list(preds.cpu().numpy())
116
+ pred_nls = [tokenizer.decode(_id[2:], skip_special_tokens=True, clean_up_tokenization_spaces=False)
117
+ for _id in preds]
118
  output += diff + "\n#######\nComment:\n#######\n" + pred_nls[0] + "\n#######\n"
119
  return output
120
 
121
 
122
+ description = "An interface for running " \
123
+ "\"Microsoft CodeBERT CodeReviewer: Pre-Training for Automating Code Review Activities.\" " \
124
+ "(microsoft/codereviewer) on GitHub commits."
125
+ examples = [
126
+ ["p4vv37", "ueflow", "610a8c7b02b946bc9e5e26e6dacbba0e2abba259"],
127
+ ["microsoft", "vscode", "378b0d711f6b82ac59b47fb246906043a6fb995a"],
128
+ ]
129
+ iface = gr.Interface(fn=review_commit,
130
+ description=description,
131
+ inputs=["text", "text", "text"],
132
+ outputs="text",
133
+ examples=examples)
134
  iface.launch()