import streamlit as st
import google.generativeai as genai
from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters import HtmlFormatter
import html
# 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}"
def create_code_block(code, language):
lexer = get_lexer_by_name(language, stripall=True)
formatter = HtmlFormatter(style="monokai", linenos=True, cssclass="source")
highlighted_code = highlight(code, lexer, formatter)
css = formatter.get_style_defs('.source')
return highlighted_code, css
# Streamlit UI setup
st.set_page_config(page_title="Advanced AI Code Assistant", page_icon="💻", layout="wide")
st.markdown("""
""", unsafe_allow_html=True)
st.markdown('
', unsafe_allow_html=True)
st.title("💻 Advanced AI Code Assistant")
st.markdown('
Powered by Google Gemini - Expert-level coding solutions
', unsafe_allow_html=True)
prompt = st.text_area("What advanced coding challenge can I assist you with today?", height=120)
if st.button("Generate Expert Code"):
if prompt.strip() == "":
st.error("Please enter a valid prompt.")
else:
with st.spinner("Generating advanced code solution..."):
completed_text = generate_response(prompt)
if "An error occurred" in completed_text:
st.error(completed_text)
else:
st.success("Expert-level code generated successfully!")
# Attempt to determine the language (this is a simple guess, you might want to improve this)
language = "python" if "def " in completed_text or "import " in completed_text else "javascript"
highlighted_code, css = create_code_block(completed_text, language)
st.markdown(f'', unsafe_allow_html=True)
st.markdown('
', unsafe_allow_html=True)
st.markdown('
', unsafe_allow_html=True)
st.markdown(highlighted_code, unsafe_allow_html=True)
st.markdown('
', unsafe_allow_html=True)
st.markdown('
', unsafe_allow_html=True)
st.markdown("""
Crafted with ❤️ by Your Advanced AI Code Assistant
""", unsafe_allow_html=True)
st.markdown('
', unsafe_allow_html=True)