Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,88 +2,72 @@ import streamlit as st
|
|
| 2 |
import google.generativeai as genai
|
| 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 ๐ค")
|
| 10 |
-
st.markdown(
|
| 11 |
-
"### Paste your code and a short prompt โ the AI will review it for you!"
|
| 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
|
| 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
|
| 32 |
code_box = st.text_area(
|
| 33 |
"๐ป Your Python code:",
|
| 34 |
height=200,
|
| 35 |
placeholder="# Paste your Python code here"
|
| 36 |
)
|
| 37 |
|
| 38 |
-
# What the model should do with the code
|
| 39 |
prompt_box = st.text_area(
|
| 40 |
"๐ What should the AI do with this code?",
|
| 41 |
height=80,
|
| 42 |
placeholder="e.g., 'Find bugs and suggest improvements.'"
|
| 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 |
-
#
|
| 55 |
api_key = api_key_input.strip() or "AIzaSyBiAW2GQLid0HGe9Vs_ReKwkwsSVNegNzs"
|
| 56 |
genai.configure(api_key=api_key)
|
| 57 |
|
| 58 |
-
#
|
| 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 |
-
#
|
|
|
|
| 72 |
user_message = (
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
| 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!_ ๐")
|
|
|
|
| 2 |
import google.generativeai as genai
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
# Page setup
|
|
|
|
|
|
|
| 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 |
+
# Inputs
|
|
|
|
|
|
|
|
|
|
| 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="Enter your own Gemini API key if you have one. Otherwise the app uses the default key."
|
| 15 |
)
|
| 16 |
|
|
|
|
| 17 |
sys_prompt_input = st.text_input(
|
| 18 |
"๐๏ธ System Prompt (optional)",
|
| 19 |
placeholder="e.g., 'You are a friendly Python code reviewer.'",
|
| 20 |
+
help="Custom instructions for the model. If left empty, the USER_PROMPT env var is used."
|
| 21 |
)
|
| 22 |
|
|
|
|
| 23 |
code_box = st.text_area(
|
| 24 |
"๐ป Your Python code:",
|
| 25 |
height=200,
|
| 26 |
placeholder="# Paste your Python code here"
|
| 27 |
)
|
| 28 |
|
|
|
|
| 29 |
prompt_box = st.text_area(
|
| 30 |
"๐ What should the AI do with this code?",
|
| 31 |
height=80,
|
| 32 |
placeholder="e.g., 'Find bugs and suggest improvements.'"
|
| 33 |
)
|
| 34 |
|
|
|
|
|
|
|
|
|
|
| 35 |
if st.button("๐ Review"):
|
|
|
|
| 36 |
if not code_box.strip():
|
| 37 |
st.warning("โ ๏ธ Please paste some Python code.")
|
| 38 |
st.stop()
|
| 39 |
|
| 40 |
+
# Select API key (user-provided > fallback)
|
| 41 |
api_key = api_key_input.strip() or "AIzaSyBiAW2GQLid0HGe9Vs_ReKwkwsSVNegNzs"
|
| 42 |
genai.configure(api_key=api_key)
|
| 43 |
|
| 44 |
+
# Select system instruction (user > env > default)
|
| 45 |
system_instruction = (
|
| 46 |
sys_prompt_input.strip()
|
| 47 |
or os.getenv("USER_PROMPT")
|
| 48 |
or "You are a helpful Python code reviewer."
|
| 49 |
)
|
| 50 |
|
|
|
|
| 51 |
model = genai.GenerativeModel(
|
| 52 |
model_name="models/gemini-2.5-flash",
|
| 53 |
system_instruction=system_instruction,
|
| 54 |
)
|
| 55 |
|
| 56 |
+
# Build message WITHOUT triple-backtick fences to avoid backslash/escape issues
|
| 57 |
+
separator = "\n---CODE START---\n"
|
| 58 |
user_message = (
|
| 59 |
+
"Please analyze the following Python code and perform the requested task.\n"
|
| 60 |
+
+ separator
|
| 61 |
+
+ code_box
|
| 62 |
+
+ "\n---CODE END---\n"
|
| 63 |
+
+ ("Task: " + (prompt_box.strip() or "Review the code."))
|
| 64 |
)
|
| 65 |
|
|
|
|
| 66 |
with st.spinner("Analyzing your code... ๐ง"):
|
| 67 |
response = model.generate_content(user_message)
|
| 68 |
|
|
|
|
| 69 |
st.markdown("### ๐ค Response")
|
| 70 |
st.success(response.text)
|
| 71 |
|
|
|
|
|
|
|
|
|
|
| 72 |
st.markdown("---")
|
| 73 |
st.markdown("๐ ๏ธ **Built with Streamlit & Google Gemini AI** | โค๏ธ _Happy Coding!_ ๐")
|