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
| 4 |
+
refactor_code,
|
| 5 |
+
code_feedback,
|
| 6 |
+
suggest_best_practices,
|
| 7 |
+
remove_code_errors,
|
| 8 |
+
)
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def main():
|
| 12 |
+
st.title("CodeMentor - (AI-Enhanced Code Collaboration Tool)")
|
| 13 |
+
st.subheader("Collaborate, Refactor, and Optimize with AI.")
|
| 14 |
+
st.write(
|
| 15 |
+
"A smart tool for distributed teams to automate code reviews, refactor efficiently, and get real-time AI-driven feedback."
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
# Instructions
|
| 19 |
+
st.write(
|
| 20 |
+
"Upload a file or paste your code below to get an AI-generated code review."
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
# Input Methods: File Upload or Text Area
|
| 24 |
+
uploaded_file = st.file_uploader(
|
| 25 |
+
"Upload a code file (Max 500 lines)", type=["py", "js", "txt"]
|
| 26 |
+
)
|
| 27 |
+
code_input = st.text_area("Or paste your code here (Max 1000 words)", height=300)
|
| 28 |
+
|
| 29 |
+
# Limit input size for code
|
| 30 |
+
if uploaded_file:
|
| 31 |
+
code = uploaded_file.read().decode("utf-8")
|
| 32 |
+
if len(code.splitlines()) > 500:
|
| 33 |
+
st.error(
|
| 34 |
+
"File is too large! Please upload a file with a maximum of 500 lines."
|
| 35 |
+
)
|
| 36 |
+
code = None # Reset code if it's too large
|
| 37 |
+
else:
|
| 38 |
+
st.success(f"File uploaded: {uploaded_file.name}")
|
| 39 |
+
elif code_input:
|
| 40 |
+
code = code_input
|
| 41 |
+
if len(code.split()) > 1000:
|
| 42 |
+
st.error("Code exceeds 1000 words! Please shorten your code.")
|
| 43 |
+
code = None # Reset code if it's too large
|
| 44 |
+
else:
|
| 45 |
+
code = None
|
| 46 |
+
|
| 47 |
+
# Button to trigger code review
|
| 48 |
+
if st.button("Get Code Review") and code:
|
| 49 |
+
with st.spinner("Processing..."):
|
| 50 |
+
# Call the OpenAI API to get code review
|
| 51 |
+
review = get_code_review_response(code)
|
| 52 |
+
st.subheader("Code Review Results:")
|
| 53 |
+
st.write(review)
|
| 54 |
+
|
| 55 |
+
# Provide download option
|
| 56 |
+
st.download_button(
|
| 57 |
+
label="Download Code Review",
|
| 58 |
+
data=review,
|
| 59 |
+
file_name="code_review.txt",
|
| 60 |
+
mime="text/plain",
|
| 61 |
+
)
|
| 62 |
+
st.success("You can download the code review as code_review.txt")
|
| 63 |
+
|
| 64 |
+
# Button to trigger code refactoring
|
| 65 |
+
if st.button("Refactor Code") and code:
|
| 66 |
+
with st.spinner("Refactoring your code..."):
|
| 67 |
+
refactored_code = refactor_code(code)
|
| 68 |
+
st.subheader("Refactored Code:")
|
| 69 |
+
st.write(refactored_code)
|
| 70 |
+
|
| 71 |
+
# Provide download option for refactored code
|
| 72 |
+
st.download_button(
|
| 73 |
+
label="Download Refactored Code",
|
| 74 |
+
data=refactored_code,
|
| 75 |
+
file_name="refactored_code.txt",
|
| 76 |
+
mime="text/plain",
|
| 77 |
+
)
|
| 78 |
+
st.success("You can download the refactored code as refactored_code.txt")
|
| 79 |
+
|
| 80 |
+
# Button to trigger code feedback
|
| 81 |
+
if st.button("Get Code Feedback") and code:
|
| 82 |
+
with st.spinner("Getting feedback on your code..."):
|
| 83 |
+
feedback = code_feedback(code)
|
| 84 |
+
st.subheader("Code Feedback:")
|
| 85 |
+
st.write(feedback)
|
| 86 |
+
|
| 87 |
+
# Ensure feedback is a string for download
|
| 88 |
+
feedback_text = feedback if isinstance(feedback, str) else str(feedback)
|
| 89 |
+
|
| 90 |
+
# Provide download option for code feedback
|
| 91 |
+
st.download_button(
|
| 92 |
+
label="Download Code Feedback",
|
| 93 |
+
data=feedback_text, # Use the extracted string here
|
| 94 |
+
file_name="code_feedback.txt",
|
| 95 |
+
mime="text/plain",
|
| 96 |
+
)
|
| 97 |
+
st.success("You can download the code feedback as code_feedback.txt")
|
| 98 |
+
|
| 99 |
+
# Add button to suggest best practices
|
| 100 |
+
if st.button("Suggest Best Practices") and code:
|
| 101 |
+
with st.spinner("Getting best practices..."):
|
| 102 |
+
best_practices = suggest_best_practices(code)
|
| 103 |
+
st.subheader("Best Practices Suggestions:")
|
| 104 |
+
st.write(best_practices)
|
| 105 |
+
|
| 106 |
+
# Provide download option for best practices suggestions
|
| 107 |
+
best_practices_text = (
|
| 108 |
+
best_practices
|
| 109 |
+
if isinstance(best_practices, str)
|
| 110 |
+
else str(best_practices)
|
| 111 |
+
)
|
| 112 |
+
st.download_button(
|
| 113 |
+
label="Download Best Practices Suggestions",
|
| 114 |
+
data=best_practices_text,
|
| 115 |
+
file_name="best_practices.txt",
|
| 116 |
+
mime="text/plain",
|
| 117 |
+
)
|
| 118 |
+
st.success(
|
| 119 |
+
"You can download the best practices suggestions as best_practices.txt"
|
| 120 |
+
)
|
| 121 |
+
|
| 122 |
+
# Button to trigger error removal
|
| 123 |
+
if st.button("Remove Code Errors") and code:
|
| 124 |
+
with st.spinner("Removing errors from your code..."):
|
| 125 |
+
error_removal_suggestions = remove_code_errors(code)
|
| 126 |
+
st.subheader("Error Removal Suggestions:")
|
| 127 |
+
st.write(error_removal_suggestions)
|
| 128 |
+
|
| 129 |
+
# Provide download option for error removal suggestions
|
| 130 |
+
error_removal_text = (
|
| 131 |
+
error_removal_suggestions
|
| 132 |
+
if isinstance(error_removal_suggestions, str)
|
| 133 |
+
else str(error_removal_suggestions)
|
| 134 |
+
)
|
| 135 |
+
st.download_button(
|
| 136 |
+
label="Download Error Removal Suggestions",
|
| 137 |
+
data=error_removal_text,
|
| 138 |
+
file_name="error_removal_suggestions.txt",
|
| 139 |
+
mime="text/plain",
|
| 140 |
+
)
|
| 141 |
+
st.success(
|
| 142 |
+
"You can download the error removal suggestions as error_removal_suggestions.txt"
|
| 143 |
+
)
|
| 144 |
+
|
| 145 |
+
|
| 146 |
+
if __name__ == "__main__":
|
| 147 |
+
main()
|
openai_client.py
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from dotenv import load_dotenv
|
| 3 |
+
from openai import OpenAI
|
| 4 |
+
import anthropic
|
| 5 |
+
|
| 6 |
+
# Load environment variables from .env file
|
| 7 |
+
load_dotenv()
|
| 8 |
+
|
| 9 |
+
client = OpenAI(
|
| 10 |
+
api_key=os.getenv("OPENAI_API_KEY"),
|
| 11 |
+
base_url=os.getenv("OPENAI_API_BASE"), # Uncomment if using a custom base URL
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
# Function to get GPT-4o Mini response
|
| 16 |
+
def get_code_review_response(prompt, max_tokens=1000):
|
| 17 |
+
try:
|
| 18 |
+
response = client.chat.completions.create(
|
| 19 |
+
model="gpt-4o-mini",
|
| 20 |
+
messages=[
|
| 21 |
+
{
|
| 22 |
+
"role": "system",
|
| 23 |
+
"content": "You are an AI assistant who helps users in code reviews by deep thinking in points max 5-6 point shortly.",
|
| 24 |
+
},
|
| 25 |
+
{"role": "user", "content": prompt},
|
| 26 |
+
],
|
| 27 |
+
max_tokens=max_tokens,
|
| 28 |
+
)
|
| 29 |
+
return response.choices[0].message.content
|
| 30 |
+
except Exception as e:
|
| 31 |
+
return "Sorry, an error occurred while generating your idea. Please try again later."
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
# Function to refactor code
|
| 35 |
+
def refactor_code(code_snippet):
|
| 36 |
+
try:
|
| 37 |
+
response = client.chat.completions.create(
|
| 38 |
+
model="gpt-4o-2024-08-06",
|
| 39 |
+
messages=[
|
| 40 |
+
{
|
| 41 |
+
"role": "system",
|
| 42 |
+
"content": "You are an expert code refactoring assistant.",
|
| 43 |
+
},
|
| 44 |
+
{
|
| 45 |
+
"role": "user",
|
| 46 |
+
"content": f"Refactor the following code. Do not provide any explanation or comments, just return the refactored code.\n{code_snippet}",
|
| 47 |
+
},
|
| 48 |
+
],
|
| 49 |
+
)
|
| 50 |
+
refactored_code = response.choices[0].message.content
|
| 51 |
+
return refactored_code
|
| 52 |
+
except Exception as e:
|
| 53 |
+
return "Sorry, an error occurred while refactoring your code. Please try again later."
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
# Initialize the Anthropic client
|
| 57 |
+
anthropic_client = anthropic.Anthropic(api_key=os.getenv("ANTHROPIC_API_KEY"))
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
# Function to get feedback on code using Anthropic
|
| 61 |
+
def code_feedback(code_snippet):
|
| 62 |
+
try:
|
| 63 |
+
response = anthropic_client.messages.create(
|
| 64 |
+
model="claude-3-5-sonnet-20240620",
|
| 65 |
+
max_tokens=1024,
|
| 66 |
+
messages=[
|
| 67 |
+
{
|
| 68 |
+
"role": "user",
|
| 69 |
+
"content": f"Please provide feedback on the given code, don't refactor the code:\n{code_snippet}",
|
| 70 |
+
},
|
| 71 |
+
],
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
# Extract feedback text from the response
|
| 75 |
+
feedback = response.text if hasattr(response, "text") else str(response)
|
| 76 |
+
|
| 77 |
+
# Check if feedback is a Message object and extract text if necessary
|
| 78 |
+
if hasattr(response, "content") and isinstance(response.content, list):
|
| 79 |
+
feedback = "\n\n".join(
|
| 80 |
+
[
|
| 81 |
+
text_block.text
|
| 82 |
+
for text_block in response.content
|
| 83 |
+
if hasattr(text_block, "text")
|
| 84 |
+
]
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
return feedback
|
| 88 |
+
|
| 89 |
+
except Exception as e:
|
| 90 |
+
return "Sorry, an error occurred while getting feedback on your code. Please try again later."
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
# Function to suggest best coding practices based on given code
|
| 94 |
+
def suggest_best_practices(code_snippet):
|
| 95 |
+
try:
|
| 96 |
+
response = anthropic_client.messages.create(
|
| 97 |
+
model="claude-3-5-sonnet-20240620",
|
| 98 |
+
max_tokens=1024,
|
| 99 |
+
messages=[
|
| 100 |
+
{
|
| 101 |
+
"role": "user",
|
| 102 |
+
"content": (
|
| 103 |
+
f"Based on the following code, suggest best practices max 5-6 point shortly"
|
| 104 |
+
f"for coding patterns that align with industry standards: \n{code_snippet}"
|
| 105 |
+
),
|
| 106 |
+
},
|
| 107 |
+
],
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
# Extract suggestions from the response
|
| 111 |
+
best_practices = response.text if hasattr(response, "text") else str(response)
|
| 112 |
+
|
| 113 |
+
# Check if the feedback is a Message object and extract text if necessary
|
| 114 |
+
if hasattr(response, "content") and isinstance(response.content, list):
|
| 115 |
+
best_practices = "\n\n".join(
|
| 116 |
+
[
|
| 117 |
+
text_block.text
|
| 118 |
+
for text_block in response.content
|
| 119 |
+
if hasattr(text_block, "text")
|
| 120 |
+
]
|
| 121 |
+
)
|
| 122 |
+
|
| 123 |
+
return best_practices
|
| 124 |
+
|
| 125 |
+
except Exception as e:
|
| 126 |
+
return "Sorry, an error occurred while suggesting best practices. Please try again later."
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
# Function to remove code errors
|
| 130 |
+
def remove_code_errors(code_snippet):
|
| 131 |
+
try:
|
| 132 |
+
response = client.chat.completions.create(
|
| 133 |
+
model="gpt-4o-mini",
|
| 134 |
+
messages=[
|
| 135 |
+
{
|
| 136 |
+
"role": "system",
|
| 137 |
+
"content": "You are an expert in debugging code. Provide concise suggestions to remove errors from the following code snippet.",
|
| 138 |
+
},
|
| 139 |
+
{
|
| 140 |
+
"role": "user",
|
| 141 |
+
"content": f"Identify and suggest fixes for errors in the following code:\n{code_snippet}",
|
| 142 |
+
},
|
| 143 |
+
],
|
| 144 |
+
)
|
| 145 |
+
error_removal_suggestions = response.choices[0].message.content
|
| 146 |
+
return error_removal_suggestions
|
| 147 |
+
except Exception as e:
|
| 148 |
+
return "Sorry, an error occurred while removing code errors. Please try again later."
|