|
import streamlit as st |
|
import google.generativeai as genai |
|
import pyperclip |
|
import time |
|
|
|
|
|
genai.configure(api_key=st.secrets["GOOGLE_API_KEY"]) |
|
|
|
|
|
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) |
|
|
|
|
|
st.set_page_config(page_title="Advanced AI Code Assistant", page_icon="π", layout="wide") |
|
|
|
st.markdown(""" |
|
<style> |
|
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;600;700&display=swap'); |
|
|
|
body { |
|
font-family: 'Poppins', sans-serif; |
|
background: linear-gradient(120deg, #84fab0 0%, #8fd3f4 100%); |
|
color: #2c3e50; |
|
} |
|
.stApp { |
|
max-width: 1200px; |
|
margin: 0 auto; |
|
padding: 2rem; |
|
} |
|
.main-container { |
|
background: rgba(255, 255, 255, 0.95); |
|
border-radius: 20px; |
|
padding: 2rem; |
|
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1); |
|
} |
|
h1 { |
|
font-size: 2.5rem; |
|
font-weight: 700; |
|
color: #3498db; |
|
text-align: center; |
|
margin-bottom: 1rem; |
|
} |
|
.subtitle { |
|
font-size: 1.2rem; |
|
text-align: center; |
|
color: #7f8c8d; |
|
margin-bottom: 2rem; |
|
} |
|
.stTextArea label { |
|
font-size: 1.1rem; |
|
font-weight: 600; |
|
color: #34495e; |
|
} |
|
.stTextArea textarea { |
|
border: 2px solid #3498db; |
|
border-radius: 10px; |
|
font-size: 1rem; |
|
padding: 0.5rem; |
|
} |
|
.stButton button { |
|
background: linear-gradient(45deg, #3498db, #2980b9); |
|
color: white; |
|
border: none; |
|
border-radius: 30px; |
|
font-size: 1.1rem; |
|
font-weight: 600; |
|
padding: 0.7rem 2rem; |
|
transition: all 0.3s ease; |
|
box-shadow: 0 4px 6px rgba(0,0,0,0.1); |
|
} |
|
.stButton button:hover { |
|
transform: translateY(-2px); |
|
box-shadow: 0 6px 8px rgba(0,0,0,0.15); |
|
} |
|
.output-container { |
|
background: #f8f9fa; |
|
border-radius: 10px; |
|
padding: 1rem; |
|
margin-top: 2rem; |
|
} |
|
.code-block { |
|
background-color: #282c34; |
|
color: #abb2bf; |
|
font-family: 'Fira Code', monospace; |
|
font-size: 0.9rem; |
|
border-radius: 10px; |
|
padding: 1rem; |
|
margin-top: 1rem; |
|
position: relative; |
|
} |
|
.copy-btn { |
|
position: absolute; |
|
top: 0.5rem; |
|
right: 0.5rem; |
|
background: rgba(255, 255, 255, 0.1); |
|
color: #abb2bf; |
|
border: none; |
|
border-radius: 5px; |
|
padding: 0.2rem 0.5rem; |
|
font-size: 0.8rem; |
|
cursor: pointer; |
|
transition: all 0.2s ease; |
|
} |
|
.copy-btn:hover { |
|
background: rgba(255, 255, 255, 0.2); |
|
} |
|
.stAlert { |
|
background-color: #e8f4fd; |
|
color: #3498db; |
|
border-radius: 10px; |
|
border: none; |
|
padding: 0.5rem 1rem; |
|
} |
|
.stSpinner { |
|
color: #3498db; |
|
} |
|
</style> |
|
""", unsafe_allow_html=True) |
|
|
|
st.markdown('<div class="main-container">', unsafe_allow_html=True) |
|
st.title("π Advanced AI Code Assistant") |
|
st.markdown('<p class="subtitle">Powered by Google Gemini</p>', 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('<div class="output-container">', unsafe_allow_html=True) |
|
code_blocks = completed_text.split("\n\n") |
|
for i, block in enumerate(code_blocks): |
|
st.markdown(f'<div class="code-block">', 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('</div>', unsafe_allow_html=True) |
|
st.markdown('</div>', unsafe_allow_html=True) |
|
|
|
st.markdown(""" |
|
<div style='text-align: center; margin-top: 2rem; color: #7f8c8d;'> |
|
Created with β€οΈ by Your Advanced AI Code Assistant |
|
</div> |
|
""", unsafe_allow_html=True) |
|
|
|
st.markdown('</div>', unsafe_allow_html=True) |