Hug0endob commited on
Commit
c0d7296
·
verified ·
1 Parent(s): 39ba76a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -17
app.py CHANGED
@@ -3,7 +3,7 @@ import google.generativeai as genai
3
  import os
4
 
5
  # -------------------------------------------------
6
- # 1️⃣ UI page setup
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️⃣ Input fields
16
  # -------------------------------------------------
17
- # API key (optional fallback to 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 app uses the default key."
22
  )
23
 
24
- # System prompt (optional fallback to env var or 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. If left empty, the USER_PROMPT env var is used."
29
  )
30
 
31
  # Code to be reviewed
@@ -43,49 +43,47 @@ prompt_box = st.text_area(
43
  )
44
 
45
  # -------------------------------------------------
46
- # 3️⃣ Main logic run when button pressed
47
  # -------------------------------------------------
48
  if st.button("🚀 Review"):
49
- # ---- Validate input ----
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 user message ----
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
- # ---- Call Gemini ----
80
  with st.spinner("Analyzing your code... 🧐"):
81
  response = model.generate_content(user_message)
82
 
83
- # ---- Show result ----
84
  st.markdown("### 🤖 Response")
85
  st.success(response.text)
86
 
87
  # -------------------------------------------------
88
- # 4️⃣ Footer
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!_ 🐍")