Giter VIP home page Giter VIP logo

python_autocomplete's Introduction

PyPI - Python Version PyPI Status Join Slack Twitter

Python Autocomplete

The full length Python autocompletion Video and a Twitter thread describing how it works

This is a learning/demo project to show how deep learning can be used to auto complete Python code. You can experiment with LSTM and Transformer models. We also have built a simple VSCode extension to try out the trained models.

Training model: Open In Colab

Evaluating trained model: Open In Colab

It gives quite decent results by saving above 30% key strokes in most files, and close to 50% in some. We calculated key strokes saved by making a single (best) prediction and selecting it with a single key.

The dataset we use is the python code found in repos linked in Awesome-pytorch-list. We download all the repositories as zip files, extract them, remove non python files and split them randomly to build training and validation datasets.

We train a character level model without any tokenization of the source code, since it's the simplest.

Try it yourself

  1. Clone this repo
  2. Install requirements from requirements.txt
  3. Run python_autocomplete/create_dataset.py.
    • It collects repos mentioned in PyTorch awesome list
    • Downloads the zip files of the repos
    • Extract the zips
    • Remove non python files
    • Collect all python code to data/train.py and, data/eval.py
  4. Run python_autocomplete/train.py to train the model. Try changing hyper-parameters like model dimensions and number of layers.
  5. Run evaluate.py to evaluate the model.

You can also run the training notebook on Google Colab.

Open In Colab

VSCode extension

  1. Clone this repo

  2. Install requirements from requirements.txt

  3. Install npm packages

You need to have Node.JS installed

cd vscode_extension
npm install # This will install the NPM packages
  1. Start the server python_autocomplete/serve.py

  2. Open the extension project (folder) in VSCode

cd vscode_extension
code . # This will open vscode_extension in VSCode

If you don't have VSCode command line launcher start VSCode and open the project with File > Open

  1. Run the extension from VSCode
Run > Start Debugging

This will open another VSCode editor window, with the extension

  1. Create or open a python file and start editing!

Sample

Here's a sample evaluation of a trained transformer model.

Colors:

  • yellow: the token predicted is wrong and the user needs to type that character.
  • blue: the token predicted is correct and the user selects it with a special key press, such as TAB or ENTER.
  • green: autocompleted characters based on the prediction

python_autocomplete's People

Contributors

hnipun avatar vpj avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

python_autocomplete's Issues

size mismatch for weights and bias

Hi there. After trained the model, I run "python serve.py" to test whether the model is capable to use, before this I have changed run_uuid to be that of my model and checkpoint. Any idea about why it raises error "RuntimeError: Error(s) in loading state_dict for TransformerXLModel:"? Thanks.


(autocomplete) daijianbo@ubuntu18:~/python_autocomplete-master-old/python_autocomplete$ python serve.py

LABML WARNING
Not a valid git repository: /home/daijianbo/python_autocomplete-master-old

Prepare model...
Prepare n_tokens...
Prepare tokenizer...[DONE] 1.27ms
Prepare n_tokens...[DONE] 2.10ms
Prepare transformer...[DONE] 1.33ms
Prepare ffn...[DONE] 0.30ms
Prepare device...
Prepare device_info...[DONE] 23.29ms
Prepare device...[DONE] 23.51ms
Prepare model...[DONE] 107.18ms
Selected experiment = source_code run = b32da5eea23711eb982bccbbfe110075 checkpoint = 1744896
Loading checkpoint...[FAIL] 840.09ms
Traceback (most recent call last):
File "serve.py", line 18, in
predictor = get_predictor()
File "/home/daijianbo/python_autocomplete-master-old/python_autocomplete/evaluate/factory.py", line 39, in get_predictor conf = load_experiment()
File "/home/daijianbo/python_autocomplete-master-old/python_autocomplete/evaluate/factory.py", line 33, in load_experiment
experiment.start()
File "/home/daijianbo/miniconda3/envs/autocomplete/lib/python3.8/site-packages/labml/experiment.py", line 256, in start
return _experiment_singleton().start(run_uuid=_load_run_uuid, checkpoint=_load_checkpoint)
File "/home/daijianbo/miniconda3/envs/autocomplete/lib/python3.8/site-packages/labml/internal/experiment/init.py", line 407, in start
global_step = self.__start_from_checkpoint(run_uuid, checkpoint)
File "/home/daijianbo/miniconda3/envs/autocomplete/lib/python3.8/site-packages/labml/internal/experiment/init.py", line 312, in __start_from_check point
self._load_checkpoint(checkpoint_path)
File "/home/daijianbo/miniconda3/envs/autocomplete/lib/python3.8/site-packages/labml/internal/experiment/init.py", line 280, in _load_checkpoint
self.checkpoint_saver.load(checkpoint_path)
File "/home/daijianbo/miniconda3/envs/autocomplete/lib/python3.8/site-packages/labml/internal/experiment/init.py", line 118, in load
saver.load(checkpoint_path, info[name])
File "/home/daijianbo/miniconda3/envs/autocomplete/lib/python3.8/site-packages/labml/internal/experiment/pytorch.py", line 66, in load self.model.load_state_dict(state)
File "/home/daijianbo/miniconda3/envs/autocomplete/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1223, in load_state_dict
raise RuntimeError('Error(s) in loading state_dict for {}:\n\t{}'.format(RuntimeError: Error(s) in loading state_dict for TransformerXLModel:
size mismatch for src_embed.weight: copying a param with shape torch.Size([1096, 512]) from checkpoint, the shape in current model is torch.Size([1097, 512]).
size mismatch for generator.weight: copying a param with shape torch.Size([1096, 512]) from checkpoint, the shape in current model is torch.Size([1097, 512]).
size mismatch for generator.bias: copying a param with shape torch.Size([1096]) from checkpoint, the shape in current model is torch.Size([1097]).

BPE not cached

hello, when I tried to training this model, it raised RuntimeError('BPE not cached'). I really wanna know why it happened. Thanks

How to link vscode to the server?

Installed both npm and nodejs on windows. npm -v and node -v both works on the windows terminal.
Server running at port http://192.168.0.243:5000/.
Installed vscode.
Tried to follow read me but when start dubug it requires a config file.
How to link vscode to the server? How to configure vscode the interact with the server and get auto complete options when typing code?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.