Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,61 +1,59 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import google.generativeai as genai
|
| 3 |
-
|
| 4 |
-
#
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
st.
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
# --------------------------
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
if "
|
| 14 |
-
api_key = st.
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
#
|
| 22 |
-
#
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
# --------------------------
|
| 27 |
-
# 4. Code Review Function
|
| 28 |
-
# --------------------------
|
| 29 |
-
def
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
"""
|
| 43 |
-
)
|
| 44 |
-
return response.text
|
| 45 |
-
|
| 46 |
-
# --------------------------
|
| 47 |
# 5. Run Code Review
|
| 48 |
-
# --------------------------
|
| 49 |
-
if st.button("π Review Code"):
|
| 50 |
-
if not user_code.strip():
|
| 51 |
-
st.warning("β οΈ Please enter some Python code first.")
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import openai import google.generativeai as genai
|
| 3 |
+
# --------------------------
|
| 4 |
+
# 1. Page Configuration
|
| 5 |
+
# --------------------------
|
| 6 |
+
st.set_page_config(page_title="AI-Powered Code Reviewer", layout="wide")
|
| 7 |
+
st.title("π AI-Powered Python Code Reviewer")
|
| 8 |
+
# --------------------------
|
| 9 |
+
# 2. Sidebar API Selection
|
| 10 |
+
# --------------------------
|
| 11 |
+
st.sidebar.header("βοΈ Settings")
|
| 12 |
+
api_choice = st.sidebar.radio("Choose API:", ["OpenAI", "Google Gemini"])
|
| 13 |
+
if api_choice == "OpenAI":
|
| 14 |
+
api_key = st.sidebar.text_input("Enter OpenAI API Key", type="password")
|
| 15 |
+
if api_key:
|
| 16 |
+
openai.api_key = api_key
|
| 17 |
+
else:
|
| 18 |
+
api_key = st.sidebar.text_input("Enter Google API Key", type="password")
|
| 19 |
+
if api_key:
|
| 20 |
+
genai.configure(api_key=api_key)
|
| 21 |
+
# --------------------------
|
| 22 |
+
# 3. User Code Input
|
| 23 |
+
# --------------------------
|
| 24 |
+
st.subheader("βοΈ Enter Your Python Code")
|
| 25 |
+
user_code = st.text_area("Paste Python code here:", height=300)
|
| 26 |
+
# --------------------------
|
| 27 |
+
# 4. Code Review Function
|
| 28 |
+
# --------------------------
|
| 29 |
+
def review_code_with_openai(code):
|
| 30 |
+
response = openai.ChatCompletion.create(
|
| 31 |
+
model="gpt-4o-mini", # You can change model
|
| 32 |
+
(gpt-4o, gpt-4o-mini)
|
| 33 |
+
messages=[
|
| 34 |
+
{"role": "system", "content": "You are a Python code reviewer. Identify bugs and suggest fixes."},
|
| 35 |
+
{"role": "user", "content": f"Review this Python code:\n{code}"} ], temperature=0.3, )
|
| 36 |
+
return response["choices"][0]["message"]["content"]
|
| 37 |
+
def review_code_with_gemini(code):
|
| 38 |
+
model = genai.GenerativeModel("gemini-2.5-pro")
|
| 39 |
+
response = model.generate_content( f"Review this Python code, detect bugs, and suggest fixes with corrected code:\n{code}" )
|
| 40 |
+
return response.text
|
| 41 |
+
# --------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
# 5. Run Code Review
|
| 43 |
+
# --------------------------
|
| 44 |
+
if st.button("π Review Code"):
|
| 45 |
+
if not user_code.strip():
|
| 46 |
+
st.warning("β οΈ Please enter some Python code first.")
|
| 47 |
+
elif not api_key:
|
| 48 |
+
st.error("β Please enter your API key in the sidebar.")
|
| 49 |
+
else:
|
| 50 |
+
with st.spinner("Analyzing your code..."):
|
| 51 |
+
try:
|
| 52 |
+
if api_choice == "OpenAI":
|
| 53 |
+
feedback = review_code_with_openai(user_code)
|
| 54 |
+
else:
|
| 55 |
+
feedback = review_code_with_gemini(user_code)
|
| 56 |
+
st.subheader("π’ Review Feedback & Fixes")
|
| 57 |
+
st.write(feedback)
|
| 58 |
+
except Exception as e:
|
| 59 |
+
st.error(f"Error: {str(e)}")
|