Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import google.generativeai as genai | |
| import os | |
| # ------------------------------------------------- | |
| # 1️⃣ Page configuration | |
| # ------------------------------------------------- | |
| st.set_page_config(page_title="Python Code Reviewer 🤖", layout="centered") | |
| st.title("🐍 Python Code Reviewer 🤖") | |
| st.markdown( | |
| "### Paste your code and a short prompt – the AI will review it for you!" | |
| ) | |
| # ------------------------------------------------- | |
| # 2️⃣ Input fields | |
| # ------------------------------------------------- | |
| # Optional API key – falls back to the hard‑coded key | |
| api_key_input = st.text_input( | |
| "🔑 Google API Key (optional – leave blank to use the built‑in key)", | |
| type="password", | |
| help="Enter your own Gemini API key if you have one; otherwise the default key is used." | |
| ) | |
| # Optional system prompt – falls back to env var or a generic default | |
| sys_prompt_input = st.text_input( | |
| "🗒️ System Prompt (optional)", | |
| placeholder="e.g., 'You are a friendly Python code reviewer.'", | |
| help="Custom instructions for the model." | |
| ) | |
| # Code to be reviewed | |
| code_box = st.text_area( | |
| "💻 Your Python code:", | |
| height=200, | |
| placeholder="# Paste your Python code here" | |
| ) | |
| # What the model should do with the code | |
| prompt_box = st.text_area( | |
| "📝 What should the AI do with this code?", | |
| height=80, | |
| placeholder="e.g., 'Find bugs and suggest improvements.'" | |
| ) | |
| # ------------------------------------------------- | |
| # 3️⃣ Main logic (runs on button press) | |
| # ------------------------------------------------- | |
| if st.button("🚀 Review"): | |
| # ---- Validate that code was provided ---- | |
| if not code_box.strip(): | |
| st.warning("⚠️ Please paste some Python code.") | |
| st.stop() | |
| # ---- Choose API key (user‑provided > fallback) ---- | |
| api_key = api_key_input.strip() or "AIzaSyBiAW2GQLid0HGe9Vs_ReKwkwsSVNegNzs" | |
| genai.configure(api_key=api_key) | |
| # ---- Choose system instruction (user input > env var > default) ---- | |
| system_instruction = ( | |
| sys_prompt_input.strip() | |
| or os.getenv("USER_PROMPT") | |
| or "You are a helpful Python code reviewer." | |
| ) | |
| # ---- Initialise the Gemini model ---- | |
| model = genai.GenerativeModel( | |
| model_name="models/gemini-2.5-flash", | |
| system_instruction=system_instruction, | |
| ) | |
| # ---- Build the message sent to Gemini ---- | |
| user_message = ( | |
| f"Code:\n```python\n{code\_box}\n```\n\n" | |
| f"Task: {prompt_box or 'Review the code.'}" | |
| ) | |
| # ---- Generate the response ---- | |
| with st.spinner("Analyzing your code... 🧐"): | |
| response = model.generate_content(user_message) | |
| # ---- Display the result ---- | |
| st.markdown("### 🤖 Response") | |
| st.success(response.text) | |
| # ------------------------------------------------- | |
| # 4️⃣ Footer | |
| # ------------------------------------------------- | |
| st.markdown("---") | |
| st.markdown("🛠️ **Built with Streamlit & Google Gemini AI** | ❤️ _Happy Coding!_ 🐍") | |