Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,9 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import google.generativeai as genai
|
| 3 |
-
import
|
| 4 |
-
import re
|
| 5 |
|
| 6 |
# Configure API Key
|
| 7 |
-
api_key = "
|
| 8 |
genai.configure(api_key=api_key)
|
| 9 |
|
| 10 |
# System Instruction for AI Code Review
|
|
@@ -21,39 +20,6 @@ model = genai.GenerativeModel(
|
|
| 21 |
system_instruction=system_prompt
|
| 22 |
)
|
| 23 |
|
| 24 |
-
# Cache storage for feedback history
|
| 25 |
-
if "history" not in st.session_state:
|
| 26 |
-
st.session_state.history = []
|
| 27 |
-
|
| 28 |
-
@st.cache_data # Using cache_data for better handling of user-specific data
|
| 29 |
-
def get_feedback_from_ai(user_code):
|
| 30 |
-
"""Function to generate AI code review feedback."""
|
| 31 |
-
try:
|
| 32 |
-
# Validate Python syntax
|
| 33 |
-
compile(user_code, '<string>', 'exec')
|
| 34 |
-
|
| 35 |
-
# Generate AI response
|
| 36 |
-
response = model.generate_content(user_code)
|
| 37 |
-
feedback = response.text.strip() if hasattr(response, "text") else "β Error: Unexpected response format."
|
| 38 |
-
|
| 39 |
-
return feedback
|
| 40 |
-
except SyntaxError as e:
|
| 41 |
-
st.warning(f"β οΈ Invalid Python code: {e}")
|
| 42 |
-
return None
|
| 43 |
-
except Exception as e:
|
| 44 |
-
st.error("π¨ An error occurred during code review.")
|
| 45 |
-
st.exception(e)
|
| 46 |
-
return None
|
| 47 |
-
|
| 48 |
-
def add_to_history(user_code, feedback):
|
| 49 |
-
"""Add code and its feedback to the session history."""
|
| 50 |
-
history_entry = {
|
| 51 |
-
"code": user_code,
|
| 52 |
-
"feedback": feedback,
|
| 53 |
-
"hash": hashlib.md5(user_code.encode()).hexdigest()
|
| 54 |
-
}
|
| 55 |
-
st.session_state.history.append(history_entry)
|
| 56 |
-
|
| 57 |
# Streamlit UI
|
| 58 |
st.title("π’ AI Code Reviewer")
|
| 59 |
st.write("π Enter your Python code below and receive AI-powered feedback.")
|
|
@@ -65,35 +31,28 @@ user_code = st.text_area(
|
|
| 65 |
placeholder="Write or paste your Python code here..."
|
| 66 |
)
|
| 67 |
|
| 68 |
-
# Display past feedback history
|
| 69 |
-
if st.session_state.history:
|
| 70 |
-
with st.expander("π Review History", expanded=True):
|
| 71 |
-
for idx, entry in enumerate(reversed(st.session_state.history)):
|
| 72 |
-
with st.expander(f"π Review {idx + 1}"):
|
| 73 |
-
st.code(entry["code"], language="python")
|
| 74 |
-
st.write(entry["feedback"])
|
| 75 |
-
|
| 76 |
# Button to trigger code review
|
| 77 |
if st.button("π Generate Review") and user_code:
|
| 78 |
if not user_code.strip():
|
| 79 |
st.warning("β οΈ Please enter some Python code to review.")
|
| 80 |
else:
|
| 81 |
with st.spinner("π€ Analyzing your code..."):
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import google.generativeai as genai
|
| 3 |
+
from IPython import display
|
|
|
|
| 4 |
|
| 5 |
# Configure API Key
|
| 6 |
+
api_key = st.secrets["GOOGLE_API_KEY"]
|
| 7 |
genai.configure(api_key=api_key)
|
| 8 |
|
| 9 |
# System Instruction for AI Code Review
|
|
|
|
| 20 |
system_instruction=system_prompt
|
| 21 |
)
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
# Streamlit UI
|
| 24 |
st.title("π’ AI Code Reviewer")
|
| 25 |
st.write("π Enter your Python code below and receive AI-powered feedback.")
|
|
|
|
| 31 |
placeholder="Write or paste your Python code here..."
|
| 32 |
)
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
# Button to trigger code review
|
| 35 |
if st.button("π Generate Review") and user_code:
|
| 36 |
if not user_code.strip():
|
| 37 |
st.warning("β οΈ Please enter some Python code to review.")
|
| 38 |
else:
|
| 39 |
with st.spinner("π€ Analyzing your code..."):
|
| 40 |
+
try:
|
| 41 |
+
# Validate Python syntax
|
| 42 |
+
compile(user_code, '<string>', 'exec')
|
| 43 |
+
|
| 44 |
+
# Generate AI response
|
| 45 |
+
response = model.generate_content(user_code)
|
| 46 |
+
feedback = response.text.strip() if hasattr(response, "text") else "β Error: Unexpected response format."
|
| 47 |
+
|
| 48 |
+
# Display feedback
|
| 49 |
+
if feedback:
|
| 50 |
+
st.subheader("π‘ AI Review Feedback:")
|
| 51 |
+
st.write(feedback)
|
| 52 |
+
else:
|
| 53 |
+
st.error("β Code review failed. Please check your input or try again.")
|
| 54 |
+
except SyntaxError as e:
|
| 55 |
+
st.warning(f"β οΈ Invalid Python code: {e}")
|
| 56 |
+
except Exception as e:
|
| 57 |
+
st.error("π¨ An error occurred during code review.")
|
| 58 |
+
st.exception(e)
|