Ai_Code_Viewer / app.py
DasariHarshitha's picture
Update app.py
ccd424d verified
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)}")