Converts python code into c++ by using OpenAI CODEX.

Overview

🦾 codex_py2cpp 🤖

OpenAI Codex Python to C++ Code Generator

Your Python Code is too slow? 🐌 You want to speed it up but forgot how to code in C++? Convert your Python script to C++ Code using OpenAI Codex.

How it works

Reads a Python file and creates an input prompt which is then fed to OpenAI Codex to generate corresponding C++ code. The generated code is getting compiled using g++ and if compilation is successful the executable is saved.

To generate your own files you need to get access to the Codex API (https://openai.com/blog/openai-codex/).

Installation

git clone https://github.com/alxschwrz/codex_py2cpp.git
cd codex_py2cpp
pip3 install -r requirements.txt

Run example

Reads the file "simpleScript.py", and feeds the corresponding input prompt to OpenAI Codex. Compilable solutions are stored in the form of .cpp and .exe files.

python3 python2cppconverter.py

If the generated C++ code got compiled, test it with

./simpleScript.exe

You hopefully get the same output as when running

python3 simpleScript.py

Check how much faster you are now ...

time ./simpleScript.exe
time python3 simpleScript.py

Example Code Generation:

[In]: Python Snippet [Out]: How the CODEX conversion might look like
def add_something(x, y):
    print("casually adding some stuff together")
    z = x + y
    return z


if __name__ == "__main__":
    print('Okay, lets go')
    print(add_something(5, 2))
// C++ Code generated from Python Code: 
#include <iostream>
using namespace std;

int add_something(int x, int y) {
    cout << "casually adding some stuff together" << endl;
    int z = x + y;
    return z;
}

int main() {
    cout << "Okay, lets go" << endl;
    cout << add_something(5, 2) << endl;
    return 0;
}

Please test your generated code before usage. This does not produce robust code conversions, but is intended to experiment with codex. [WIP]

Credits

This project is based on the OpenAI Codex project. Inspired by https://github.com/tom-doerr

You might also like...
OpenAI CLIP text encoders for multiple languages!
OpenAI CLIP text encoders for multiple languages!

Multilingual-CLIP OpenAI CLIP text encoders for any language Colab Notebook · Pre-trained Models · Report Bug Overview OpenAI recently released the pa

Use Tensorflow2.7.0 Build OpenAI'GPT-2
Use Tensorflow2.7.0 Build OpenAI'GPT-2

TF2_GPT-2 Use Tensorflow2.7.0 Build OpenAI'GPT-2 使用最新tensorflow2.7.0构建openai官方的GPT-2 NLP模型 优点 使用无监督技术 拥有大量词汇量 可实现续写(堪比“xx梦续写”) 实现对话后续将应用于FloatTech的Bot

Weaviate demo with the text2vec-openai module

Weaviate demo with the text2vec-openai module This repository contains an example of how to use the Weaviate text2vec-openai module. When using this d

This is a modification of the OpenAI-CLIP repository of moein-shariatnia

This is a modification of the OpenAI-CLIP repository of moein-shariatnia

Text to speech is a process to convert any text into voice. Text to speech project takes words on digital devices and convert them into audio. Here I have used Google-text-to-speech library popularly known as gTTS library to convert text file to .mp3 file. Hope you like my project! When doing audio and video sentiment recognition, I found that a lot of code is duplicated, often a function in different time debugging for a long time, based on this problem, I want to manage all the previous work, organized into an open source library can be iterative. For their own use and others.
Code for "Finetuning Pretrained Transformers into Variational Autoencoders"

transformers-into-vaes Code for Finetuning Pretrained Transformers into Variational Autoencoders (our submission to NLP Insights Workshop 2021). Gathe

Code for the paper TestRank: Bringing Order into Unlabeled Test Instances for Deep Learning Tasks

TestRank in Pytorch Code for the paper TestRank: Bringing Order into Unlabeled Test Instances for Deep Learning Tasks by Yu Li, Min Li, Qiuxia Lai, Ya

Integrating the Best of TF into PyTorch, for Machine Learning, Natural Language Processing, and Text Generation.  This is part of the CASL project: http://casl-project.ai/
Integrating the Best of TF into PyTorch, for Machine Learning, Natural Language Processing, and Text Generation. This is part of the CASL project: http://casl-project.ai/

Texar-PyTorch is a toolkit aiming to support a broad set of machine learning, especially natural language processing and text generation tasks. Texar

Comments
  • openai.error.InvalidRequestError: That model does not exist

    openai.error.InvalidRequestError: That model does not exist

    Hello,i want to convert my pyscript to cpp,but i think there are some troubles File "python2cppconverter.py", line 131, in <module> iterate_for_compilable_solution(prompt=prompt, maxIterations=3) File "python2cppconverter.py", line 111, in iterate_for_compilable_solution response = generate_completion(prompt, num_tokens=MAX_TOKENS_DEFAULT) File "python2cppconverter.py", line 73, in generate_completion top_p=1.0, frequency_penalty=0.0, presence_penalty=0.0) File "/home/dby/anaconda3/envs/rl/lib/python3.7/site-packages/openai/api_resources/completion.py", line 25, in create return super().create(*args, **kwargs) File "/home/dby/anaconda3/envs/rl/lib/python3.7/site-packages/openai/api_resources/abstract/engine_api_resource.py", line 122, in create request_timeout=request_timeout, File "/home/dby/anaconda3/envs/rl/lib/python3.7/site-packages/openai/api_requestor.py", line 181, in request resp, got_stream = self._interpret_response(result, stream) File "/home/dby/anaconda3/envs/rl/lib/python3.7/site-packages/openai/api_requestor.py", line 397, in _interpret_response result.content, result.status_code, result.headers, stream=False File "/home/dby/anaconda3/envs/rl/lib/python3.7/site-packages/openai/api_requestor.py", line 430, in _interpret_response_line rbody, rcode, resp.data, rheaders, stream_error=stream_error openai.error.InvalidRequestError: That model does not exist

    opened by IDCARDBORING 3
  • OpenAI engine needed.

    OpenAI engine needed.

    Hi, nice work, but I could not convert your "simpleScript.py" using "python2cppconverter.py" because I got the error:

    openai.error.InvalidRequestError: No such model: code-davinci-002

    But, worked with text-davinci-002 engine.

    opened by argenta-ma 0
  • Is this only for simple print script or?

    Is this only for simple print script or?

    Is this only for simple python script. like few lines of print? I am asking because some 'harder' python script can't be converted. Always errors!

    I am trying on Ubuntu 22.04 and I don't need *.exe but *.cpp.

    For example, this is very simple code and it can't be converted without errors:

    import sys
    py_ver = sys.version[0]
    
    def is_url_file_alive(url, timeout):
        try:
            if py_ver == '2':
                import urllib2
                try:
                    request = urllib2.Request(url)
                    response = urllib2.urlopen(request, timeout=timeout)
                    return True
                except:
                    return False
            else:
                import urllib.request, urllib.error, urllib.parse
                try:
                    request = urllib.request.Request(url)
                    response = urllib.request.urlopen(request, timeout=timeout)
                    return True
                except:
                    return False
        except:
            return False
    

    above code come with errors:

    C++ File: simpleScript.cpp
    Compiled Executable: simpleScript.exe
    simpleScript.cpp:2:35: error: ‘string’ in namespace ‘std’ does not name a type
        2 | bool is_url_file_alive(const std::string& url, int timeout)
          |                                   ^~~~~~
    simpleScript.cpp:1:1: note: ‘std::string’ is defined in header ‘<string>’; did you forget to ‘#include <string>’?
      +++ |+#include <string>
        1 | // C++ Code generated from Python Code:
    cnc@cnc-x:~/codex_py2cpp$ simpleScript.cpp: In function ‘bool is_url_file_alive(const int&, int)’:
    simpleScript.cpp:6:14: error: ‘string’ is not a member of ‘std’
        6 |         std::string command = "wget --spider " + url + " -t 1 -T " + std::to_string(timeout);
          |              ^~~~~~
    simpleScript.cpp:6:14: note: ‘std::string’ is defined in header ‘<string>’; did you forget to ‘#include <string>’?
    simpleScript.cpp:7:26: error: ‘command’ was not declared in this scope
        7 |         int ret = system(command.c_str());
          |                          ^~~~~~~
    simpleScript.cpp:7:19: error: ‘system’ was not declared in this scope
        7 |         int ret = system(command.c_str());
          |                   ^~~~~~
    

    Some bigger python scripts can't even starts to convert. I can't put the codes here.

    opened by FrancHR 0
Owner
Alexander
M.Sc. Candidate Robotics, Cognition, Intelligence, TUM
Alexander
Converts text into a PDF of handwritten notes

Text To Handwritten Notes Converts text into a PDF of handwritten notes Explore the docs » · Report Bug · Request Feature · Steps: $ git clone https:/

UVSinghK 63 Oct 9, 2022
Galois is an auto code completer for code editors (or any text editor) based on OpenAI GPT-2.

Galois is an auto code completer for code editors (or any text editor) based on OpenAI GPT-2. It is trained (finetuned) on a curated list of approximately 45K Python (~470MB) files gathered from the Github. Currently, it just works properly on Python but not bad at other languages (thanks to GPT-2's power).

Galois Autocompleter 91 Sep 23, 2022
An easy to use, user-friendly and efficient code for extracting OpenAI CLIP (Global/Grid) features from image and text respectively.

Extracting OpenAI CLIP (Global/Grid) Features from Image and Text This repo aims at providing an easy to use and efficient code for extracting image &

Jianjie(JJ) Luo 13 Jan 6, 2023
A simple command line tool for text to image generation, using OpenAI's CLIP and a BigGAN

artificial intelligence cosmic love and attention fire in the sky a pyramid made of ice a lonely house in the woods marriage in the mountains lantern

Phil Wang 2.3k Jan 1, 2023
This project converts your human voice input to its text transcript and to an automated voice too.

Human Voice to Automated Voice & Text Introduction: In this project, whenever you'll speak, it will turn your voice into a robot voice and furthermore

Hassan Shahzad 3 Oct 15, 2021
Python package to easily retrain OpenAI's GPT-2 text-generating model on new texts

gpt-2-simple A simple Python package that wraps existing model fine-tuning and generation scripts for OpenAI's GPT-2 text generation model (specifical

Max Woolf 3.1k Jan 7, 2023
Python package to easily retrain OpenAI's GPT-2 text-generating model on new texts

gpt-2-simple A simple Python package that wraps existing model fine-tuning and generation scripts for OpenAI's GPT-2 text generation model (specifical

Max Woolf 2.5k Feb 17, 2021
Implementation / replication of DALL-E, OpenAI's Text to Image Transformer, in Pytorch

Implementation / replication of DALL-E, OpenAI's Text to Image Transformer, in Pytorch

Phil Wang 5k Jan 2, 2023
Simple Text-Generator with OpenAI gpt-2 Pytorch Implementation

GPT2-Pytorch with Text-Generator Better Language Models and Their Implications Our model, called GPT-2 (a successor to GPT), was trained simply to pre

Tae-Hwan Jung 775 Jan 8, 2023
Bot to connect a real Telegram user, simulating responses with OpenAI's davinci GPT-3 model.

AI-BOT Bot to connect a real Telegram user, simulating responses with OpenAI's davinci GPT-3 model.

Thempra 2 Dec 21, 2022