File size: 2,471 Bytes
b4213c6
 
 
 
c0fd556
 
 
 
b4213c6
c0fd556
 
 
 
 
 
b4213c6
c0fd556
 
 
 
 
 
b4213c6
c0fd556
 
 
 
 
 
b4213c6
c0fd556
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b4213c6
 
 
 
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import streamlit as st
import google.generativeai as genai
import os

# ---------- UI ----------
st.set_page_config(page_title="Python Code Reviewer 🤖", layout="centered")
st.title("🐍 Python Code Reviewer 🤖")
st.markdown("### Paste your code and a short prompt – the AI will review it for you!")

# 1️⃣ API key input (optional)
api_key_input = st.text_input(
    "🔑 Google API Key (optional – leave blank to use the built‑in key)",
    type="password",
    help="If you have your own Gemini API key, enter it here. Otherwise the app will fall back to the default key."
)

# 2️⃣ System prompt (optional)
sys_prompt = st.text_input(
    "🗒️ System Prompt (optional)",
    placeholder="e.g., 'You are a friendly Python code reviewer.'",
    help="Custom instructions for the model. If left empty, the environment variable USER_PROMPT is used."
)

# 3️⃣ Code box
code_box = st.text_area(
    "💻 Your Python code:",
    height=200,
    placeholder="# Paste your Python code here"
)

# 4️⃣ Action prompt box
prompt_box = st.text_area(
    "📝 What should the AI do with this code?",
    height=80,
    placeholder="e.g., 'Find bugs and suggest improvements.'"
)

# ---------- Logic ----------
if st.button("🚀 Review"):
    # Validate that there is code to review
    if not code_box.strip():
        st.warning("⚠️ Please paste some Python code.")
        st.stop()

    # Choose API key: user‑provided > hard‑coded fallback
    api_key = api_key_input.strip() or "AIzaSyBiAW2GQLid0HGe9Vs_ReKwkwsSVNegNzs"
    genai.configure(api_key=api_key)

    # Choose system instruction: user input > env var > none
    system_instruction = (
        sys_prompt.strip()
        or os.getenv("USER_PROMPT")
        or "You are a helpful Python code reviewer."
    )

    # Initialise model with the chosen system instruction
    model = genai.GenerativeModel(
        model_name="models/gemini-2.5-flash",
        system_instruction=system_instruction,
    )

    # Build the full user message
    user_message = f"Code:\n```python\n{code\_box}\n```\n\nTask: {prompt_box or 'Review the code.'}"

    # Call Gemini
    with st.spinner("Analyzing your code... 🧐"):
        response = model.generate_content(user_message)

    # Show result
    st.markdown("### 🤖 Response")
    st.success(response.text)

# Footer
st.markdown("---")
st.markdown("🛠️ **Built with Streamlit & Google Gemini AI** | ❤️ _Happy Coding!_ 🐍")