Spaces:
Build error
Build error
Upload 4 files
Browse files
.env
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
OPENAI_API_KEY="sk-8iW36nSTjh49kjxf8y9fx5sd98sn5hj99jnvc367dO98vjeu4PCT"
|
.env.example
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
OPENAI_API_KEY=""
|
app.py
ADDED
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#the below import has been replaced by the later mentioned import, recently by langchain as a per of their improvement strategy :)
|
2 |
+
#from langchain.chat_models import ChatOpenAI
|
3 |
+
from langchain_openai import ChatOpenAI
|
4 |
+
|
5 |
+
from langchain.schema import HumanMessage, SystemMessage
|
6 |
+
from io import StringIO
|
7 |
+
import streamlit as st
|
8 |
+
from dotenv import load_dotenv
|
9 |
+
import time
|
10 |
+
import base64
|
11 |
+
|
12 |
+
|
13 |
+
#This function is typically used in Python to load environment variables from a .env file into the application's environment.
|
14 |
+
load_dotenv()
|
15 |
+
|
16 |
+
st.title("Let's do code review for your python code")
|
17 |
+
st.header("Please upload your .py file here:")
|
18 |
+
|
19 |
+
|
20 |
+
# Function to download text content as a file using Streamlit
|
21 |
+
def text_downloader(raw_text):
|
22 |
+
# Generate a timestamp for the filename to ensure uniqueness
|
23 |
+
timestr = time.strftime("%Y%m%d-%H%M%S")
|
24 |
+
|
25 |
+
# Encode the raw text in base64 format for file download
|
26 |
+
b64 = base64.b64encode(raw_text.encode()).decode()
|
27 |
+
|
28 |
+
# Create a new filename with a timestamp
|
29 |
+
new_filename = "code_review_analysis_file_{}_.txt".format(timestr)
|
30 |
+
|
31 |
+
st.markdown("#### Download File ✅###")
|
32 |
+
|
33 |
+
# Create an HTML link with the encoded content and filename for download
|
34 |
+
href = f'<a href="data:file/txt;base64,{b64}" download="{new_filename}">Click Here!!</a>'
|
35 |
+
|
36 |
+
# Display the HTML link using Streamlit markdown
|
37 |
+
st.markdown(href, unsafe_allow_html=True)
|
38 |
+
|
39 |
+
# Capture the .py file data
|
40 |
+
data = st.file_uploader("Upload python file",type=".py")
|
41 |
+
|
42 |
+
if data:
|
43 |
+
|
44 |
+
# Create a StringIO object and initialize it with the decoded content of 'data'
|
45 |
+
stringio = StringIO(data.getvalue().decode('utf-8'))
|
46 |
+
|
47 |
+
# Read the content of the StringIO object and store it in the variable 'read_data'
|
48 |
+
fetched_data = stringio.read()
|
49 |
+
|
50 |
+
# Optionally, uncomment the following line to write the read data to the streamlit app
|
51 |
+
st.write(fetched_data)
|
52 |
+
|
53 |
+
# Initialize a ChatOpenAI instance with the specified model name "gpt-3.5-turbo" and a temperature of 0.9.
|
54 |
+
chat = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0.9)
|
55 |
+
|
56 |
+
# Create a SystemMessage instance with the specified content, providing information about the assistant's role.
|
57 |
+
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")
|
58 |
+
|
59 |
+
# Create a HumanMessage instance with content read from some data source.
|
60 |
+
humanMessage = HumanMessage(content=fetched_data)
|
61 |
+
|
62 |
+
# Call the chat method of the ChatOpenAI instance, passing a list of messages containing the system and human messages.
|
63 |
+
# Recently langchain has recommended to use invoke function for the below please :)
|
64 |
+
finalResponse = chat.invoke([systemMessage, humanMessage])
|
65 |
+
|
66 |
+
|
67 |
+
#Display review comments
|
68 |
+
st.markdown(finalResponse.content)
|
69 |
+
|
70 |
+
|
71 |
+
text_downloader(finalResponse.content)
|
72 |
+
|
code.py
ADDED
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
num = 39
|
2 |
+
|
3 |
+
flag = False
|
4 |
+
|
5 |
+
if num == 1:
|
6 |
+
print(num, "is not a prime number")
|
7 |
+
elif num > 1:
|
8 |
+
for i in range(2, num):
|
9 |
+
if (num % i) == 0:
|
10 |
+
flag = True
|
11 |
+
break
|
12 |
+
|
13 |
+
|
14 |
+
if flag:
|
15 |
+
print(num, "is not a prime number")
|
16 |
+
else:
|
17 |
+
print(num, "is a prime number")
|