Hello, everyone.
If Python code is automatically converted into other programming languages, will you try it?
Today I will share with you an interesting project, which can automatically convert Python code into C code. This may be helpful to students who are engaged in algorithms.
This project is a GitHub open source project. It is very simple to use. After downloading, install the dependencies and run the corresponding Python script.
python3 python2cppconverter.py
Take the following Python code as an example
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))
The converted code into C is as follows:
// 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; }
python2cppconverter.py Actual The above is to call OpenAI's API to complete code conversion. The core code is as follows:
openai.Completion.create(engine='code-davinci-002', prompt=input_prompt, temperature=temperature, max_tokens=num_tokens, stream=STREAM, stop='===================n', top_p=1.0, frequency_penalty=0.0, presence_penalty=0.0)
is essentially a function call, where the engine parameter is the code conversion model.
Before introducing code-davinci-002, let’s first understand the GPT-3 model.
GPT-3 is a model released by OpenAI in May 2020. This model contains 175 billion parameters, which is two orders of magnitude more than GPT-2 and is a great improvement over GPT-2.
GPT-3 achieves strong performance on many NLP datasets, including translation, question answering, and cloze tasks, as well as some tasks that require on-the-fly reasoning or domain adaptation, such as deciphering words or performing arithmetic operations. The
code-davinci-002 model is a descendant of GPT-3, and its training data contains natural language and billions of lines of public code from GitHub. Therefore, it can understand and generate code, and is proficient in more than a dozen programming languages, and is best at Python.
So, the code-davinci-002 model can not only convert between programming languages, but also convert between natural languages, find bugs in the code, write documents based on the code, etc.
For example, the following example is to generate a textual description for the code function
Code to natural language
You don’t have to worry about writing it in the future Annotated?
Look at the following example again, you can generate docstring for Python code
docstring
For other examples, you can see the OpenAI official website.
The above is beautiful, but the reality is cruel.
Since I downloaded this project, I have been stuck every step of the way.
The OpenAI API used above is not open to domestic users. Domestic friends will see the following prompt when applying to use it
Not available in China
My neck got stuck when I came up!
Of course, a domestic team has provided a solution. You can buy a ready-made foreign account for 18 yuan, which is valid for nearly 3 months and comes with $18 in the account. Therefore, OpenAI’s model is paid and not accurate at all.
When you buy an account, obtain the API key, configure it in the project, run the project, and prepare to witness the miracle of time, you will find the following error:
No such model: code-davinci-002
This is because the code-davinci-002 model is in private testing and requires an application to be submitted and passed before it can be called.
Finally, I do not recommend that you run the code-davinci-002 model. For domestic friends, the cost is too high. If you want to see the effect, you can go to the OpenAI official website to read the documentation and run some examples online.
Although the project did not run successfully, this does not prevent us from learning technology. This wave of force is not a loss.
I hope today’s content is useful to you. Thank you for your attention. I will continue to share excellent AI projects.
The above is the detailed content of Python code is automatically converted into other programming language codes. For more information, please follow other related articles on the PHP Chinese website!