| import streamlit as st |
| import google.generativeai as genai |
|
|
| |
| genai.configure(api_key=st.secrets["GOOGLE_API_KEY"]) |
|
|
| |
| 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}" |
|
|
| |
| st.set_page_config(page_title="CodeCraft AI", page_icon="🧠", layout="wide") |
|
|
| st.markdown(""" |
| <style> |
| @import url('https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;700&family=Fira+Code:wght@400;500&display=swap'); |
| |
| body { |
| font-family: 'Roboto', sans-serif; |
| background-color: #1e1e1e; |
| color: #e0e0e0; |
| } |
| .stApp { |
| max-width: 1000px; |
| margin: 0 auto; |
| padding: 2rem; |
| } |
| .main-container { |
| background: #2d2d2d; |
| border-radius: 15px; |
| padding: 2rem; |
| box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3); |
| } |
| h1 { |
| font-size: 2.5rem; |
| font-weight: 700; |
| color: #61dafb; |
| text-align: center; |
| margin-bottom: 1rem; |
| text-shadow: 0 0 10px rgba(97, 218, 251, 0.5); |
| } |
| .subtitle { |
| font-size: 1.1rem; |
| text-align: center; |
| color: #bbb; |
| margin-bottom: 2rem; |
| } |
| .stTextArea textarea { |
| background-color: #3a3a3a; |
| color: #e0e0e0; |
| border: 1px solid #4a4a4a; |
| border-radius: 8px; |
| font-size: 1rem; |
| font-family: 'Fira Code', monospace; |
| padding: 1rem; |
| transition: all 0.3s ease; |
| } |
| .stTextArea textarea:focus { |
| border-color: #61dafb; |
| box-shadow: 0 0 0 2px rgba(97, 218, 251, 0.3); |
| } |
| .stButton button { |
| background-color: #61dafb; |
| color: #1e1e1e; |
| border: none; |
| border-radius: 8px; |
| font-size: 1rem; |
| font-weight: 500; |
| padding: 0.75rem 1.5rem; |
| transition: all 0.3s ease; |
| width: 100%; |
| } |
| .stButton button:hover { |
| background-color: #4fa8d5; |
| transform: translateY(-2px); |
| box-shadow: 0 4px 8px rgba(97, 218, 251, 0.4); |
| } |
| .output-container { |
| background: #3a3a3a; |
| border-radius: 8px; |
| padding: 1.5rem; |
| margin-top: 2rem; |
| border: 1px solid #4a4a4a; |
| } |
| .code-block { |
| background-color: #2b2b2b; |
| color: #e0e0e0; |
| font-family: 'Fira Code', monospace; |
| font-size: 0.9rem; |
| border-radius: 8px; |
| padding: 1.5rem; |
| margin-top: 1rem; |
| overflow-x: auto; |
| } |
| .stAlert { |
| background-color: #4a4a4a; |
| color: #e0e0e0; |
| border-radius: 8px; |
| border: none; |
| padding: 1rem 1.5rem; |
| margin-bottom: 1rem; |
| } |
| .stSpinner { |
| color: #61dafb; |
| } |
| /* Custom scrollbar */ |
| ::-webkit-scrollbar { |
| width: 8px; |
| height: 8px; |
| } |
| ::-webkit-scrollbar-track { |
| background: #2d2d2d; |
| border-radius: 4px; |
| } |
| ::-webkit-scrollbar-thumb { |
| background: #4a4a4a; |
| border-radius: 4px; |
| } |
| ::-webkit-scrollbar-thumb:hover { |
| background: #5a5a5a; |
| } |
| </style> |
| """, unsafe_allow_html=True) |
|
|
| st.markdown('<div class="main-container">', unsafe_allow_html=True) |
| st.title("🧠 CodeCraft AI") |
| st.markdown('<p class="subtitle">Empowering Developers with Advanced AI-Driven Solutions</p>', unsafe_allow_html=True) |
|
|
| |
| languages = ["Python", "JavaScript", "Java", "C++", "Ruby", "Go", "Rust", "TypeScript", "PHP", "Swift"] |
| selected_language = st.selectbox("Select your preferred programming language:", languages) |
|
|
| prompt = st.text_area(f"What {selected_language} challenge can I assist you with today?", height=120) |
|
|
| col1, col2 = st.columns([3, 1]) |
| with col1: |
| generate_button = st.button("Generate Expert Code") |
| with col2: |
| complexity = st.select_slider("Code Complexity", options=["Low", "Medium", "High"]) |
|
|
| if generate_button: |
| if prompt.strip() == "": |
| st.error("Please enter a valid prompt.") |
| else: |
| with st.spinner(f"Crafting {complexity.lower()} complexity {selected_language} solution..."): |
| completed_text = generate_response(f"{complexity} complexity {selected_language} code for: {prompt}") |
| if "An error occurred" in completed_text: |
| st.error(completed_text) |
| else: |
| st.success(f"Expert-level {selected_language} code 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) |
| st.markdown('</div>', unsafe_allow_html=True) |
|
|
| |
| st.markdown( |
| f""" |
| <button onclick="navigator.clipboard.writeText(`{completed_text}`)" style="margin-top: 10px; background-color: #4a4a4a; color: #e0e0e0; border: none; padding: 5px 10px; border-radius: 5px; cursor: pointer;"> |
| Copy to Clipboard |
| </button> |
| """, |
| unsafe_allow_html=True |
| ) |
|
|
| st.markdown(""" |
| <div style='text-align: center; margin-top: 2rem; color: #bbb;'> |
| Engineered with 💡 by CodeCraft AI - Elevating Your Coding Experience |
| </div> |
| """, unsafe_allow_html=True) |
|
|
| st.markdown('</div>', unsafe_allow_html=True) |