Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -15,19 +15,31 @@ system_prompt = """You are a Python code reviewer. Provide feedback on:
|
|
| 15 |
Return formatted responses with clear explanations and Python code blocks when needed."""
|
| 16 |
|
| 17 |
# Load AI Model
|
| 18 |
-
model = genai.GenerativeModel(
|
|
|
|
|
|
|
|
|
|
| 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"):
|
| 26 |
return response.text.strip()
|
| 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)
|
|
@@ -48,22 +60,21 @@ 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):
|
| 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.")
|
|
|
|
| 15 |
Return formatted responses with clear explanations and Python code blocks when needed."""
|
| 16 |
|
| 17 |
# Load AI Model
|
| 18 |
+
model = genai.GenerativeModel(
|
| 19 |
+
model_name="models/gemini-2.0-flash-exp",
|
| 20 |
+
system_instruction=system_prompt
|
| 21 |
+
)
|
| 22 |
|
| 23 |
@st.cache_resource
|
| 24 |
def review_code(user_code):
|
| 25 |
"""Function to review Python code using Gemini AI with caching."""
|
| 26 |
try:
|
| 27 |
+
# Validate Python syntax
|
| 28 |
+
compile(user_code, '<string>', 'exec')
|
| 29 |
+
|
| 30 |
+
# Generate AI response
|
| 31 |
response = model.generate_content(user_code)
|
| 32 |
+
|
| 33 |
+
# Handle response
|
| 34 |
if hasattr(response, "text"):
|
| 35 |
return response.text.strip()
|
| 36 |
+
elif hasattr(response, "candidates") and isinstance(response.candidates, list) and len(response.candidates) > 0:
|
| 37 |
return response.candidates[0].text.strip()
|
| 38 |
else:
|
| 39 |
return "β Error: Unexpected response format."
|
| 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)
|
|
|
|
| 60 |
if not user_code.strip():
|
| 61 |
st.warning("β οΈ Please enter some Python code to review.")
|
| 62 |
st.stop()
|
| 63 |
+
|
| 64 |
with st.spinner("π€ Analyzing your code..."):
|
| 65 |
feedback = review_code(user_code)
|
| 66 |
+
|
| 67 |
if feedback:
|
| 68 |
# Extract Python code blocks and explanations using regex
|
| 69 |
code_blocks = re.split(r'```python|```', feedback, flags=re.IGNORECASE)
|
|
|
|
| 70 |
with st.expander("π‘ AI Review Feedback", expanded=True):
|
| 71 |
for block in code_blocks:
|
| 72 |
cleaned_block = block.strip()
|
| 73 |
if cleaned_block:
|
| 74 |
# Check if the block is code or text
|
| 75 |
+
if re.search(r'\b(def |import |=|return |class |for |while |if |elif |else )', cleaned_block):
|
| 76 |
st.code(cleaned_block, language="python")
|
| 77 |
else:
|
| 78 |
st.write(cleaned_block)
|
| 79 |
else:
|
| 80 |
+
st.error("β Code review failed. Please check your input or try again.")
|