Spaces:
Sleeping
Sleeping
File size: 2,532 Bytes
bf6404a b2d75aa bf6404a d2b18c7 bf6404a ccd424d bf6404a 57e6f5e bf6404a f61fe1c bf6404a |
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 |
import streamlit as st
import openai
import google.generativeai as genai
# --------------------------
# 1. Page Configuration
# --------------------------
st.set_page_config(page_title="AI-Powered Code Reviewer", layout="wide")
st.title("AI-Powered Python Code Reviewer")
# --------------------------
# 2. Sidebar API Selection
# --------------------------
st.sidebar.header("⚙️ Settings")
api_choice = st.sidebar.radio("Choose API:", ["OpenAI", "Google Gemini"])
if api_choice == "OpenAI":
api_key = st.sidebar.text_input("Enter OpenAI API Key", type="password")
if api_key:
openai.api_key = api_key
else:
api_key = st.sidebar.text_input("Enter Google API Key", type="password")
if api_key:
genai.configure(api_key=api_key)
# --------------------------
# 3. User Code Input
# --------------------------
st.subheader("✍️ Enter Your Python Code")
user_code = st.text_area("Paste Python code here:", height=300)
# --------------------------
# 4. Code Review Function
# --------------------------
def review_code_with_openai(code):
response = openai.ChatCompletion.create(
model="gpt-4o-mini", # You can change model (gpt-4o, gpt-4o-mini)
messages=[
{"role": "system", "content": "You are a Python code reviewer. Identify bugs and suggest fixes."},
{"role": "user", "content": f"Review this Python code:\n{code}"} ], temperature=0.3, )
return response["choices"][0]["message"]["content"]
def review_code_with_gemini(code):
model = genai.GenerativeModel("gemini-2.5-pro")
response = model.generate_content( f"Review this Python code, detect bugs, and suggest fixes with corrected code:\n{code}" )
return response.text
# --------------------------
# 5. Run Code Review
# --------------------------
if st.button("🔍 Review Code"):
if not user_code.strip():
st.warning("⚠️ Please enter some Python code first.")
elif not api_key:
st.error("❌ Please enter your API key in the sidebar.")
else:
with st.spinner("Analyzing your code..."):
try:
if api_choice == "OpenAI":
feedback = review_code_with_openai(user_code)
else:
feedback = review_code_with_gemini(user_code)
st.subheader("📢 Review Feedback & Fixes")
st.write(feedback)
except Exception as e:
st.error(f"Error: {str(e)}") |