Documenting undocumented python scripts using Python-OpenAI

Today, I will discuss another very impressive & innovative new AI, which is now operational in Python. We’ll document a dummy python code with no comment captured through OpenAI’s ChatGPT model. But before we start, don’t we see the demo first?

Demo

Great! Let us understand we can leverage this by writing a tiny snippet using this new AI model.

Architecture:

Let us understand the flow of events –

The above diagram represents the newly released OpenAI ChatGPT, where one needs to supply the code, which was missed to capture the logic earlier due to whatever may be the reasons. We need to provide these scripts (maybe in parts) as source code to be analyzed. Then it will use this new model & translate that into English-like language & capture the logic/comments for that specific snippet.


Python Packages:

Following are the python packages that are necessary to develop this brilliant use case –

pip install pandas
pip install openai

To know more, please click the below – “Continue Reading” link –

CODE:

Let us now understand the code. For this use case, we will only discuss three python scripts. However, we need more than these three. However, we have already discussed them in some of the early posts. Hence, we will skip them here.

  • clsConfigClient.py (Configuration file)


################################################
#### Written By: SATYAKI DE ####
#### Written On: 15-May-2020 ####
#### Modified On: 26-Dec-2022 ####
#### ####
#### Objective: This script is a config ####
#### file, contains all the keys for ####
#### best bowling prediction from a sports ####
#### streaming. ####
#### ####
################################################
import os
import platform as pl
class clsConfigClient(object):
Curr_Path = os.path.dirname(os.path.realpath(__file__))
os_det = pl.system()
if os_det == "Windows":
sep = '\\'
else:
sep = '/'
conf = {
'APP_ID': 1,
'ARCH_DIR': Curr_Path + sep + 'arch' + sep,
'PROFILE_PATH': Curr_Path + sep + 'profile' + sep,
'LOG_PATH': Curr_Path + sep + 'log' + sep,
'REPORT_PATH': Curr_Path + sep + 'output' + sep,
'REPORT_DIR': 'output',
'SRC_PATH': Curr_Path + sep + 'data' + sep,
'CODE_PATH': Curr_Path + sep + 'Code' + sep,
'APP_DESC_1': 'Predicting the direction of football!',
'DEBUG_IND': 'N',
'INIT_PATH': Curr_Path,
'FILE_NAME': 'FirstPart.py',
'TITLE': "Predicting the direction of football!",
'PATH' : Curr_Path,
'OPENAI_API_KEY': "sk-gfjrjIIYRED%KKLkw8373hdtYdevGKdjd734BH",
'MODEL_NAME': "code-davinci-002",
"KEY_Q":"What the above class is doing?"
}

Let us understand a few important entries –

'OPENAI_API_KEY': "sk-gfjrjIIYRED%KKLkw8373hdtYdevGKdjd734BH",
'MODEL_NAME': "code-davinci-002",
"KEY_Q":"What the above class is doing?"

As one can see that the following entries are extremely important for the main application. I’ve shown the process to extract the API Key in the next part. And, by default, the application will be using “code-davinci-002”. And finally, the Key_Q is the main variable, which contains the question that the application will ask OpenAI ChatGPT.

  • writeDoc.py (Main application to invoke the OpenAI API)


#####################################################
#### Written By: SATYAKI DE ####
#### Written On: 26-Dec-2022 ####
#### Modified On 27-Dec-2022 ####
#### ####
#### Objective: This is the main calling ####
#### python script that will invoke the ####
#### ChatGPT OpenAI class to initiate the ####
#### prediction to interpret the python code ####
#### & display the result. ####
#####################################################
import os
import openai
import json
from clsConfigClient import clsConfigClient as cf
import pandas as p
import ssl
import time
import datetime
import logging
from pathlib import Path
# Bypassing SSL Authentication
try:
_create_unverified_https_context = ssl._create_unverified_context
except AttributeError:
# Legacy python that doesn't verify HTTPS certificates by default
pass
else:
# Handle target environment that doesn't support HTTPS verification
ssl._create_default_https_context = _create_unverified_https_context
# Disbling Warning
def warn(*args, **kwargs):
pass
import warnings
warnings.warn = warn
###############################################
### Global Section ###
###############################################
OPENAI_API_KEY=str(cf.conf['OPENAI_API_KEY'])
FILE_NAME=str(cf.conf['FILE_NAME'])
CODE_PATH=str(cf.conf['CODE_PATH'])
MODEL_NAME=str(cf.conf['MODEL_NAME'])
KEY_Q=str(cf.conf['KEY_Q'])
###############################################
### End of Global Section ###
###############################################
def readFile(inputFile):
try:
data = Path(inputFile).read_text()
return data + '""" ' + KEY_Q
except Exception as e:
x = str(e)
print(x)
data = ''
return data
def main():
try:
# Other useful variables
debugInd = 'Y'
var = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
var1 = datetime.datetime.now()
print('Start Time: ', str(var))
# End of useful variables
# Initiating Log Class
general_log_path = str(cf.conf['LOG_PATH'])
# Enabling Logging Info
logging.basicConfig(filename=general_log_path + 'predCode.log', level=logging.INFO)
print('Started predicting & documenting code!')
print('Read Class Name: ')
print(str(FILE_NAME))
FileNameWithPath = CODE_PATH + FILE_NAME
data = readFile(FileNameWithPath)
print('#'*120)
print(data)
print(type(data))
print('#'*120)
# ChatGPT API_KEY
openai.api_key = OPENAI_API_KEY
# Getting response from ChatGPT
response = openai.Completion.create(
model=MODEL_NAME,
prompt=data,
temperature=0,
max_tokens=64,
top_p=1.0,
frequency_penalty=0.0,
presence_penalty=0.0,
stop=["\"\"\""]
)
print('Final Result:')
print('*'*120)
#print(response)
print(response.choices[0].text)
print('*'*120)
var2 = datetime.datetime.now()
c = var2 var1
minutes = c.total_seconds() / 60
print('Total difference in minutes: ', str(minutes))
print('End Time: ', str(var1))
except Exception as e:
x = str(e)
print('Error: ', x)
if __name__ == "__main__":
main()

view raw

writeDoc.py

hosted with ❤ by GitHub

Let us understand the key snippets –

def readFile(inputFile):
    try:
        data = Path(inputFile).read_text()
        return data + '""" ' + KEY_Q
    except Exception as e:
        x = str(e)
        print(x)

        data = ''

        return data

This function appears to read the contents of a file and return it as a string, with some additional text appended to the end. The function also includes a try-except block to handle any exceptions that might occur while trying to read the file & it will append the string'” “”‘and the value of the variable KEY_Q to the end of the file contents and then return and print the resulting string.

If an exception occurs while trying to read the file, the function will print the error message and return an empty string.

Let us understand the process of acquiring API Key –

Please go to the above-mentioned page & under the API_keys (highlighted in Red), and click – “Create new secret key.”

This key will be used later in the next snippet.

def main():
    try:
        # Other useful variables
        debugInd = 'Y'
        var = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
        var1 = datetime.datetime.now()

        print('Start Time: ', str(var))
        # End of useful variables

        # Initiating Log Class
        general_log_path = str(cf.conf['LOG_PATH'])

        # Enabling Logging Info
        logging.basicConfig(filename=general_log_path + 'predCode.log', level=logging.INFO)

        print('Started predicting & documenting code!')
        print('Read Class Name: ')
        print(str(FILE_NAME))

        FileNameWithPath = CODE_PATH + FILE_NAME

        data = readFile(FileNameWithPath)

        print('#'*120)
        print(data)
        print(type(data))
        print('#'*120)

        # ChatGPT API_KEY
        openai.api_key = OPENAI_API_KEY

        # Getting response from ChatGPT
        response = openai.Completion.create(
        model=MODEL_NAME,
        prompt=data,
        temperature=0,
        max_tokens=64,
        top_p=1.0,
        frequency_penalty=0.0,
        presence_penalty=0.0,
        stop=["\"\"\""]
        )

        print('Final Result:')
        print('*'*120)
        #print(response)
        print(response.choices[0].text)
        print('*'*120)

        var2 = datetime.datetime.now()

        c = var2 - var1
        minutes = c.total_seconds() / 60
        print('Total difference in minutes: ', str(minutes))

        print('End Time: ', str(var1))

    except Exception as e:
        x = str(e)
        print('Error: ', x)

This function uses the OpenAI API to generate text based on a prompt. The function reads a file, processes the contents of the file, and then sends the processed data to the OpenAI API as a prompt and also includes a try-except block to handle any exceptions that might occur while processing the file or using the OpenAI API.

Here is a brief overview of what the function does:

  1. Initializes all the variables, including a log file and the current time.
  2.  Reads in the contents of a file using the readFile, a function defined earlier.
  3.  Sends the file’s contents to the OpenAI API as a prompt using the “openai.Completion.create” method.
  4.  It prints the response from the OpenAI API.
  5.  Calculates the time taken to process the file and send the prompt to the OpenAI API.

If an exception occurs at any point during the execution of the function, it will be caught, and the error message will be printed.


FOLDER STRUCTURE:

Let us understand the directory structure –

ANALYZING OUTPUT:

Let us first explore the dummy code, which we’ve considered for this use case –


def predStream(self, img, hsvVals, FrNo):
try:
pT1 = self.pT1
pT2 = self.pT2
pT3 = self.pT3
pT4 = self.pT4
#Find the color ball
imgColor, mask = myColorFinder.update(img, hsvVals)
#Find location of the red_ball
imgContours, contours = cvzone.findContours(img, mask, minArea=500)
if contours:
posListX.append(contours[0]['center'][0])
posListY.append(contours[0]['center'][1])
if posListX:
# Find the Coefficients
A, B, C = np.polyfit(posListX, posListY, 2)
for i, (posX, posY) in enumerate(zip(posListX, posListY)):
pos = (posX, posY)
cv2.circle(imgContours, pos, 10, (0,255,0), cv2.FILLED)
# Using Karman Filter Prediction
predicted = kf.predict(posX, posY)
cv2.circle(imgContours, (predicted[0], predicted[1]), 12, (255,0,255), cv2.FILLED)
ballDetectFlag = True
if ballDetectFlag:
print('Balls Detected!')
if i == 0:
cv2.line(imgContours, pos, pos, (0,255,0), 5)
cv2.line(imgContours, predicted, predicted, (255,0,255), 5)
else:
predictedM = kf.predict(posListX[i1], posListY[i1])
cv2.line(imgContours, pos, (posListX[i1], posListY[i1]), (0,255,0), 5)
cv2.line(imgContours, predicted, predictedM, (255,0,255), 5)
if len(posListX) < 10:
# Calculation for best place to ball
a1 = A
b1 = B
c1 = C pT1
X1 = int(( b1 math.sqrt(b1**2 (4*a1*c1)))/(2*a1))
prediction1 = pT2 < X1 < pT3
a2 = A
b2 = B
c2 = C pT4
X2 = int(( b2 math.sqrt(b2**2 (4*a2*c2)))/(2*a2))
prediction2 = pT2 < X2 < pT3
prediction = prediction1 | prediction2
if prediction:
print('Good Length Ball!')
sMsg = "Good Length Ball – (" + str(FrNo) + ")"
cvzone.putTextRect(imgContours, sMsg, (50,150), scale=5, thickness=5, colorR=(0,200,0), offset=20)
else:
print('Loose Ball!')
sMsg = "Loose Ball – (" + str(FrNo) + ")"
cvzone.putTextRect(imgContours, sMsg, (50,150), scale=5, thickness=5, colorR=(0,0,200), offset=20)
return imgContours
except Exception as e:
x = str(e)
print('Error predStream:', x)
return img

view raw

FirstPart.py

hosted with ❤ by GitHub

So, finally, we’ve done it.

You will get the complete codebase in the following GitHub link.

I’ll bring some more exciting topics in the coming days from the Python verse. Please share & subscribe to my post & let me know your feedback.

Till then, Happy Avenging! 😀

Note: All the data & scenarios posted here are representational data & scenarios & available over the internet & for educational purposes only. Some of the images (except my photo) we’ve used are available over the net. We don’t claim ownership of these images. There is always room for improvement & especially in the prediction quality.