| 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 |
|
|
| |
| 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 load_lottieurl(url: str): |
| r = requests.get(url) |
| if r.status_code != 200: |
| return None |
| return r.json() |
|
|
| |
| lottie_url = "https://assets7.lottiefiles.com/packages/lf20_s1jf29.json" |
| lottie_json = load_lottieurl(lottie_url) |
|
|
| |
| st.set_page_config(page_title="AI Code Assistant", page_icon="🤖", layout="wide") |
|
|
| |
| 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) |
|
|
| |
| st.title("🤖 AI Code Assistant") |
| st.markdown("#### Powered by Google Gemini") |
|
|
| |
| st.sidebar.header("Settings") |
| language = st.sidebar.selectbox("Select programming language", ["Python", "JavaScript", "Java", "C++", "Ruby"]) |
|
|
| |
| 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!") |
| |
| |
| 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") |
|
|
| |
| st.header("Additional Tools") |
|
|
| |
| 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.") |
|
|
| |
| 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.") |
|
|
| |
| st.markdown("---") |
| st.markdown("Created with ❤️ by Your Name") |