import streamlit as st
import google.generativeai as genai
import pyperclip
import time
# Configure the Gemini API
genai.configure(api_key=st.secrets["GOOGLE_API_KEY"])
# Create the model with system instructions
generation_config = {
"temperature": 1,
"top_p": 0.95,
"top_k": 64,
"max_output_tokens": 8192,
"response_mime_type": "text/plain",
}
model = genai.GenerativeModel(
model_name="gemini-1.5-pro",
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):
response = chat_session.send_message(user_input)
return response.text
def copy_to_clipboard(text):
pyperclip.copy(text)
# Streamlit UI setup
st.set_page_config(page_title="Advanced AI Code Assistant", page_icon="🚀", layout="wide")
st.markdown("""
""", unsafe_allow_html=True)
st.markdown('
', unsafe_allow_html=True)
st.title("🚀 Advanced 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=100)
col1, col2 = st.columns([1, 1])
with col1:
generate_button = st.button("Generate Code")
with col2:
language = st.selectbox("Select language", ["Python", "JavaScript", "Java", "C++", "Ruby"])
if generate_button:
if prompt.strip() == "":
st.error("Please enter a valid prompt.")
else:
with st.spinner("Generating code..."):
completed_text = generate_response(prompt)
st.success("Code generated successfully!")
st.markdown('
', unsafe_allow_html=True)
code_blocks = completed_text.split("\n\n")
for i, block in enumerate(code_blocks):
st.markdown(f'
', unsafe_allow_html=True)
st.code(block, language=language.lower())
copy_button = st.button(f"Copy Code Block {i+1}", key=f"copy_{i}")
if copy_button:
copy_to_clipboard(block)
st.success(f"Code block {i+1} copied to clipboard!")
st.markdown('
', unsafe_allow_html=True)
st.markdown('
', unsafe_allow_html=True)
st.markdown("""
Created with ❤️ by Your Advanced AI Code Assistant
""", unsafe_allow_html=True)
st.markdown('
', unsafe_allow_html=True)