samim2024/Code-Analysis-META-AI
0
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 ChatOpenAI3#from langchain_openai import ChatOpenAI4from langchain_community.llms import HuggingFaceEndpoint5from langchain.schema import HumanMessage, SystemMessage6from io import StringIO7import streamlit as st8from dotenv import load_dotenv9import time10import base6411 12 13#This function is typically used in Python to load environment variables from a .env file into the application's environment.14load_dotenv()15 16st.title("Let's do code review for your python code")17st.header("Please upload your .py file here:")18 19 20# Function to download text content as a file using Streamlit21def text_downloader(raw_text):22 # Generate a timestamp for the filename to ensure uniqueness23 timestr = time.strftime("%Y%m%d-%H%M%S")24 25 # Encode the raw text in base64 format for file download26 b64 = base64.b64encode(raw_text.encode()).decode()27 28 # Create a new filename with a timestamp29 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 download34 href = f'<a href="data:file/txt;base64,{b64}" download="{new_filename}">Click Here!!</a>'35 36 # Display the HTML link using Streamlit markdown37 st.markdown(href, unsafe_allow_html=True)38 39# Capture the .py file data40data = st.file_uploader("Upload python file",type=".py")41 42if 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 app51 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 chat = HuggingFaceEndpoint(temperature=0.9,repo_id="mistralai/Mixtral-8x7B-Instruct-v0.1") #"mistralai/Mistral-7B-Instruct-v0.2" # 'text-davinci-003' model is depreciated now, so we are using the openai's recommended model56 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 comments69 st.markdown(finalResponse)70 71 72 text_downloader(finalResponse)73 74 