File size: 4,010 Bytes
f8dbf90 83ac817 f8dbf90 83ac817 f8dbf90 83ac817 f8dbf90 83ac817 f8dbf90 83ac817 f8dbf90 83ac817 f8dbf90 83ac817 f8dbf90 258dc70 f8dbf90 83ac817 f8dbf90 83ac817 |
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 |
import os
import streamlit as st
import google.generativeai as genai
import requests
import json
from streamlit_lottie import st_lottie
from streamlit_ace import st_ace
# 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 load_lottieurl(url: str):
r = requests.get(url)
if r.status_code != 200:
return None
return r.json()
# Load Lottie animation
lottie_url = "https://assets7.lottiefiles.com/packages/lf20_s1jf29.json"
lottie_json = load_lottieurl(lottie_url)
# Streamlit UI setup
st.set_page_config(page_title="AI Code Assistant", page_icon="🤖", layout="wide")
# Custom CSS
st.markdown("""
<style>
.main {
background-color: #f0f2f6;
font-family: 'Roboto', sans-serif;
}
h1, h2 {
color: #333;
font-weight: bold;
}
.stTextArea label {
font-size: 1.2rem;
color: #333;
}
.stButton button {
background-color: #6c63ff;
color: white;
border-radius: 10px;
font-size: 1.1rem;
transition: all 0.3s ease;
}
.stButton button:hover {
background-color: #5a52d1;
transform: translateY(-2px);
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
}
.stCodeBlock {
background-color: #272822;
color: #f8f8f2;
font-size: 1rem;
border-radius: 10px;
}
.css-1v0mbdj.etr89bj1 {
display: flex;
flex-direction: column;
align-items: center;
}
</style>
""", unsafe_allow_html=True)
# App header
st.title("🤖 AI Code Assistant")
st.markdown("#### Powered by Google Gemini")
# Sidebar
st.sidebar.header("Settings")
language = st.sidebar.selectbox("Select programming language", ["Python", "JavaScript", "Java", "C++", "Ruby"])
# Main content
col1, col2 = st.columns([2, 1])
with col1:
prompt = st.text_area("Enter your coding question or request:", height=150)
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)
st.success("Code generated successfully!")
# Display code in an interactive editor
generated_code = st_ace(value=completed_text, language=language.lower(), theme="monokai", height=300)
with col2:
if lottie_json:
st_lottie(lottie_json, height=300, key="coding")
# Additional features
st.header("Additional Tools")
# Code explanation
if st.checkbox("Explain the generated code"):
if 'generated_code' in locals():
explanation = generate_response(f"Explain the following {language} code:\n\n{generated_code}")
st.write(explanation)
else:
st.warning("Generate some code first to get an explanation.")
# Code optimization
if st.checkbox("Optimize the generated code"):
if 'generated_code' in locals():
optimized_code = generate_response(f"Optimize the following {language} code:\n\n{generated_code}")
st.code(optimized_code, language=language.lower())
else:
st.warning("Generate some code first to optimize it.")
# Footer
st.markdown("---")
st.markdown("Created with ❤️ by Your Name") |