nilkanth007 commited on
Commit
3479e9b
·
verified ·
1 Parent(s): f62f91a
Files changed (1) hide show
  1. app.py +73 -72
app.py CHANGED
@@ -1,72 +1,73 @@
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
-
 
 
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
+ import os
12
+
13
+
14
+ #This function is typically used in Python to load environment variables from a .env file into the application's environment.
15
+ load_dotenv()
16
+
17
+ st.title("Let's do code review for your python code")
18
+ st.header("Please upload your .py file here:")
19
+
20
+
21
+ # Function to download text content as a file using Streamlit
22
+ def text_downloader(raw_text):
23
+ # Generate a timestamp for the filename to ensure uniqueness
24
+ timestr = time.strftime("%Y%m%d-%H%M%S")
25
+
26
+ # Encode the raw text in base64 format for file download
27
+ b64 = base64.b64encode(raw_text.encode()).decode()
28
+
29
+ # Create a new filename with a timestamp
30
+ new_filename = "code_review_analysis_file_{}_.txt".format(timestr)
31
+
32
+ st.markdown("#### Download File ✅###")
33
+
34
+ # Create an HTML link with the encoded content and filename for download
35
+ href = f'<a href="data:file/txt;base64,{b64}" download="{new_filename}">Click Here!!</a>'
36
+
37
+ # Display the HTML link using Streamlit markdown
38
+ st.markdown(href, unsafe_allow_html=True)
39
+
40
+ # Capture the .py file data
41
+ data = st.file_uploader("Upload python file",type=".py")
42
+
43
+ if data:
44
+
45
+ # Create a StringIO object and initialize it with the decoded content of 'data'
46
+ stringio = StringIO(data.getvalue().decode('utf-8'))
47
+
48
+ # Read the content of the StringIO object and store it in the variable 'read_data'
49
+ fetched_data = stringio.read()
50
+
51
+ # Optionally, uncomment the following line to write the read data to the streamlit app
52
+ st.write(fetched_data)
53
+
54
+ # Initialize a ChatOpenAI instance with the specified model name "gpt-3.5-turbo" and a temperature of 0.9.
55
+ chat = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0.9)
56
+
57
+ # Create a SystemMessage instance with the specified content, providing information about the assistant's role.
58
+ 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")
59
+
60
+ # Create a HumanMessage instance with content read from some data source.
61
+ humanMessage = HumanMessage(content=fetched_data)
62
+
63
+ # Call the chat method of the ChatOpenAI instance, passing a list of messages containing the system and human messages.
64
+ # Recently langchain has recommended to use invoke function for the below please :)
65
+ finalResponse = chat.invoke([systemMessage, humanMessage])
66
+
67
+
68
+ #Display review comments
69
+ st.markdown(finalResponse.content)
70
+
71
+
72
+ text_downloader(finalResponse.content)
73
+