Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import google.generativeai as genai | |
| import os | |
| # Page setup | |
| 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!") | |
| # Inputs | |
| 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 app uses the default key." | |
| ) | |
| 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. If left empty, the USER_PROMPT env var is used." | |
| ) | |
| code_box = st.text_area( | |
| "π» Your Python code:", | |
| height=200, | |
| placeholder="# Paste your Python code here" | |
| ) | |
| prompt_box = st.text_area( | |
| "π What should the AI do with this code?", | |
| height=80, | |
| placeholder="e.g., 'Find bugs and suggest improvements.'" | |
| ) | |
| if st.button("π Review"): | |
| if not code_box.strip(): | |
| st.warning("β οΈ Please paste some Python code.") | |
| st.stop() | |
| # Select API key (user-provided > fallback) | |
| api_key = api_key_input.strip() or "AIzaSyBiAW2GQLid0HGe9Vs_ReKwkwsSVNegNzs" | |
| genai.configure(api_key=api_key) | |
| # Select system instruction (user > env > default) | |
| system_instruction = ( | |
| sys_prompt_input.strip() | |
| or os.getenv("USER_PROMPT") | |
| or "You are a helpful Python code reviewer." | |
| ) | |
| model = genai.GenerativeModel( | |
| model_name="models/gemini-2.5-flash", | |
| system_instruction=system_instruction, | |
| ) | |
| # Build message WITHOUT triple-backtick fences to avoid backslash/escape issues | |
| separator = "\n---CODE START---\n" | |
| user_message = ( | |
| "Please analyze the following Python code and perform the requested task.\n" | |
| + separator | |
| + code_box | |
| + "\n---CODE END---\n" | |
| + ("Task: " + (prompt_box.strip() or "Review the code.")) | |
| ) | |
| with st.spinner("Analyzing your code... π§"): | |
| response = model.generate_content(user_message) | |
| st.markdown("### π€ Response") | |
| st.success(response.text) | |
| st.markdown("---") | |
| st.markdown("π οΈ **Built with Streamlit & Google Gemini AI** | β€οΈ _Happy Coding!_ π") | |