vmalapaka commited on
Commit
cd13835
1 Parent(s): e99cda9

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -0
app.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain_openai import ChatOpenAI
2
+
3
+ from langchain.schema import HumanMessage, SystemMessage
4
+ from io import StringIO
5
+ import streamlit as st
6
+ from dotenv import load_dotenv
7
+ import time
8
+ import base64
9
+
10
+ st.title("Review Python Code")
11
+ st.header("Upload your .py file here:")
12
+
13
+ def text_downloader(raw_text):
14
+ # Generate a timestamp for the filename to ensure uniqueness
15
+ timestr = time.strftime("%Y%m%d-%H%M%S")
16
+
17
+ # Encode the raw text in base64 format for file download
18
+ b64 = base64.b64encode(raw_text.encode()).decode()
19
+
20
+ # Create a new filename with a timestamp
21
+ new_filename = "code_review_analysis_file_{}_.txt".format(timestr)
22
+
23
+ st.markdown("Download File")
24
+
25
+ # Create an HTML link with the encoded content and filename for download
26
+ href = f'<a href="data:file/txt;base64,{b64}" download="{new_filename}">Click Here!!</a>'
27
+
28
+ # Display the HTML link using Streamlit markdown
29
+ st.markdown(href, unsafe_allow_html=True)
30
+
31
+ # Capture the .py file data
32
+ data = st.file_uploader("Upload python file",type=".py")
33
+
34
+ if data:
35
+
36
+ # Create a StringIO object and initialize it with the decoded content of 'data'
37
+ stringio = StringIO(data.getvalue().decode('utf-8'))
38
+
39
+ # Read the content of the StringIO object and store it in the variable 'read_data'
40
+ fetched_data = stringio.read()
41
+
42
+ st.write(fetched_data)
43
+
44
+ # Initialize a ChatOpenAI instance with the specified model name "gpt-3.5-turbo" and a temperature of 0.9.
45
+ chat = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0.9)
46
+
47
+ # Create a SystemMessage instance with the specified content, providing information about the assistant's role.
48
+ 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")
49
+
50
+ # Create a HumanMessage instance with content read from some data source.
51
+ humanMessage = HumanMessage(content=fetched_data)
52
+
53
+ # Call the chat method of the ChatOpenAI instance, passing a list of messages containing the system and human messages.
54
+ finalResponse = chat.invoke([systemMessage, humanMessage])
55
+
56
+
57
+ #Display review comments
58
+ st.markdown(finalResponse.content)
59
+
60
+
61
+ text_downloader(finalResponse.content)