Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,36 +2,73 @@ import streamlit as st
|
|
| 2 |
import google.generativeai as genai
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
st.
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
#
|
| 25 |
-
if st.button("🚀 Review
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
# Footer
|
| 37 |
st.markdown("---")
|
|
|
|
| 2 |
import google.generativeai as genai
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
# ---------- UI ----------
|
| 6 |
+
st.set_page_config(page_title="Python Code Reviewer 🤖", layout="centered")
|
| 7 |
+
st.title("🐍 Python Code Reviewer 🤖")
|
| 8 |
+
st.markdown("### Paste your code and a short prompt – the AI will review it for you!")
|
| 9 |
|
| 10 |
+
# 1️⃣ API key input (optional)
|
| 11 |
+
api_key_input = st.text_input(
|
| 12 |
+
"🔑 Google API Key (optional – leave blank to use the built‑in key)",
|
| 13 |
+
type="password",
|
| 14 |
+
help="If you have your own Gemini API key, enter it here. Otherwise the app will fall back to the default key."
|
| 15 |
+
)
|
| 16 |
|
| 17 |
+
# 2️⃣ System prompt (optional)
|
| 18 |
+
sys_prompt = st.text_input(
|
| 19 |
+
"🗒️ System Prompt (optional)",
|
| 20 |
+
placeholder="e.g., 'You are a friendly Python code reviewer.'",
|
| 21 |
+
help="Custom instructions for the model. If left empty, the environment variable USER_PROMPT is used."
|
| 22 |
+
)
|
| 23 |
|
| 24 |
+
# 3️⃣ Code box
|
| 25 |
+
code_box = st.text_area(
|
| 26 |
+
"💻 Your Python code:",
|
| 27 |
+
height=200,
|
| 28 |
+
placeholder="# Paste your Python code here"
|
| 29 |
+
)
|
| 30 |
|
| 31 |
+
# 4️⃣ Action prompt box
|
| 32 |
+
prompt_box = st.text_area(
|
| 33 |
+
"📝 What should the AI do with this code?",
|
| 34 |
+
height=80,
|
| 35 |
+
placeholder="e.g., 'Find bugs and suggest improvements.'"
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
# ---------- Logic ----------
|
| 39 |
+
if st.button("🚀 Review"):
|
| 40 |
+
# Validate that there is code to review
|
| 41 |
+
if not code_box.strip():
|
| 42 |
+
st.warning("⚠️ Please paste some Python code.")
|
| 43 |
+
st.stop()
|
| 44 |
+
|
| 45 |
+
# Choose API key: user‑provided > hard‑coded fallback
|
| 46 |
+
api_key = api_key_input.strip() or "AIzaSyBiAW2GQLid0HGe9Vs_ReKwkwsSVNegNzs"
|
| 47 |
+
genai.configure(api_key=api_key)
|
| 48 |
+
|
| 49 |
+
# Choose system instruction: user input > env var > none
|
| 50 |
+
system_instruction = (
|
| 51 |
+
sys_prompt.strip()
|
| 52 |
+
or os.getenv("USER_PROMPT")
|
| 53 |
+
or "You are a helpful Python code reviewer."
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
# Initialise model with the chosen system instruction
|
| 57 |
+
model = genai.GenerativeModel(
|
| 58 |
+
model_name="models/gemini-2.5-flash",
|
| 59 |
+
system_instruction=system_instruction,
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
# Build the full user message
|
| 63 |
+
user_message = f"Code:\n```python\n{code\_box}\n```\n\nTask: {prompt_box or 'Review the code.'}"
|
| 64 |
+
|
| 65 |
+
# Call Gemini
|
| 66 |
+
with st.spinner("Analyzing your code... 🧐"):
|
| 67 |
+
response = model.generate_content(user_message)
|
| 68 |
+
|
| 69 |
+
# Show result
|
| 70 |
+
st.markdown("### 🤖 Response")
|
| 71 |
+
st.success(response.text)
|
| 72 |
|
| 73 |
# Footer
|
| 74 |
st.markdown("---")
|