|
#!/usr/bin/env python |
|
# coding: utf-8 |
|
|
|
import argparse |
|
import os |
|
import time |
|
|
|
import openai |
|
from tqdm import tqdm |
|
|
|
|
|
def generate_docstring(file, openai_token): |
|
openai.api_key = openai_token |
|
|
|
with open(file) as f: |
|
lines = f.read() |
|
|
|
functions = lines.split('def ')[1:] |
|
functions[-1] = functions[-1].split('if __name__')[0] |
|
|
|
i = 1 |
|
|
|
for func in tqdm(functions): |
|
|
|
func_tokens = 'def ' + func.replace(' ', ' ').strip() |
|
if '"""' in func_tokens: |
|
func_tokens = ''.join( |
|
func_tokens.split('"""')[0::2]).strip().replace('\n\n', '\n') |
|
|
|
func_name = func_tokens.split('(')[0].split('def ')[1] |
|
print(f'# >>>>>>>>>>>>>>>>>>>> METHOD/FUNCTION: {func_name}\n') |
|
|
|
prompt_tokens = len(func_tokens) / 2 |
|
max_tokens = int(4000 - prompt_tokens) |
|
|
|
response = openai.Completion.create( |
|
engine="code-davinci-002", |
|
prompt=f"# Python 3.7\n \n{func_tokens}\n \n# An elaborate, " |
|
"high quality docstring for the above function in google " |
|
"format:\n\"\"\"", |
|
temperature=0, |
|
max_tokens=max_tokens, |
|
top_p=1, |
|
frequency_penalty=0, |
|
presence_penalty=0.2, |
|
stop=["#", "\"\"\""]) |
|
|
|
try: |
|
resp = response['choices'][0]['text'].strip() |
|
except KeyError as e: |
|
print(f'ERROR!: {e}') |
|
|
|
if resp.startswith(' '): |
|
resp = resp[5:] |
|
print(' """' + resp.strip()) |
|
print(' """') |
|
print(f'\n{"-" * 80}\n') |
|
|
|
if i != len(functions): |
|
time.sleep(20) |
|
i += 1 |
|
|
|
|
|
def opts() -> argparse.Namespace: |
|
parser = argparse.ArgumentParser() |
|
parser.add_argument('-f', |
|
'--file', |
|
help='Path to the input file', |
|
type=str, |
|
required=True) |
|
parser.add_argument('-t', |
|
'--token', |
|
help='OpenAI token', |
|
type=str, |
|
default=os.getenv('OPENAI_API_KEY')) |
|
return parser.parse_args() |
|
|
|
|
|
if __name__ == '__main__': |
|
args = opts() |
|
generate_docstring(file=args.file, openai_token=args.token) |