If you will Python
Code is automatically converted to other programming languages , Will you try ?
Today I would like to share an interesting project , It can Python
The code is automatically converted to C++
Code . This may be of some help to students engaged in algorithms .
1. How to achieve
The project is GitHub Open source project , Very simple to use , Install dependencies after downloading , Run the corresponding Python
The script can be .
python3 python2cppconverter.py
With the following Python
Code, for 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))
convert to C++
The code 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
It's actually a call OpenAI
Of 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)
It is essentially a function call , among engine
The parameter is the transcoding model .
Introduce code-davinci-002
Before , So let's see GPT-3
Model .
GPT-3
yes OpenAI
stay 2020 year 5 A model released in June , This model contains 1750 100 million parameters , Than GPT-2
Two orders of magnitude more , Than GPT-2 There has been a great improvement .
GPT-3
In many NLP
Powerful performance on the dataset , Including translation 、 Q & A and cloze tasks , And tasks that require immediate reasoning or domain adaptation , For example, reading words or performing arithmetic operations .
code-davinci-002
The model is GPT-3
The offspring of , Its training data includes natural language and data from GitHub
Billions of lines of public code . therefore , It can understand and generate code , Proficient in more than ten programming languages , Best at Python
.
therefore ,code-davinci-002
The model can not only realize the transformation between programming languages , It can also interact with natural language 、 Find... In the code BUG、 Write documents according to the code, etc .
For example, the following example is to generate textual descriptions for code functions
Code to natural language
Don't worry about writing notes in the future ?
Let's look at the following example , It can be for Python
Code generation docstring
docstring
You can see other examples OpenAI
Official website .
2. A bad news
It says very well , But the reality is cruel .
Since I downloaded this project , From the beginning of operation, one pit at a time .
Used above OpenAI
API
, It is not open to China , Domestic friends will see the following tips when applying for use
It can't be used in China
I got stuck when I came up !
Of course , Some domestic teams have provided solutions , flowers 18 Yuan can buy a ready-made foreign account , The term of validity is approaching 3 Months , Self contained in account 18 dollar . therefore ,OpenAI
The model of is a charge , Not really .
When you buy an account , Got API key
, Configure it into the project , Run the project , Ready to witness the miracle of the moment , You will find the following error reports :
No such model: code-davinci-002
This is because code-davinci-002
The model is in private testing , Application required , Can only be called after passing .
Last , I don't recommend that you run code-davinci-002
Model , For domestic friends , The cost is too high . If you want to see the effect , You can go to OpenAI
Look at the documents on the official website 、 Run some online example
that will do .
Although the project did not run successfully , But this does not prevent us from learning technology , This wave of force does not lose .
I hope today's content is useful to you , Thank you for your attention , I will continue to share excellent AI project .
Project address :https://github.com/alxschwrz/codex_py2cpp
【python Study 】
learn Python The partners , Welcome to join the new exchange 【 Junyang 】:1020465983
Discuss programming knowledge together , Become a great God , There are also software installation packages in the group , Practical cases 、 Learning materials