File size: 5,317 Bytes
f8dbf90 b479e07 f8dbf90 83ac817 f8dbf90 b479e07 f8dbf90 b479e07 f8dbf90 b479e07 d35faf8 b479e07 f8dbf90 d35faf8 b479e07 d35faf8 b479e07 d35faf8 b479e07 d35faf8 b479e07 d35faf8 b479e07 d35faf8 b479e07 d35faf8 f8dbf90 b479e07 d35faf8 b479e07 d35faf8 b479e07 f8dbf90 b479e07 f8dbf90 d35faf8 f8dbf90 d35faf8 83ac817 d35faf8 f8dbf90 83ac817 d35faf8 f8dbf90 b479e07 d35faf8 258dc70 d35faf8 b479e07 f8dbf90 d35faf8 b479e07 d35faf8 b479e07 d35faf8 b479e07 83ac817 f8dbf90 83ac817 b479e07 83ac817 b479e07 83ac817 b479e07 d35faf8 83ac817 d35faf8 b479e07 d35faf8 b479e07 83ac817 d35faf8 b479e07 d35faf8 b479e07 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
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("""
<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) |