Update README.md
Browse files
README.md
CHANGED
|
@@ -25,29 +25,25 @@ tokenizer = pipe.tokenizer
|
|
| 25 |
|
| 26 |
|
| 27 |
def prepare_prompt(commit_message: str, git_diff: str, context_window: int = 1024):
|
| 28 |
-
prompt_head = "<s>[INST] <<SYS>>\nYou are a commit classifier based on commit message and code diff.Please classify the given commit into one of the ten categories: docs, perf, style, refactor, feat, fix, test, ci, build, and chore. The definitions of each category are as follows:\n**feat**: Code changes aim to introduce new features to the codebase, encompassing both internal and user-oriented features.\n**fix**: Code changes aim to fix bugs and faults within the codebase.\n**perf**: Code changes aim to improve performance, such as enhancing execution speed or reducing memory consumption.\n**style**: Code changes aim to improve readability without affecting the meaning of the code. This type encompasses aspects like variable naming, indentation, and addressing linting or code analysis warnings.\n**refactor**: Code changes aim to restructure the program without changing its behavior, aiming to improve maintainability. To avoid confusion and overlap, we propose the constraint that this category does not include changes classified as ``perf'' or ``style''. Examples include enhancing modularity, refining exception handling, improving scalability, conducting code cleanup, and removing deprecated code.\n**docs**: Code changes that modify documentation or text, such as correcting typos, modifying comments, or updating documentation.\n**test**: Code changes that modify test files, including the addition or updating of tests.\n**ci**: Code changes to CI (Continuous Integration) configuration files and scripts, such as configuring or updating CI/CD scripts, e.g., ``.travis.yml'' and ``.github/workflows''.\n**build**: Code changes affecting the build system (e.g., Maven, Gradle, Cargo). Change examples include updating dependencies, configuring build configurations, and adding scripts.\n**chore**: Code changes for other miscellaneous tasks that do not neatly fit into any of the above categories.\n<</SYS>>\n\n"
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
f"- given commit message:\n{commit_message}\n"
|
| 32 |
-
max_length=64,
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
)
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
add_special_tokens=False,
|
| 41 |
-
)
|
| 42 |
-
prompt_end = tokenizer.encode(" [/INST]", add_special_tokens=False)
|
| 43 |
-
return tokenizer.decode(prompt_head + prompt_message + prompt_diff + prompt_end)
|
| 44 |
|
| 45 |
|
| 46 |
def classify_commit(commit_message: str, git_diff: str, context_window: int = 1024):
|
| 47 |
-
prompt = prepare_prompt(commit_message, git_diff, context_window)
|
| 48 |
-
result = pipe(prompt, max_new_tokens=10, pad_token_id=pipe.tokenizer.eos_token_id)
|
| 49 |
-
label = result[0]["generated_text"].split()[-1]
|
| 50 |
-
return label
|
| 51 |
|
| 52 |
```
|
| 53 |
|
|
@@ -58,21 +54,21 @@ import requests
|
|
| 58 |
from github import Github
|
| 59 |
|
| 60 |
def fetch_message_and_diff(repo_name, commit_sha):
|
| 61 |
-
g = Github()
|
| 62 |
-
try:
|
| 63 |
-
repo = g.get_repo(repo_name)
|
| 64 |
-
commit = repo.get_commit(commit_sha)
|
| 65 |
-
if commit.parents:
|
| 66 |
-
parent_sha = commit.parents[0].sha
|
| 67 |
-
diff_url = repo.compare(parent_sha, commit_sha).diff_url
|
| 68 |
-
return commit.commit.message, requests.get(diff_url).text
|
| 69 |
-
else:
|
| 70 |
-
raise ValueError("No parent found for this commit, unable to retrieve diff.")
|
| 71 |
-
except Exception as e:
|
| 72 |
-
raise RuntimeError(f"Error retrieving commit information: {e}")
|
| 73 |
|
| 74 |
message, diff = fetch_message_and_diff("pytorch/pytorch", "9856bc50a251ac054debfdbbb5ed29fc4f6aeb39")
|
| 75 |
print(classify_commit(message, diff))
|
| 76 |
```
|
| 77 |
|
| 78 |
-
In this setup, we've defined a function `fetch_message_and_diff` that fetches the commit message and diff for any specified SHA from a GitHub repository, enabling our model to classify the commit accordingly.
|
|
|
|
| 25 |
|
| 26 |
|
| 27 |
def prepare_prompt(commit_message: str, git_diff: str, context_window: int = 1024):
|
| 28 |
+
prompt_head = "<s>[INST] <<SYS>>\nYou are a commit classifier based on commit message and code diff.Please classify the given commit into one of the ten categories: docs, perf, style, refactor, feat, fix, test, ci, build, and chore. The definitions of each category are as follows:\n**feat**: Code changes aim to introduce new features to the codebase, encompassing both internal and user-oriented features.\n**fix**: Code changes aim to fix bugs and faults within the codebase.\n**perf**: Code changes aim to improve performance, such as enhancing execution speed or reducing memory consumption.\n**style**: Code changes aim to improve readability without affecting the meaning of the code. This type encompasses aspects like variable naming, indentation, and addressing linting or code analysis warnings.\n**refactor**: Code changes aim to restructure the program without changing its behavior, aiming to improve maintainability. To avoid confusion and overlap, we propose the constraint that this category does not include changes classified as ``perf'' or ``style''. Examples include enhancing modularity, refining exception handling, improving scalability, conducting code cleanup, and removing deprecated code.\n**docs**: Code changes that modify documentation or text, such as correcting typos, modifying comments, or updating documentation.\n**test**: Code changes that modify test files, including the addition or updating of tests.\n**ci**: Code changes to CI (Continuous Integration) configuration files and scripts, such as configuring or updating CI/CD scripts, e.g., ``.travis.yml'' and ``.github/workflows''.\n**build**: Code changes affecting the build system (e.g., Maven, Gradle, Cargo). Change examples include updating dependencies, configuring build configurations, and adding scripts.\n**chore**: Code changes for other miscellaneous tasks that do not neatly fit into any of the above categories.\n<</SYS>>\n\n"
|
| 29 |
+
prompt_head_encoded = tokenizer.encode(prompt_head, add_special_tokens=False)
|
| 30 |
+
|
| 31 |
+
prompt_message = f"- given commit message:\n{commit_message}\n"
|
| 32 |
+
prompt_message_encoded = tokenizer.encode(prompt_message, max_length=64, truncation=True, add_special_tokens=False)
|
| 33 |
+
|
| 34 |
+
prompt_diff = f"- given commit diff: \n{git_diff}\n"
|
| 35 |
+
remaining_length = (context_window - len(prompt_head_encoded) - len(prompt_message_encoded) - 6)
|
| 36 |
+
prompt_diff_encoded = tokenizer.encode(prompt_diff, max_length=remaining_length, truncation=True, add_special_tokens=False)
|
| 37 |
+
|
| 38 |
+
prompt_end = tokenizer.encode(" [/INST]", add_special_tokens=False)
|
| 39 |
+
return tokenizer.decode(prompt_head_encoded + prompt_message_encoded + prompt_diff_encoded + prompt_end)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
|
| 42 |
def classify_commit(commit_message: str, git_diff: str, context_window: int = 1024):
|
| 43 |
+
prompt = prepare_prompt(commit_message, git_diff, context_window)
|
| 44 |
+
result = pipe(prompt, max_new_tokens=10, pad_token_id=pipe.tokenizer.eos_token_id)
|
| 45 |
+
label = result[0]["generated_text"].split()[-1]
|
| 46 |
+
return label
|
| 47 |
|
| 48 |
```
|
| 49 |
|
|
|
|
| 54 |
from github import Github
|
| 55 |
|
| 56 |
def fetch_message_and_diff(repo_name, commit_sha):
|
| 57 |
+
g = Github()
|
| 58 |
+
try:
|
| 59 |
+
repo = g.get_repo(repo_name)
|
| 60 |
+
commit = repo.get_commit(commit_sha)
|
| 61 |
+
if commit.parents:
|
| 62 |
+
parent_sha = commit.parents[0].sha
|
| 63 |
+
diff_url = repo.compare(parent_sha, commit_sha).diff_url
|
| 64 |
+
return commit.commit.message, requests.get(diff_url).text
|
| 65 |
+
else:
|
| 66 |
+
raise ValueError("No parent found for this commit, unable to retrieve diff.")
|
| 67 |
+
except Exception as e:
|
| 68 |
+
raise RuntimeError(f"Error retrieving commit information: {e}")
|
| 69 |
|
| 70 |
message, diff = fetch_message_and_diff("pytorch/pytorch", "9856bc50a251ac054debfdbbb5ed29fc4f6aeb39")
|
| 71 |
print(classify_commit(message, diff))
|
| 72 |
```
|
| 73 |
|
| 74 |
+
In this setup, we've defined a function `fetch_message_and_diff` that fetches the commit message and diff for any specified SHA from a GitHub repository, enabling our model to classify the commit accordingly.
|