import streamlit as st
import google.generativeai as genai
import subprocess
# Configure the Gemini API
genai.configure(api_key=st.secrets["GOOGLE_API_KEY"])
# Create the model with system instructions
generation_config = {
"temperature": 0.5,
"top_p": 0.95,
"top_k": 64,
"max_output_tokens": 12288,
}
model = genai.GenerativeModel(
model_name="gemini-2.0-pro", # Assuming a more advanced model is available
generation_config=generation_config,
system_instruction="You are a human named Ath who is a highly knowledgeable code assistant. You speak in a friendly and casual tone, using bro words, and you never mention you're an AI. Your responses should contain code only, without explanations.",
)
chat_session = model.start_chat(history=[])
def generate_response(user_input):
try:
response = chat_session.send_message(user_input)
return response.text
except Exception as e:
return f"An error occurred: {e}"
def validate_code(code, language):
try:
if language == "Python":
result = subprocess.run(['python', '-c', code], capture_output=True, text=True)
elif language == "JavaScript":
result = subprocess.run(['node', '-e', code], capture_output=True, text=True)
# Add more languages as needed
return result.stdout, result.stderr
except Exception as e:
return None, f"Validation error: {e}"
# Streamlit UI setup
st.set_page_config(page_title="Sleek AI Code Assistant", page_icon="💻", layout="wide")
st.markdown("""
""", unsafe_allow_html=True)
st.markdown('
', unsafe_allow_html=True)
st.title("💻 Sleek AI Code Assistant")
st.markdown('
Powered by Google Gemini
', unsafe_allow_html=True)
prompt = st.text_area("What code can I help you with today?", height=120)
language = st.selectbox("Select programming language", ["Python", "JavaScript", "Other"])
if st.button("Generate Code"):
if prompt.strip() == "":
st.error("Please enter a valid prompt.")
else:
with st.spinner("Generating code..."):
completed_text = generate_response(prompt)
if "An error occurred" in completed_text:
st.error(completed_text)
else:
st.success("Code generated successfully!")
st.markdown('
', unsafe_allow_html=True)
st.markdown('
', unsafe_allow_html=True)
st.code(completed_text)
st.markdown('
', unsafe_allow_html=True)
st.markdown('
', unsafe_allow_html=True)
if language in ["Python", "JavaScript"]:
with st.spinner("Validating code..."):
stdout, stderr = validate_code(completed_text, language)
if stdout:
st.info(f"Validation Output:\n{stdout}")
if stderr:
st.warning(f"Validation Errors:\n{stderr}")
st.markdown("""
Created with ❤️ by Your Sleek AI Code Assistant
""", unsafe_allow_html=True)
st.markdown('
', unsafe_allow_html=True)