Code / app.py
Artificial-superintelligence's picture
Update app.py
4319a04 verified
raw
history blame
6.78 kB
import streamlit as st
import google.generativeai as genai
# Configure the Gemini API
genai.configure(api_key=st.secrets["GOOGLE_API_KEY"])
# Create the model with enhanced system instructions
generation_config = {
"temperature": 0.7,
"top_p": 0.95,
"top_k": 64,
"max_output_tokens": 10240,
}
model = genai.GenerativeModel(
model_name="gemini-1.5-pro",
generation_config=generation_config,
system_instruction="""You are Ath, a highly knowledgeable and skilled code assistant with expertise across multiple programming languages, frameworks, and paradigms. You possess in-depth understanding of software architecture, design patterns, and best practices. Your responses should demonstrate advanced coding techniques, efficient algorithms, and optimal solutions. Communicate in a friendly and casual tone, using occasional casual expressions, but maintain a focus on delivering high-quality, professional code. Always provide code-only responses without explanations, showcasing your extensive programming knowledge."""
)
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}"
# Streamlit UI setup
st.set_page_config(page_title="CodeGenius", page_icon="πŸš€", layout="wide")
st.markdown("""
<style>
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;600&family=JetBrains+Mono:wght@400;500&display=swap');
body {
font-family: 'Inter', sans-serif;
background-color: #f8f9fa;
color: #343a40;
}
.stApp {
max-width: 900px;
margin: 0 auto;
padding: 2rem;
}
.main-container {
background: #ffffff;
border-radius: 12px;
padding: 2rem;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
}
h1 {
font-size: 2.5rem;
font-weight: 600;
color: #212529;
text-align: center;
margin-bottom: 0.5rem;
}
.subtitle {
font-size: 1rem;
text-align: center;
color: #6c757d;
margin-bottom: 2rem;
}
.stTextArea textarea {
background-color: #f1f3f5;
color: #495057;
border: 1px solid #ced4da;
border-radius: 6px;
font-size: 1rem;
font-family: 'JetBrains Mono', monospace;
padding: 0.75rem;
transition: all 0.2s ease;
}
.stTextArea textarea:focus {
border-color: #4dabf7;
box-shadow: 0 0 0 2px rgba(77, 171, 247, 0.2);
}
.stButton button {
background-color: #4dabf7;
color: #ffffff;
border: none;
border-radius: 6px;
font-size: 1rem;
font-weight: 500;
padding: 0.6rem 1.2rem;
transition: all 0.2s ease;
}
.stButton button:hover {
background-color: #3793dd;
transform: translateY(-1px);
}
.output-container {
background: #f8f9fa;
border-radius: 6px;
padding: 1.5rem;
margin-top: 2rem;
border: 1px solid #e9ecef;
}
.code-block {
background-color: #f1f3f5;
color: #212529;
font-family: 'JetBrains Mono', monospace;
font-size: 0.9rem;
border-radius: 6px;
padding: 1.25rem;
margin-top: 1rem;
overflow-x: auto;
}
.stAlert {
background-color: #e9ecef;
color: #495057;
border-radius: 6px;
border: 1px solid #ced4da;
padding: 0.75rem 1rem;
margin-bottom: 1rem;
}
.stSpinner {
color: #4dabf7;
}
/* Custom scrollbar */
::-webkit-scrollbar {
width: 6px;
height: 6px;
}
::-webkit-scrollbar-track {
background: #f1f3f5;
border-radius: 3px;
}
::-webkit-scrollbar-thumb {
background: #adb5bd;
border-radius: 3px;
}
::-webkit-scrollbar-thumb:hover {
background: #868e96;
}
.language-selector {
margin-bottom: 1rem;
}
.copy-button {
background-color: #e9ecef;
color: #495057;
border: none;
border-radius: 4px;
padding: 0.4rem 0.8rem;
font-size: 0.9rem;
cursor: pointer;
transition: all 0.2s ease;
}
.copy-button:hover {
background-color: #ced4da;
}
</style>
""", unsafe_allow_html=True)
st.markdown('<div class="main-container">', unsafe_allow_html=True)
st.title("πŸš€ CodeGenius")
st.markdown('<p class="subtitle">Elevate Your Code with AI-Powered Insights</p>', unsafe_allow_html=True)
languages = ["Python", "JavaScript", "Java", "C++", "Ruby", "Go", "Rust", "TypeScript", "PHP", "Swift"]
selected_language = st.selectbox("Select your programming language:", languages, key="language-selector")
prompt = st.text_area(f"What {selected_language} challenge can I help you with?", height=100)
col1, col2, col3 = st.columns([2, 1, 1])
with col1:
generate_button = st.button("Generate Solution")
with col2:
complexity = st.radio("Complexity", ["Low", "Medium", "High"], horizontal=True)
with col3:
style = st.radio("Style", ["Concise", "Detailed"], horizontal=True)
if generate_button:
if prompt.strip() == "":
st.warning("Please enter a valid prompt.")
else:
with st.spinner(f"Crafting {complexity.lower()} complexity {selected_language} solution..."):
completed_text = generate_response(f"{complexity} complexity, {style} {selected_language} code for: {prompt}")
if "An error occurred" in completed_text:
st.error(completed_text)
else:
st.success(f"Solution generated successfully!")
st.markdown('<div class="output-container">', unsafe_allow_html=True)
st.markdown('<div class="code-block">', unsafe_allow_html=True)
st.code(completed_text, language=selected_language.lower())
st.markdown('</div>', unsafe_allow_html=True)
# Add copy to clipboard button
st.markdown(
f"""
<button onclick="navigator.clipboard.writeText(`{completed_text}`)" class="copy-button">
Copy to Clipboard
</button>
""",
unsafe_allow_html=True
)
st.markdown('</div>', unsafe_allow_html=True)
st.markdown("""
<div style='text-align: center; margin-top: 2rem; color: #adb5bd; font-size: 0.9rem;'>
Powered by CodeGenius AI β€’ Simplifying Complex Code Challenges
</div>
""", unsafe_allow_html=True)
st.markdown('</div>', unsafe_allow_html=True)