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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -20
app.py CHANGED
@@ -2,74 +2,90 @@ import streamlit as st
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("---")
75
  st.markdown("🛠️ **Built with Streamlit & Google Gemini AI** | ❤️ _Happy Coding!_ 🐍")
 
2
  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 🤖")
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
+ # 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
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 – 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!_ 🐍")