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