Skip to content

Commit da089fe

Browse files
committed
add tests.
1 parent ffcfb53 commit da089fe

10 files changed

Lines changed: 16451 additions & 93 deletions

File tree

README.md

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,10 @@ code-autocomplete, a code completion plugin for Python.
2121
- [Reference](#reference)
2222

2323
# Feature
24-
25-
26-
# Demo
27-
28-
http://42.193.145.218/product/short_text_sim/
24+
- GPT2-based code completion
25+
- Code completion for Python, other language is coming soon
26+
- Line and block completion
27+
- Train(Fine-tune GPT2) and predict model with your own data
2928

3029
# Install
3130
```
@@ -46,7 +45,7 @@ python3 setup.py install
4645
### Code Completion
4746

4847

49-
开源项目:[code-autocomplete](https://github.com/shibing624/code-autocomplete),可支持GPT2模型,通过如下命令调用:
48+
基于GPT2模型预测补全代码,通过如下命令调用:
5049

5150
```python
5251
from autocomplete.gpt2 import Infer
@@ -55,6 +54,10 @@ i = m.predict('import torch.nn as')
5554
print(i)
5655
```
5756

57+
output:
58+
```shell
59+
import torch.nn as nn
60+
```
5861
当然,你也可使用官方的huggingface/transformers调用:
5962

6063
*Please use 'GPT2' related functions to load this model!*
@@ -81,7 +84,7 @@ prompts = [
8184
"""import numpy as np
8285
import torch
8386
import torch.nn as""",
84-
"import java.util.ArrayList",
87+
"import java.util.ArrayList;",
8588
"def factorial(n):",
8689
]
8790
for prompt in prompts:
@@ -101,6 +104,27 @@ for prompt in prompts:
101104
print("=" * 20)
102105
```
103106

107+
output:
108+
```python
109+
from torch import nn
110+
class LSTM(Module):
111+
def __init__(self, *,
112+
n_tokens: int,
113+
embedding_size: int,
114+
hidden_size: int,
115+
n_layers: int):
116+
self.hidden_size = hidden_size
117+
self.embedding_size = embedding_size
118+
119+
====================
120+
121+
import numpy as np
122+
import torch
123+
import torch.nn as nn
124+
125+
====================
126+
...
127+
```
104128

105129

106130
# Contact

autocomplete/gpt2.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
import torch
88
from simpletransformers.language_generation import LanguageGenerationModel
99
from simpletransformers.language_modeling import LanguageModelingModel
10+
import transformers
1011

12+
transformers.logging.set_verbosity_error()
1113
use_cuda = torch.cuda.is_available()
1214

1315

@@ -55,13 +57,13 @@ def __init__(self, model_name="gpt2", model_dir="outputs/fine-tuned", use_cuda=u
5557
# cache_dir: None means use default cache dir: ~/.cache/huggingface/transformers/
5658
self.model = LanguageGenerationModel(model_name, model_dir, args=args, use_cuda=use_cuda)
5759

58-
def predict(self, query):
60+
def predict(self, prompt):
5961
"""
6062
Generate text using the model. Verbose set to False to prevent logging generated sequences.
61-
:param query: str, input string
63+
:param prompt: str, input string
6264
:return: str
6365
"""
64-
generated = self.model.generate(query, verbose=False)
66+
generated = self.model.generate(prompt, verbose=False)
6567
generated = generated[0]
6668
return generated
6769

examples/base_demo.py

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,13 @@
33
@author:XuMing(xuming624@qq.com)
44
@description:
55
"""
6-
import argparse
76
import sys
87
import torch
98

109
sys.path.append('..')
1110
from autocomplete.gpt2 import Infer
1211

1312
use_cuda = torch.cuda.is_available()
14-
if __name__ == '__main__':
15-
prompts = [
16-
"""from torch import nn
17-
class LSTM(Module):
18-
def __init__(self, *,
19-
n_tokens: int,
20-
embedding_size: int,
21-
hidden_size: int,
22-
n_layers: int):""",
23-
"""import numpy as np
24-
import torch
25-
import torch.nn as""",
26-
"import java.util.ArrayList",
27-
]
28-
infer = Infer(model_name="gpt2", model_dir="shibing624/code-autocomplete-gpt2-base", use_cuda=use_cuda)
29-
for prompt in prompts:
30-
res = infer.predict(prompt)
31-
print("Query:", prompt)
32-
print("Result:", res)
33-
print("=" * 20)
13+
m = Infer(model_name="gpt2", model_dir="shibing624/code-autocomplete-gpt2-base", use_cuda=use_cuda)
14+
i = m.predict('import torch.nn as')
15+
print(i)

examples/gpt2_demo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def __init__(self, *,
3434
"""import numpy as np
3535
import torch
3636
import torch.nn as""",
37-
"import java.util.ArrayList",
37+
"import java.util.ArrayList;",
3838
]
3939
predict_with_original_gpt2(prompts)
4040
if args.do_train:

0 commit comments

Comments
 (0)