CoolFace
Apppublic

rk803/code_reviewer

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
app.py57 linesDownload Raw Back to root
1import streamlit as st2import google.generativeai as genai3import os4 5# Configure API Key securely6genai.configure(api_key="AIzaSyCSJIjNAHdxZucIbBbSOF0hCDqNPoqheYw")7 8# Initialize AI model9model = genai.GenerativeModel("gemini-1.5-flash")10 11def analyze_code(code):12    """Analyzes Python code, detects bugs, and provides a fixed version of the code only."""13    prompt = f"""14    You are a Python code reviewer. Analyze the following Python code for errors, 15    bugs. Provide a bug report and a fixed version of the code only.16 17    Code:18    19    {code}20    21 22    Format:23    Bug Report: List of detected issues.24    Fixed Code: A corrected version of the code.25    """26 27    response = model.generate_content(prompt)28    output = response.text29 30    if "Fixed Code:" in output:31        bug_report, fixed_code = output.split("Fixed Code:")32    else:33        bug_report = output34        fixed_code = "No fixed code generated."35 36    return bug_report.strip(), fixed_code.strip()37 38# Streamlit UI39st.set_page_config(page_title="AI Python Code Reviewer", layout="centered")40 41st.title(" AI Code Reviewer for Python ")42#st.write("Enter your Python code here!")43 44user_code = st.text_area("Enter your Python code here:", height=100)45 46if st.button("Generate "):47    if user_code:48        feedback, fixed_code = analyze_code(user_code)49        st.subheader("๐Ÿ” Bug Report ")50        st.write(feedback)51 52        st.subheader(" Fixed Code")53        st.code(fixed_code, language="python")54 55        56    else:57        st.warning("โš ๏ธ Please enter some code to analyze!")