Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,7 @@ import google.generativeai as genai
|
|
| 3 |
import os
|
| 4 |
|
| 5 |
# -------------------------------------------------
|
| 6 |
-
# 1️⃣
|
| 7 |
# -------------------------------------------------
|
| 8 |
st.set_page_config(page_title="Python Code Reviewer 🤖", layout="centered")
|
| 9 |
st.title("🐍 Python Code Reviewer 🤖")
|
|
@@ -12,20 +12,20 @@ st.markdown(
|
|
| 12 |
)
|
| 13 |
|
| 14 |
# -------------------------------------------------
|
| 15 |
-
# 2️⃣
|
| 16 |
# -------------------------------------------------
|
| 17 |
-
# API key
|
| 18 |
api_key_input = st.text_input(
|
| 19 |
"🔑 Google API Key (optional – leave blank to use the built‑in key)",
|
| 20 |
type="password",
|
| 21 |
-
help="Enter your own Gemini API key if you have one
|
| 22 |
)
|
| 23 |
|
| 24 |
-
#
|
| 25 |
sys_prompt_input = st.text_input(
|
| 26 |
"🗒️ System Prompt (optional)",
|
| 27 |
placeholder="e.g., 'You are a friendly Python code reviewer.'",
|
| 28 |
-
help="Custom instructions for the model.
|
| 29 |
)
|
| 30 |
|
| 31 |
# Code to be reviewed
|
|
@@ -43,49 +43,47 @@ prompt_box = st.text_area(
|
|
| 43 |
)
|
| 44 |
|
| 45 |
# -------------------------------------------------
|
| 46 |
-
# 3️⃣
|
| 47 |
# -------------------------------------------------
|
| 48 |
if st.button("🚀 Review"):
|
| 49 |
-
# ---- Validate
|
| 50 |
if not code_box.strip():
|
| 51 |
st.warning("⚠️ Please paste some Python code.")
|
| 52 |
st.stop()
|
| 53 |
|
| 54 |
-
# ---- Choose API key ----
|
| 55 |
-
# User‑provided key > hard‑coded fallback
|
| 56 |
api_key = api_key_input.strip() or "AIzaSyBiAW2GQLid0HGe9Vs_ReKwkwsSVNegNzs"
|
| 57 |
genai.configure(api_key=api_key)
|
| 58 |
|
| 59 |
-
# ---- Choose system instruction ----
|
| 60 |
-
# User input > env var > generic default
|
| 61 |
system_instruction = (
|
| 62 |
sys_prompt_input.strip()
|
| 63 |
or os.getenv("USER_PROMPT")
|
| 64 |
or "You are a helpful Python code reviewer."
|
| 65 |
)
|
| 66 |
|
| 67 |
-
# ---- Initialise model ----
|
| 68 |
model = genai.GenerativeModel(
|
| 69 |
model_name="models/gemini-2.5-flash",
|
| 70 |
system_instruction=system_instruction,
|
| 71 |
)
|
| 72 |
|
| 73 |
-
# ---- Build the
|
| 74 |
user_message = (
|
| 75 |
f"Code:\n```python\n{code\_box}\n```\n\n"
|
| 76 |
f"Task: {prompt_box or 'Review the code.'}"
|
| 77 |
)
|
| 78 |
|
| 79 |
-
# ----
|
| 80 |
with st.spinner("Analyzing your code... 🧐"):
|
| 81 |
response = model.generate_content(user_message)
|
| 82 |
|
| 83 |
-
# ----
|
| 84 |
st.markdown("### 🤖 Response")
|
| 85 |
st.success(response.text)
|
| 86 |
|
| 87 |
# -------------------------------------------------
|
| 88 |
-
# 4️⃣
|
| 89 |
# -------------------------------------------------
|
| 90 |
st.markdown("---")
|
| 91 |
st.markdown("🛠️ **Built with Streamlit & Google Gemini AI** | ❤️ _Happy Coding!_ 🐍")
|
|
|
|
| 3 |
import os
|
| 4 |
|
| 5 |
# -------------------------------------------------
|
| 6 |
+
# 1️⃣ Page configuration
|
| 7 |
# -------------------------------------------------
|
| 8 |
st.set_page_config(page_title="Python Code Reviewer 🤖", layout="centered")
|
| 9 |
st.title("🐍 Python Code Reviewer 🤖")
|
|
|
|
| 12 |
)
|
| 13 |
|
| 14 |
# -------------------------------------------------
|
| 15 |
+
# 2️⃣ Input fields
|
| 16 |
# -------------------------------------------------
|
| 17 |
+
# Optional API key – falls back to the hard‑coded key
|
| 18 |
api_key_input = st.text_input(
|
| 19 |
"🔑 Google API Key (optional – leave blank to use the built‑in key)",
|
| 20 |
type="password",
|
| 21 |
+
help="Enter your own Gemini API key if you have one; otherwise the default key is used."
|
| 22 |
)
|
| 23 |
|
| 24 |
+
# Optional system prompt – falls back to env var or a generic default
|
| 25 |
sys_prompt_input = st.text_input(
|
| 26 |
"🗒️ System Prompt (optional)",
|
| 27 |
placeholder="e.g., 'You are a friendly Python code reviewer.'",
|
| 28 |
+
help="Custom instructions for the model."
|
| 29 |
)
|
| 30 |
|
| 31 |
# Code to be reviewed
|
|
|
|
| 43 |
)
|
| 44 |
|
| 45 |
# -------------------------------------------------
|
| 46 |
+
# 3️⃣ Main logic (runs on button press)
|
| 47 |
# -------------------------------------------------
|
| 48 |
if st.button("🚀 Review"):
|
| 49 |
+
# ---- Validate that code was provided ----
|
| 50 |
if not code_box.strip():
|
| 51 |
st.warning("⚠️ Please paste some Python code.")
|
| 52 |
st.stop()
|
| 53 |
|
| 54 |
+
# ---- Choose API key (user‑provided > fallback) ----
|
|
|
|
| 55 |
api_key = api_key_input.strip() or "AIzaSyBiAW2GQLid0HGe9Vs_ReKwkwsSVNegNzs"
|
| 56 |
genai.configure(api_key=api_key)
|
| 57 |
|
| 58 |
+
# ---- Choose system instruction (user input > env var > default) ----
|
|
|
|
| 59 |
system_instruction = (
|
| 60 |
sys_prompt_input.strip()
|
| 61 |
or os.getenv("USER_PROMPT")
|
| 62 |
or "You are a helpful Python code reviewer."
|
| 63 |
)
|
| 64 |
|
| 65 |
+
# ---- Initialise the Gemini model ----
|
| 66 |
model = genai.GenerativeModel(
|
| 67 |
model_name="models/gemini-2.5-flash",
|
| 68 |
system_instruction=system_instruction,
|
| 69 |
)
|
| 70 |
|
| 71 |
+
# ---- Build the message sent to Gemini ----
|
| 72 |
user_message = (
|
| 73 |
f"Code:\n```python\n{code\_box}\n```\n\n"
|
| 74 |
f"Task: {prompt_box or 'Review the code.'}"
|
| 75 |
)
|
| 76 |
|
| 77 |
+
# ---- Generate the response ----
|
| 78 |
with st.spinner("Analyzing your code... 🧐"):
|
| 79 |
response = model.generate_content(user_message)
|
| 80 |
|
| 81 |
+
# ---- Display the result ----
|
| 82 |
st.markdown("### 🤖 Response")
|
| 83 |
st.success(response.text)
|
| 84 |
|
| 85 |
# -------------------------------------------------
|
| 86 |
+
# 4️⃣ Footer
|
| 87 |
# -------------------------------------------------
|
| 88 |
st.markdown("---")
|
| 89 |
st.markdown("🛠️ **Built with Streamlit & Google Gemini AI** | ❤️ _Happy Coding!_ 🐍")
|