File size: 2,853 Bytes
6e98b9e
 
c37d233
6e98b9e
c37d233
 
6e98b9e
 
 
c37d233
 
 
 
 
 
6e98b9e
 
 
c37d233
6e98b9e
 
 
c37d233
 
 
 
 
 
 
43bb228
 
c37d233
43bb228
c37d233
43bb228
c37d233
43bb228
c37d233
43bb228
c37d233
43bb228
c37d233
 
43bb228
c37d233
 
 
6e98b9e
c37d233
 
6e98b9e
 
 
c37d233
6e98b9e
c37d233
6e98b9e
 
c37d233
6e98b9e
 
c37d233
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43bb228
6e98b9e
43bb228
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 = "AIzaSyBuhWQHGfMQewIY_gdnmbzzYoori6faeUo" 
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.")