File size: 2,846 Bytes
2774169
 
 
7963b9d
2774169
 
 
 
 
 
 
 
 
 
 
 
60aeb1f
2774169
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import google.generativeai as genai

st.title("AI Code Reviewer")

if "api_key" not in st.session_state:
    st.session_state.api_key = ""

if not st.session_state.api_key:
    st.session_state.api_key = st.text_input("Enter your Google Generative AI API Key", type="password")
    if not st.session_state.api_key:
        st.warning("Please enter your API key to continue.")
        st.stop()

try:
    genai.configure(api_key=st.session_state.api_key)
    model = genai.GenerativeModel(model_name="gemini-1.5-flash-001")
except Exception as e:
    st.error("Invalid API key or configuration error.")
    st.stop()

# System prompt for AI behavior
sys_prompt = '''
You are an AI Code Reviewer. Your task is to analyze submitted code in any programming language and provide a structured review.

If the code contains errors (syntax, logical, or runtime): 
1. Identify and explain the errors concisely.  
2. Provide a corrected version in a copyable format.  
3. Ensure the logic is correct; if flawed, correct it with an explanation.  

If the code is correct but inefficient:
- Suggest performance optimizations (e.g., time and space complexity improvements).  

If the code is correct and efficient:
- Briefly explain what the code does in simple terms.  

Additional Considerations:
- Ensure readability and best coding practices (e.g., meaningful variable names, comments, modularity).  
- If the user submits a non-programming question, respond humorously as a dedicated code reviewer.  

Keep responses concise, structured, and helpful.
'''

# Review history storage
if "history" not in st.session_state:
    st.session_state.history = []

user_query = st.text_area("Submit your code for review:", height=150, key="user_input", placeholder="Paste your code here...")

if st.button("Review"):
    with st.spinner("Reviewing your code..."):
        try:
            response = model.generate_content(f"{sys_prompt}\n\nUser: {user_query}")
            ai_response = response.text.strip() if response.text else "Error: No response received."

            st.session_state.history.append({"query": user_query, "response": ai_response})

            st.subheader("Review Result:")
            st.write(ai_response)

        except Exception as e:
            st.error(f"An error occurred: {e}")

# Sidebar - Review History
st.sidebar.title("Review History")
if st.session_state.history:
    queries = [f"Query {i+1}" for i in range(len(st.session_state.history))]
    selected_query = st.sidebar.selectbox("Select a past query:", queries, index=len(queries)-1)

    if selected_query:
        idx = queries.index(selected_query)
        st.sidebar.subheader("Selected Review Result:")
        st.sidebar.write(st.session_state.history[idx]["response"])
else:
    st.sidebar.write("No queries yet. Submit your code above!")