Skip to content

Commit ff401a1

Browse files
authored
Merge pull request #16 from thepushkarp/patch
Patches
2 parents a409284 + 01faefd commit ff401a1

22 files changed

Lines changed: 264 additions & 139 deletions

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ htmlcov/
4747
.nox/
4848
.coverage
4949
.coverage.*
50-
!.cache
50+
.cache
5151
nosetests.xml
5252
coverage.xml
5353
*.cover

README.md

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
<a href="https://github.com/thepushkarp/nalcos/stargazers"><img alt="Stargazers" src="https://img.shields.io/github/stars/thepushkarp/nalcos?style=for-the-badge"></a>
88
<a href="https://github.com/thepushkarp/nalcos/blob/main/LICENSE"><img alt="License" src="https://img.shields.io/github/license/thepushkarp/nalcos?style=for-the-badge"></a>
99
<a href="https://github.com/thepushkarp/nalcos/releases"><img alt="GitHub release (latest by date)" src="https://img.shields.io/github/v/release/thepushkarp/nalcos?style=for-the-badge"></a>
10+
<a href="https://github.com/psf/black"><img alt="Code style: black" src="https://img.shields.io/badge/code%20style-black-000000.svg?style=for-the-badge"></a>
11+
<a href="https://pypi.org/project/nalcos/"><img alt="PyPi" src="https://img.shields.io/pypi/v/nalcos?style=for-the-badge"></a>
1012
</p>
1113

1214
---
@@ -36,8 +38,6 @@ Though there are various improvements left, I'm happy with what this initially t
3638

3739
## Requirements
3840

39-
Tested on Python 3.8.11.
40-
4141
NaLCoS uses the following packages:
4242

4343
- [Sentence Transformers](https://github.com/UKPLab/sentence-transformers) for the Transformer model.
@@ -47,39 +47,51 @@ NaLCoS uses the following packages:
4747

4848
## Installation
4949

50+
### Installing with `pip` (Recommended)
51+
52+
Install with `pip` or your favourite PyPi manager:
53+
54+
```console
55+
$ pip install nalcos
56+
```
57+
58+
Run NaLCoS with the `--help` flag to see all the available options:
59+
60+
```console
61+
$ nalcos --help
62+
```
63+
64+
**Note**: When you run the `nalcos` command for the first time, it will, download the model which would be cached and used the next time you run NaLCoS.
65+
66+
### Installing bleeding edge from the GitHub repository
67+
5068
- Clone the repository:
5169

5270
```console
5371
$ git clone https://github.com/thepushkarp/nalcos.git
5472
```
5573

56-
This also downloads the model weights stored in the `nalcos/.cache` directory so you don't have to download them while running the model for the first time.
74+
This also downloads the model weights stored in the `nalcos/models` directory so you don't have to download them while running the model for the first time.
5775

5876
- Create a virtual environment ([click here](https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/) to read about activating virtualenv):
5977

6078
```
6179
$ virtualenv venv
6280
```
6381

64-
<details open>
65-
<summary>Activate virtualenv (for Linux and MacOS):</summary>
82+
- Activate virtualenv (for Linux and MacOS):
6683

6784
```console
6885
$ source ./venv/bin/activate
6986
```
7087

71-
</details>
72-
73-
<details open>
74-
<summary>Activate virtualenv (for Windows):</summary>
88+
- Activate virtualenv (for Windows):
7589

7690
```console
7791
$ cd venv/Scripts/
7892
$ activate
7993
```
8094

81-
</details>
82-
8395
- Install the requirements:
8496

8597
```console
@@ -92,10 +104,10 @@ $ pip install -r requirements.txt
92104
$ cd nalcos/
93105
```
94106

95-
- Run NaLCoS:
107+
- Run NaLCoS with the `--help` flag to see all the available options:
96108

97109
```console
98-
$ python nalcos.py [-g] [-n N_MATCHES] [-b BRANCH] [-l LOOK_PAST] "query string" "repository location"
110+
$ python nalcos.py --help
99111
```
100112

101113
## Usage
@@ -130,7 +142,7 @@ optional arguments:
130142
- Input:
131143

132144
```console
133-
python nalcos.py "improve language" "github/docs" --github
145+
$ python nalcos.py "improve language" "github/docs" --github
134146
```
135147

136148
- Output:
@@ -156,17 +168,9 @@ Found 100 commits.
156168
157169
```
158170

159-
160171
## Future plans
161172

162-
- [x] Documentation
163-
- [x] Release first working version
164-
- [ ] Publish to PyPi
165-
- [ ] Add automated tests
166-
- [ ] Add personal API token support to increase GitHub API rate limit
167-
- [ ] Use a Python GitHub API wrapper [?]
168-
- [ ] Look into ways to cache and store embeddings to reduce repeated computations [?]
169-
- [ ] Try other models [?]
173+
Please visit the [NaLCoS To Do Project Board](https://github.com/thepushkarp/nalcos/projects/1) to see current status and future plans.
170174

171175
## Known issues
172176

@@ -175,6 +179,14 @@ Not all retrieved results are always relevant. I could think of two primary reas
175179
- The data the model was pre-trained on is not representative of how people write commit messages. Since commit messages usually contain technical jargon, merge commit messages, abbreviations and other non-common terms, the model (which has a limited vocabulary) is not able to generalize well to this data.
176180
- Two commits may be related even when their commit messages may not be similar and similarly two commit messages maybe unrelated even when their commit messages are similar. We often need more metadata (such as lines changes, files changed) etc. to make the predictions more accurate.
177181

182+
## Contributing
183+
184+
Any suggestions, improvements or bug reports are welcome.
185+
186+
- If you want to discuss any aspect of the project, please use the [Discussions Tab](https://github.com/thepushkarp/nalcos/discussions).
187+
- You can submit your idea by [opening an issue](https://github.com/thepushkarp/nalcos/issues/new/choose) or [creating a Pull Request](https://github.com/thepushkarp/nalcos/pulls).
188+
- If you'd like to improve the code, make sure you stick to the existing code style and naming conventions.
189+
178190
## License
179191

180192
This project is licensed under the terms of the MIT license.

nalcos/_version.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__version__ = "0.1.1"

nalcos/get_commits.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import requests
66
from git import Repo
77

8-
from utils import get_owner_and_repo
8+
from .utils import get_owner_and_repo
99

1010
__all__ = ["get_local_commits", "get_github_commits"]
1111

nalcos/get_similar_commits.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import typing
33
from pathlib import Path
44
from appdirs import user_cache_dir
5+
import torch
56
from sentence_transformers import SentenceTransformer, util
67

78
__all__ = ["get_similar_commits"]
@@ -30,25 +31,32 @@ def get_similar_commits(
3031
# The model to use for encoding the query and commit messages.
3132
# Pretrained models available at: https://www.sbert.net/docs/pretrained_models.html
3233
model_name = "multi-qa-MiniLM-L6-cos-v1"
34+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
3335
# The path to save the model to.
3436
cache_folder = user_cache_dir(
35-
os.path.join(Path(__file__).resolve().parent, ".cache")
37+
os.path.join(Path(__file__).resolve().parent, "models")
3638
)
3739

3840
# Load the model.
39-
model = SentenceTransformer(model_name, cache_folder=cache_folder)
41+
model = SentenceTransformer(model_name, device=device, cache_folder=cache_folder)
4042

4143
# Get all the commit messages.
4244
commit_messages = [commit["message"] for commit in commits]
4345

4446
# Encode all the commit messages.
45-
commit_embeddings = model.encode(commit_messages, convert_to_tensor=True)
47+
commit_embeddings = model.encode(
48+
commit_messages, convert_to_tensor=True, normalize_embeddings=True
49+
)
4650

4751
# Encode the query.
48-
query_embedding = model.encode([query], convert_to_tensor=True)
52+
query_embedding = model.encode(
53+
[query], convert_to_tensor=True, normalize_embeddings=True
54+
)
4955

5056
# Use cosine similarity to find the most similar commits.
51-
cosine_scores = util.pytorch_cos_sim(query_embedding, commit_embeddings).squeeze()
57+
# Since the returned tensors are normalized, we can use the faster dot product here. Reference:
58+
# https://www.sbert.net/examples/applications/computing-embeddings/README.html?highlight=faster%20dot-product
59+
cosine_scores = util.dot_score(query_embedding, commit_embeddings).squeeze()
5260

5361
# Get the indices of the most similar commits.
5462
sorted_scores_indices = cosine_scores.argsort().tolist()[::-1][:n_matches]

nalcos/.cache/Cache/sentence-transformers_multi-qa-MiniLM-L6-cos-v1/.gitattributes renamed to nalcos/models/Cache/sentence-transformers_multi-qa-MiniLM-L6-cos-v1/.gitattributes

File renamed without changes.

nalcos/.cache/Cache/sentence-transformers_multi-qa-MiniLM-L6-cos-v1/1_Pooling/config.json renamed to nalcos/models/Cache/sentence-transformers_multi-qa-MiniLM-L6-cos-v1/1_Pooling/config.json

File renamed without changes.

nalcos/.cache/Cache/sentence-transformers_multi-qa-MiniLM-L6-cos-v1/README.md renamed to nalcos/models/Cache/sentence-transformers_multi-qa-MiniLM-L6-cos-v1/README.md

File renamed without changes.

nalcos/.cache/Cache/sentence-transformers_multi-qa-MiniLM-L6-cos-v1/config.json renamed to nalcos/models/Cache/sentence-transformers_multi-qa-MiniLM-L6-cos-v1/config.json

File renamed without changes.

nalcos/.cache/Cache/sentence-transformers_multi-qa-MiniLM-L6-cos-v1/config_sentence_transformers.json renamed to nalcos/models/Cache/sentence-transformers_multi-qa-MiniLM-L6-cos-v1/config_sentence_transformers.json

File renamed without changes.

0 commit comments

Comments
 (0)