Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,18 +2,24 @@ import streamlit as st
|
|
| 2 |
import google.generativeai as genai
|
| 3 |
import re
|
| 4 |
|
| 5 |
-
# Configure API
|
| 6 |
api_key = st.secrets["GOOGLE_API_KEY"]
|
| 7 |
genai.configure(api_key=api_key)
|
| 8 |
|
| 9 |
-
# System
|
| 10 |
-
system_prompt = """You are a Python code reviewer. Provide feedback on
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
# Load
|
| 13 |
model = genai.GenerativeModel(model_name="models/gemini-2.0-flash-exp", system_instruction=system_prompt)
|
| 14 |
|
|
|
|
| 15 |
def review_code(user_code):
|
| 16 |
-
"""Function to review Python code using Gemini AI."""
|
| 17 |
try:
|
| 18 |
response = model.generate_content(user_code)
|
| 19 |
if hasattr(response, "text"):
|
|
@@ -21,37 +27,43 @@ def review_code(user_code):
|
|
| 21 |
elif hasattr(response, "candidates") and response.candidates:
|
| 22 |
return response.candidates[0].text.strip()
|
| 23 |
else:
|
| 24 |
-
return "Error: Unexpected response format."
|
| 25 |
except Exception as e:
|
| 26 |
-
st.error("An error occurred during code review.
|
| 27 |
st.exception(e)
|
| 28 |
return None
|
| 29 |
|
| 30 |
# Streamlit UI
|
| 31 |
st.title("π’ AI Code Reviewer")
|
| 32 |
-
st.write("Enter your Python code below and receive AI-powered feedback.")
|
| 33 |
|
| 34 |
# Text area for user input
|
| 35 |
-
user_code = st.text_area(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
if st.button("Generate Review") and user_code:
|
| 38 |
if not user_code.strip():
|
| 39 |
st.warning("β οΈ Please enter some Python code to review.")
|
| 40 |
st.stop()
|
| 41 |
|
| 42 |
-
with st.spinner("Analyzing your code..."):
|
| 43 |
feedback = review_code(user_code)
|
| 44 |
|
| 45 |
if feedback:
|
| 46 |
-
#
|
| 47 |
-
code_blocks = re.split(r'```python|```', feedback)
|
| 48 |
-
|
| 49 |
with st.expander("π‘ AI Review Feedback", expanded=True):
|
| 50 |
for block in code_blocks:
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
| 54 |
else:
|
| 55 |
-
st.write(
|
| 56 |
else:
|
| 57 |
-
st.error("
|
|
|
|
| 2 |
import google.generativeai as genai
|
| 3 |
import re
|
| 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
|
| 10 |
+
system_prompt = """You are a Python code reviewer. Provide feedback on:
|
| 11 |
+
- Code quality and best practices.
|
| 12 |
+
- Performance optimizations.
|
| 13 |
+
- Readability and maintainability.
|
| 14 |
+
- Security improvements.
|
| 15 |
+
Return formatted responses with clear explanations and Python code blocks when needed."""
|
| 16 |
|
| 17 |
+
# Load AI Model
|
| 18 |
model = genai.GenerativeModel(model_name="models/gemini-2.0-flash-exp", system_instruction=system_prompt)
|
| 19 |
|
| 20 |
+
@st.cache_resource
|
| 21 |
def review_code(user_code):
|
| 22 |
+
"""Function to review Python code using Gemini AI with caching."""
|
| 23 |
try:
|
| 24 |
response = model.generate_content(user_code)
|
| 25 |
if hasattr(response, "text"):
|
|
|
|
| 27 |
elif hasattr(response, "candidates") and response.candidates:
|
| 28 |
return response.candidates[0].text.strip()
|
| 29 |
else:
|
| 30 |
+
return "β Error: Unexpected response format."
|
| 31 |
except Exception as e:
|
| 32 |
+
st.error("π¨ An error occurred during code review.")
|
| 33 |
st.exception(e)
|
| 34 |
return None
|
| 35 |
|
| 36 |
# Streamlit UI
|
| 37 |
st.title("π’ AI Code Reviewer")
|
| 38 |
+
st.write("π Enter your Python code below and receive AI-powered feedback.")
|
| 39 |
|
| 40 |
# Text area for user input
|
| 41 |
+
user_code = st.text_area(
|
| 42 |
+
"π Python Code:",
|
| 43 |
+
height=200,
|
| 44 |
+
placeholder="Write or paste your Python code here..."
|
| 45 |
+
)
|
| 46 |
|
| 47 |
+
if st.button("π Generate Review") and user_code:
|
| 48 |
if not user_code.strip():
|
| 49 |
st.warning("β οΈ Please enter some Python code to review.")
|
| 50 |
st.stop()
|
| 51 |
|
| 52 |
+
with st.spinner("π€ Analyzing your code..."):
|
| 53 |
feedback = review_code(user_code)
|
| 54 |
|
| 55 |
if feedback:
|
| 56 |
+
# Extract Python code blocks and explanations using regex
|
| 57 |
+
code_blocks = re.split(r'```python|```', feedback, flags=re.IGNORECASE)
|
| 58 |
+
|
| 59 |
with st.expander("π‘ AI Review Feedback", expanded=True):
|
| 60 |
for block in code_blocks:
|
| 61 |
+
cleaned_block = block.strip()
|
| 62 |
+
if cleaned_block:
|
| 63 |
+
# Check if the block is code or text
|
| 64 |
+
if re.search(r'\b(def |import |=|return )', cleaned_block): # Detects Python code
|
| 65 |
+
st.code(cleaned_block, language="python")
|
| 66 |
else:
|
| 67 |
+
st.write(cleaned_block)
|
| 68 |
else:
|
| 69 |
+
st.error("β Code review failed. Please check your input or try again.")
|