Spaces:
Build error
Build error
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,68 +1,79 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import google.generativeai as genai
|
| 3 |
-
|
| 4 |
-
st.title("AI Code Reviewer
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import google.generativeai as genai
|
| 3 |
+
|
| 4 |
+
st.title("🧠 AI Code Reviewer")
|
| 5 |
+
|
| 6 |
+
# Prompt user for API key (stored in session)
|
| 7 |
+
if "api_key" not in st.session_state:
|
| 8 |
+
st.session_state.api_key = ""
|
| 9 |
+
|
| 10 |
+
# Show input field if API key is not already entered
|
| 11 |
+
if not st.session_state.api_key:
|
| 12 |
+
st.session_state.api_key = st.text_input("Enter your Google Generative AI API Key", type="password")
|
| 13 |
+
if not st.session_state.api_key:
|
| 14 |
+
st.warning("Please enter your API key to continue.")
|
| 15 |
+
st.stop()
|
| 16 |
+
|
| 17 |
+
# Configure generative model
|
| 18 |
+
try:
|
| 19 |
+
genai.configure(api_key=st.session_state.api_key)
|
| 20 |
+
model = genai.GenerativeModel(model_name="gemini-1.5-flash")
|
| 21 |
+
except Exception as e:
|
| 22 |
+
st.error("Invalid API key or configuration error.")
|
| 23 |
+
st.stop()
|
| 24 |
+
|
| 25 |
+
# System prompt for AI behavior
|
| 26 |
+
sys_prompt = '''
|
| 27 |
+
You are an AI Code Reviewer. Your task is to analyze submitted code in any programming language and provide a structured review.
|
| 28 |
+
|
| 29 |
+
If the code contains errors (syntax, logical, or runtime):
|
| 30 |
+
1. Identify and explain the errors concisely.
|
| 31 |
+
2. Provide a corrected version in a copyable format.
|
| 32 |
+
3. Ensure the logic is correct; if flawed, correct it with an explanation.
|
| 33 |
+
|
| 34 |
+
If the code is correct but inefficient:
|
| 35 |
+
- Suggest performance optimizations (e.g., time and space complexity improvements).
|
| 36 |
+
|
| 37 |
+
If the code is correct and efficient:
|
| 38 |
+
- Briefly explain what the code does in simple terms.
|
| 39 |
+
|
| 40 |
+
Additional Considerations:
|
| 41 |
+
- Ensure readability and best coding practices (e.g., meaningful variable names, comments, modularity).
|
| 42 |
+
- If the user submits a non-programming question, respond humorously as a dedicated code reviewer.
|
| 43 |
+
|
| 44 |
+
Keep responses concise, structured, and helpful.
|
| 45 |
+
'''
|
| 46 |
+
|
| 47 |
+
# Review history storage
|
| 48 |
+
if "history" not in st.session_state:
|
| 49 |
+
st.session_state.history = []
|
| 50 |
+
|
| 51 |
+
# Code input
|
| 52 |
+
user_query = st.text_area("Submit your code for review:", height=150, key="user_input", placeholder="Paste your code here...")
|
| 53 |
+
|
| 54 |
+
if st.button("Review"):
|
| 55 |
+
with st.spinner("Reviewing your code..."):
|
| 56 |
+
try:
|
| 57 |
+
response = model.generate_content(f"{sys_prompt}\n\nUser: {user_query}")
|
| 58 |
+
ai_response = response.text.strip() if response.text else "Error: No response received."
|
| 59 |
+
|
| 60 |
+
st.session_state.history.append({"query": user_query, "response": ai_response})
|
| 61 |
+
|
| 62 |
+
st.subheader("Review Result:")
|
| 63 |
+
st.write(ai_response)
|
| 64 |
+
|
| 65 |
+
except Exception as e:
|
| 66 |
+
st.error(f"An error occurred: {e}")
|
| 67 |
+
|
| 68 |
+
# Sidebar - Review History
|
| 69 |
+
st.sidebar.title("Review History")
|
| 70 |
+
if st.session_state.history:
|
| 71 |
+
queries = [f"Query {i+1}" for i in range(len(st.session_state.history))]
|
| 72 |
+
selected_query = st.sidebar.selectbox("Select a past query:", queries, index=len(queries)-1)
|
| 73 |
+
|
| 74 |
+
if selected_query:
|
| 75 |
+
idx = queries.index(selected_query)
|
| 76 |
+
st.sidebar.subheader("Selected Review Result:")
|
| 77 |
+
st.sidebar.write(st.session_state.history[idx]["response"])
|
| 78 |
+
else:
|
| 79 |
+
st.sidebar.write("No queries yet. Submit your code above!")
|