Update README.md
Browse files
README.md
CHANGED
|
@@ -1,138 +1,140 @@
|
|
| 1 |
-
---
|
| 2 |
-
language:
|
| 3 |
-
- en
|
| 4 |
-
tags:
|
| 5 |
-
- code
|
| 6 |
-
- autocomplete
|
| 7 |
-
- pytorch
|
| 8 |
-
- en
|
| 9 |
-
license:
|
| 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 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
language:
|
| 3 |
+
- en
|
| 4 |
+
tags:
|
| 5 |
+
- code
|
| 6 |
+
- autocomplete
|
| 7 |
+
- pytorch
|
| 8 |
+
- en
|
| 9 |
+
license: apache-2.0
|
| 10 |
+
library_name: transformers
|
| 11 |
+
pipeline_tag: text-generation
|
| 12 |
+
widget:
|
| 13 |
+
- text: import torch.nn as
|
| 14 |
+
---
|
| 15 |
+
|
| 16 |
+
# GPT2 for Code AutoComplete Model
|
| 17 |
+
code-autocomplete, a code completion plugin for Python.
|
| 18 |
+
|
| 19 |
+
**code-autocomplete** can automatically complete the code of lines and blocks with GPT2.
|
| 20 |
+
|
| 21 |
+
## Usage
|
| 22 |
+
|
| 23 |
+
Open source repo:[code-autocomplete](https://github.com/shibing624/code-autocomplete),support GPT2 model, usage:
|
| 24 |
+
|
| 25 |
+
```python
|
| 26 |
+
from autocomplete.gpt2_coder import GPT2Coder
|
| 27 |
+
|
| 28 |
+
m = GPT2Coder("shibing624/code-autocomplete-distilgpt2-python")
|
| 29 |
+
print(m.generate('import torch.nn as')[0])
|
| 30 |
+
```
|
| 31 |
+
|
| 32 |
+
Also, use huggingface/transformers:
|
| 33 |
+
|
| 34 |
+
*Please use 'GPT2' related functions to load this model!*
|
| 35 |
+
|
| 36 |
+
```python
|
| 37 |
+
import os
|
| 38 |
+
from transformers import GPT2Tokenizer, GPT2LMHeadModel
|
| 39 |
+
|
| 40 |
+
os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE"
|
| 41 |
+
|
| 42 |
+
tokenizer = GPT2Tokenizer.from_pretrained("shibing624/code-autocomplete-distilgpt2-python")
|
| 43 |
+
model = GPT2LMHeadModel.from_pretrained("shibing624/code-autocomplete-distilgpt2-python")
|
| 44 |
+
|
| 45 |
+
prompts = [
|
| 46 |
+
"""from torch import nn
|
| 47 |
+
class LSTM(Module):
|
| 48 |
+
def __init__(self, *,
|
| 49 |
+
n_tokens: int,
|
| 50 |
+
embedding_size: int,
|
| 51 |
+
hidden_size: int,
|
| 52 |
+
n_layers: int):""",
|
| 53 |
+
"""import numpy as np
|
| 54 |
+
import torch
|
| 55 |
+
import torch.nn as""",
|
| 56 |
+
"import java.util.ArrayList",
|
| 57 |
+
"def factorial(n):",
|
| 58 |
+
]
|
| 59 |
+
for prompt in prompts:
|
| 60 |
+
input_ids = tokenizer.encode(prompt, add_special_tokens=False, return_tensors='pt')
|
| 61 |
+
outputs = model.generate(input_ids=input_ids,
|
| 62 |
+
max_length=64 + len(prompt),
|
| 63 |
+
temperature=1.0,
|
| 64 |
+
top_k=50,
|
| 65 |
+
top_p=0.95,
|
| 66 |
+
repetition_penalty=1.0,
|
| 67 |
+
do_sample=True,
|
| 68 |
+
num_return_sequences=1,
|
| 69 |
+
length_penalty=2.0,
|
| 70 |
+
early_stopping=True)
|
| 71 |
+
decoded = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 72 |
+
print(decoded)
|
| 73 |
+
print("=" * 20)
|
| 74 |
+
```
|
| 75 |
+
|
| 76 |
+
output:
|
| 77 |
+
```shell
|
| 78 |
+
from torch import nn
|
| 79 |
+
class LSTM(Module):
|
| 80 |
+
def __init__(self, *,
|
| 81 |
+
n_tokens: int,
|
| 82 |
+
embedding_size: int,
|
| 83 |
+
hidden_size: int,
|
| 84 |
+
n_layers: int):
|
| 85 |
+
self.embedding_size = embedding_size
|
| 86 |
+
====================
|
| 87 |
+
import numpy as np
|
| 88 |
+
import torch
|
| 89 |
+
import torch.nn as nn
|
| 90 |
+
import torch.nn.functional as F
|
| 91 |
+
```
|
| 92 |
+
|
| 93 |
+
Model files:
|
| 94 |
+
```
|
| 95 |
+
code-autocomplete-distilgpt2-python
|
| 96 |
+
├── config.json
|
| 97 |
+
├── merges.txt
|
| 98 |
+
├── pytorch_model.bin
|
| 99 |
+
├── special_tokens_map.json
|
| 100 |
+
├── tokenizer_config.json
|
| 101 |
+
└── vocab.json
|
| 102 |
+
```
|
| 103 |
+
|
| 104 |
+
### Train data
|
| 105 |
+
#### pytorch_awesome projects source code
|
| 106 |
+
|
| 107 |
+
download [code-autocomplete](https://github.com/shibing624/code-autocomplete),
|
| 108 |
+
```shell
|
| 109 |
+
cd autocomplete
|
| 110 |
+
python create_dataset.py
|
| 111 |
+
```
|
| 112 |
+
|
| 113 |
+
If you want train code-autocomplete GPT2 model,refer [https://github.com/shibing624/code-autocomplete/blob/main/autocomplete/gpt2_coder.py](https://github.com/shibing624/code-autocomplete/blob/main/autocomplete/gpt2_coder.py)
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
### About GPT2
|
| 117 |
+
|
| 118 |
+
Test the whole generation capabilities here: https://transformer.huggingface.co/doc/gpt2-large
|
| 119 |
+
|
| 120 |
+
Pretrained model on English language using a causal language modeling (CLM) objective. It was introduced in
|
| 121 |
+
[this paper](https://d4mucfpksywv.cloudfront.net/better-language-models/language_models_are_unsupervised_multitask_learners.pdf)
|
| 122 |
+
and first released at [this page](https://openai.com/blog/better-language-models/).
|
| 123 |
+
|
| 124 |
+
Disclaimer: The team releasing GPT-2 also wrote a
|
| 125 |
+
[model card](https://github.com/openai/gpt-2/blob/master/model_card.md) for their model. Content from this model card
|
| 126 |
+
has been written by the Hugging Face team to complete the information they provided and give specific examples of bias.
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
## Citation
|
| 130 |
+
|
| 131 |
+
```latex
|
| 132 |
+
@misc{code-autocomplete,
|
| 133 |
+
author = {Xu Ming},
|
| 134 |
+
title = {code-autocomplete: Code AutoComplete with GPT model},
|
| 135 |
+
year = {2022},
|
| 136 |
+
publisher = {GitHub},
|
| 137 |
+
journal = {GitHub repository},
|
| 138 |
+
url = {https://github.com/shibing624/code-autocomplete},
|
| 139 |
+
}
|
| 140 |
+
```
|