Hug0endob commited on
Commit
c4de28a
ยท
verified ยท
1 Parent(s): c0d7296

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -30
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; 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
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
- # ---- 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!_ ๐Ÿ")
 
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!_ ๐Ÿ")