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(""" """, 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")