đ GPT2 for Code AutoComplete Model
A code completion plugin for Python that leverages GPT2 to automatically complete lines and blocks of code.
đ Quick Start
The open - source repository code - autocomplete supports the GPT2 model. Here's how to use it:
from autocomplete.gpt2_coder import GPT2Coder
m = GPT2Coder("shibing624/code-autocomplete-distilgpt2-python")
print(m.generate('import torch.nn as')[0])
You can also use huggingface/transformers
.
â ī¸ Important Note
Please use 'GPT2' related functions to load this model!
import os
from transformers import GPT2Tokenizer, GPT2LMHeadModel
os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE"
tokenizer = GPT2Tokenizer.from_pretrained("shibing624/code-autocomplete-distilgpt2-python")
model = GPT2LMHeadModel.from_pretrained("shibing624/code-autocomplete-distilgpt2-python")
prompts = [
"""from torch import nn
class LSTM(Module):
def __init__(self, *,
n_tokens: int,
embedding_size: int,
hidden_size: int,
n_layers: int):""",
"""import numpy as np
import torch
import torch.nn as""",
"import java.util.ArrayList",
"def factorial(n):",
]
for prompt in prompts:
input_ids = tokenizer.encode(prompt, add_special_tokens=False, return_tensors='pt')
outputs = model.generate(input_ids=input_ids,
max_length=64 + len(prompt),
temperature=1.0,
top_k=50,
top_p=0.95,
repetition_penalty=1.0,
do_sample=True,
num_return_sequences=1,
length_penalty=2.0,
early_stopping=True)
decoded = tokenizer.decode(outputs[0], skip_special_tokens=True)
print(decoded)
print("=" * 20)
Output Example
from torch import nn
class LSTM(Module):
def __init__(self, *,
n_tokens: int,
embedding_size: int,
hidden_size: int,
n_layers: int):
self.embedding_size = embedding_size
====================
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
Model Files
code-autocomplete-distilgpt2-python
âââ config.json
âââ merges.txt
âââ pytorch_model.bin
âââ special_tokens_map.json
âââ tokenizer_config.json
âââ vocab.json
đ Documentation
Training Data
Pytorch_awesome projects source code
You can download code - autocomplete and run the following commands:
cd autocomplete
python create_dataset.py
If you want to train the code - autocomplete GPT2 model, refer to https://github.com/shibing624/code-autocomplete/blob/main/autocomplete/gpt2_coder.py
About GPT2
You can test the whole generation capabilities here: https://transformer.huggingface.co/doc/gpt2-large
The GPT2 model is a pretrained model on the English language using a causal language modeling (CLM) objective. It was introduced in this paper and first released at this page.
Disclaimer: The team releasing GPT - 2 also wrote a model card for their model. Content from this model card has been written by the Hugging Face team to complete the information they provided and give specific examples of bias.
đ License
This project is licensed under the Apache - 2.0 license.
đ Citation
@misc{code-autocomplete,
author = {Xu Ming},
title = {code-autocomplete: Code AutoComplete with GPT model},
year = {2022},
publisher = {GitHub},
journal = {GitHub repository},
url = {https://github.com/shibing624/code-autocomplete},
}