Hug0endob commited on
Commit
c0fd556
·
verified ·
1 Parent(s): 0bdfc16

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -26
app.py CHANGED
@@ -2,36 +2,73 @@ import streamlit as st
2
  import google.generativeai as genai
3
  import os
4
 
5
- # Configure API
6
- genai.configure(api_key="AIzaSyBiAW2GQLid0HGe9Vs_ReKwkwsSVNegNzs")
 
 
7
 
8
- # Define system prompt
9
- sys_prompt = os.getenv("USER_PROMPT")
 
 
 
 
10
 
11
- # Initialize the model
12
- model = genai.GenerativeModel(model_name="models/gemini-2.5-flash", system_instruction=sys_prompt)
 
 
 
 
13
 
14
- # Streamlit UI Configuration
15
- st.set_page_config(page_title="Python Code Reviewer 🤖", layout="centered")
 
 
 
 
16
 
17
- # Title & Introduction
18
- st.title("🐍 Python Code Reviewer 🤖")
19
- st.markdown("### Welcome! I can help you debug and improve your Python code. Just paste your code below! 🚀")
20
-
21
- # Input Text Area
22
- user_query = st.text_area("💬 Enter your Python-related code snippet or query:", height=200)
23
-
24
- # Generate Response Button
25
- if st.button("🚀 Review Code"):
26
- if user_query.strip():
27
- with st.spinner("Analyzing your code... 🧐"):
28
- response = model.generate_content(user_query)
29
-
30
- # Display Response
31
- st.markdown("### 🤖 Response:")
32
- st.success(response.text)
33
- else:
34
- st.warning("⚠️ Please enter a Python-related code snippet!")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
  # Footer
37
  st.markdown("---")
 
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("---")