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