Package: codeclarity
CodeClarity- Code Embeddings Made EasyAbout CodeClarityThis repository contains [CodeClarity] a lightweight app for creating contextual embeddings of source code in a format that is optimized and designed with code search and understanding.
in mind. This repository is part of a larger application providing a free exploration into the documatic codesearch tools capabilities.InstallationWe recommendPython 3.7or higher,PyTorch 1.6.0or higher andtransformers v4.6.0or higher. The code doesnotwork with Python 2.Install with pip (not currently live, coming soon.)Install thecodclaritywithpip:pip install -U sentence-transformersInstall from sourcesAlternatively, you can also clone the latest version from therepositoryand install it directly from the source code:pip install -e .PyTorch with CUDAIf you want to use a GPU / CUDA, you must install PyTorch with the matching CUDA Version. FollowPyTorch - Get Startedfor further details how to install PyTorch.Getting StartedFirst download a pretrained code model.fromencoderimportCodeEmbeddermodel=CodeEmbedder(base_model="microsoft/unixcoder-base")Then provide some code snippits to the model. These can be full functions that could
be parsed by an Abstract Syntax Tree, or small snippits.code_snippits=['def read_csvs(dir) : return [pd.read_csv(fp) for fp in os.listdir(dir)]',"def set_pytorch_device(): return torch.device('cuda') if torch.cuda.is_available() else 'cpu",'read file from disk into pandas dataframe']code_embeddings=model.encode(code_snippits)And that's it! We now have a list of returned embeddings of default type numpy array.forcode,embeddinginzip(code_snippits,code_embeddings):print("Sentence:",code)print("Embedding:",embedding)print("")API Drop inThis project additionally impliments a docker container that serves a python REST api with the package running in it to serve a given model. to automatically build the container with any of the supported models for code search by running the followinggit clone https://github.com/DocumaticAI/code-embeddings-api.git 
cd api && bash ./setup.shEqually, to run the API outside the docker container, just clone the repository, navigate to the API folder and run the API file directlygit clone https://github.com/DocumaticAI/code-embeddings-api.git 
pip install -r requirements-dev.txt
cd api
python predictor.pyPre-Trained ModelsWe provide implimentations of a range of code embedding models that are currently the state of the art in various tasks, including code semantic search, code clustering, code program detection, synthesis and more.  Some models are general purpose models, while others produce embeddings for specific use cases. Pre-trained models can be loaded by just passing the model name:CodeEmbedder('model_name').Currently supported modelsCodeBERT (base model): A Pre-Trained Model for Programming and Natural LanguagesCodeBERT (python finetuned on codesearchnet corpus): A Pre-Trained Model for Programming and Natural LanguagesUniXcoder (base model): Unified Cross-Modal Pre-training for Code RepresentationUniXcoder (9 language varient): Unified Cross-Modal Pre-training for Code RepresentationUniXcoder (unimodal varient): Unified Cross-Modal Pre-training for Code RepresentationInCoder 1B parameter model: A Generative Model for Code Infilling and SynthesisInCoder 6B parameter model: A Generative Model for Code Infilling and SynthesisInternals of docker APICodeClarity is designed to be a simple, modular dockerized python application that can be used to optain dense vector representations of natrual language code queries, and source code jointly to empower semantic search of codebases.It is comprised of a lightweight, async fastapi application running on a guicorn webserver. On startup, any of the supported models will be injected into the container, converted to an optimized serving format, and run on a REST API.CodeClarity automatically handles checking for supported languages for code models, dynamic batching of both code and natrual language snippits and conversions of code models to torchscript all in an asyncronous manner!PublicationsThe following papers are implimented or used heavily in this repo and this project would not be possible without their work:CodeBERT: A Pre-Trained Model for Programming and Natural LanguagesUniXcoder: Unified Cross-Modal Pre-training for Code Representation(EMNLP 2020)InCoder: A Generative Model for Code Infilling and SynthesisA Conversational Paradigm for Program SynthesisCodeT5: Identifier-aware Unified Pre-trained Encoder-Decoder Models for Code Understanding and Generation(EMNLP 2021)About DocumaticDocumaticis the company that deliversa more efficient codebase in 5 minutes. While you focus on coding, Documatic handles, creates and deploys the documentation so it's always up to date.Getting helpIf you have any questions about, feedback for or a problem with Codeclarity:Readdocumatic website.Sign up for thedocumatic Waitlist.File an issue or request a feature.