File size: 3,631 Bytes
3479e9b
 
 
 
 
 
 
 
 
 
 
c24cd36
3479e9b
02f96f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3479e9b
 
 
 
 
 
 
 
0010519
 
 
3479e9b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0010519
3479e9b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#the below import has been replaced by the later mentioned import, recently by langchain as a per of their improvement strategy :)
#from langchain.chat_models import ChatOpenAI
from langchain_openai import ChatOpenAI

from langchain.schema import HumanMessage, SystemMessage
from io import StringIO
import streamlit as st
from dotenv import load_dotenv
import time
import base64
import os
import openai

class ChatOpenAI:
    def __init__(self, api_key, model_name="gpt-3.5-turbo", temperature=0.9):
        self.api_key = api_key
        self.model_name = model_name
        self.temperature = temperature
        openai.api_key = self.api_key

    def chat(self, prompt):
        response = openai.ChatCompletion.create(
            model=self.model_name,
            messages=[{"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": prompt}],
            temperature=self.temperature
        )
        return response['choices'][0]['message']['content']
        

#This function is typically used in Python to load environment variables from a .env file into the application's environment.
load_dotenv()

st.title("Let's do code review for your python code")
st.header("Please upload your .py file here:")


# Retrieve the API key from Hugging Face secret
api_key = os.getenv('OPENAI_API_KEY')

# Function to download text content as a file using Streamlit
def text_downloader(raw_text):
    # Generate a timestamp for the filename to ensure uniqueness
    timestr = time.strftime("%Y%m%d-%H%M%S")
    
    # Encode the raw text in base64 format for file download
    b64 = base64.b64encode(raw_text.encode()).decode()
    
    # Create a new filename with a timestamp
    new_filename = "code_review_analysis_file_{}_.txt".format(timestr)
    
    st.markdown("#### Download File ✅###")
    
    # Create an HTML link with the encoded content and filename for download
    href = f'<a href="data:file/txt;base64,{b64}" download="{new_filename}">Click Here!!</a>'
    
    # Display the HTML link using Streamlit markdown
    st.markdown(href, unsafe_allow_html=True)

# Capture the .py file data
data = st.file_uploader("Upload python file",type=".py")

if data:

    # Create a StringIO object and initialize it with the decoded content of 'data'
    stringio = StringIO(data.getvalue().decode('utf-8'))

    # Read the content of the StringIO object and store it in the variable 'read_data'
    fetched_data = stringio.read()

    # Optionally, uncomment the following line to write the read data to the streamlit app
    st.write(fetched_data)

    # Initialize a ChatOpenAI instance with the specified model name "gpt-3.5-turbo" and a temperature of 0.9.
    chat = ChatOpenAI(api_key,model_name="gpt-3.5-turbo", temperature=0.9)

    # Create a SystemMessage instance with the specified content, providing information about the assistant's role.
    systemMessage = SystemMessage(content="You are a code review assistant. Provide detailed suggestions to improve the given Python code along by mentioning the existing code line by line with proper indent")

    # Create a HumanMessage instance with content read from some data source.
    humanMessage = HumanMessage(content=fetched_data)

    # Call the chat method of the ChatOpenAI instance, passing a list of messages containing the system and human messages.
    # Recently langchain has recommended to use invoke function for the below please :)
    finalResponse = chat.invoke([systemMessage, humanMessage])

    
    #Display review comments
    st.markdown(finalResponse.content)


    text_downloader(finalResponse.content)