Update app.py
Browse files
app.py
CHANGED
|
@@ -1,19 +1,20 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import google.generativeai as genai
|
|
|
|
| 3 |
|
| 4 |
-
# Configure the Gemini API with error handling
|
| 5 |
try:
|
| 6 |
genai.configure(api_key=st.secrets["GOOGLE_API_KEY"])
|
| 7 |
except Exception as e:
|
| 8 |
st.error(f"Failed to configure API: {e}")
|
| 9 |
st.stop()
|
| 10 |
|
| 11 |
-
# Create the model with system instructions
|
| 12 |
generation_config = {
|
| 13 |
-
"temperature": 0.
|
| 14 |
-
"top_p": 0.
|
| 15 |
-
"top_k":
|
| 16 |
-
"max_output_tokens":
|
| 17 |
}
|
| 18 |
|
| 19 |
model = genai.GenerativeModel(
|
|
@@ -28,7 +29,9 @@ def generate_response(user_input):
|
|
| 28 |
response = chat_session.send_message(user_input)
|
| 29 |
return response.text
|
| 30 |
except Exception as e:
|
| 31 |
-
|
|
|
|
|
|
|
| 32 |
|
| 33 |
# Streamlit UI setup
|
| 34 |
st.set_page_config(page_title="Sleek AI Code Assistant", page_icon="π", layout="wide")
|
|
@@ -124,6 +127,10 @@ st.markdown('<div class="main-container">', unsafe_allow_html=True)
|
|
| 124 |
st.title("π Sleek AI Code Assistant")
|
| 125 |
st.markdown('<p class="subtitle">Powered by Google Gemini</p>', unsafe_allow_html=True)
|
| 126 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
prompt = st.text_area("What code can I help you with today?", height=120)
|
| 128 |
|
| 129 |
if st.button("Generate Code"):
|
|
@@ -133,15 +140,25 @@ if st.button("Generate Code"):
|
|
| 133 |
with st.spinner("Generating code..."):
|
| 134 |
try:
|
| 135 |
completed_text = generate_response(prompt)
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
|
|
|
|
|
|
| 143 |
except Exception as e:
|
| 144 |
st.error(f"An error occurred: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
|
| 146 |
st.markdown("""
|
| 147 |
<div style='text-align: center; margin-top: 2rem; color: #4a5568;'>
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import google.generativeai as genai
|
| 3 |
+
import traceback
|
| 4 |
|
| 5 |
+
# Configure the Gemini API with advanced error handling
|
| 6 |
try:
|
| 7 |
genai.configure(api_key=st.secrets["GOOGLE_API_KEY"])
|
| 8 |
except Exception as e:
|
| 9 |
st.error(f"Failed to configure API: {e}")
|
| 10 |
st.stop()
|
| 11 |
|
| 12 |
+
# Create the model with system instructions and advanced configuration
|
| 13 |
generation_config = {
|
| 14 |
+
"temperature": 0.5, # Lower temperature for more deterministic responses
|
| 15 |
+
"top_p": 0.9,
|
| 16 |
+
"top_k": 50,
|
| 17 |
+
"max_output_tokens": 10240, # Increased max output tokens for longer responses
|
| 18 |
}
|
| 19 |
|
| 20 |
model = genai.GenerativeModel(
|
|
|
|
| 29 |
response = chat_session.send_message(user_input)
|
| 30 |
return response.text
|
| 31 |
except Exception as e:
|
| 32 |
+
st.error(f"An error occurred while generating response: {e}")
|
| 33 |
+
st.error(traceback.format_exc())
|
| 34 |
+
return None
|
| 35 |
|
| 36 |
# Streamlit UI setup
|
| 37 |
st.set_page_config(page_title="Sleek AI Code Assistant", page_icon="π", layout="wide")
|
|
|
|
| 127 |
st.title("π Sleek AI Code Assistant")
|
| 128 |
st.markdown('<p class="subtitle">Powered by Google Gemini</p>', unsafe_allow_html=True)
|
| 129 |
|
| 130 |
+
# Session state to maintain conversation history
|
| 131 |
+
if 'history' not in st.session_state:
|
| 132 |
+
st.session_state.history = []
|
| 133 |
+
|
| 134 |
prompt = st.text_area("What code can I help you with today?", height=120)
|
| 135 |
|
| 136 |
if st.button("Generate Code"):
|
|
|
|
| 140 |
with st.spinner("Generating code..."):
|
| 141 |
try:
|
| 142 |
completed_text = generate_response(prompt)
|
| 143 |
+
if completed_text:
|
| 144 |
+
st.success("Code generated successfully!")
|
| 145 |
+
st.session_state.history.append({"user": prompt, "assistant": completed_text})
|
| 146 |
+
|
| 147 |
+
st.markdown('<div class="output-container">', unsafe_allow_html=True)
|
| 148 |
+
st.markdown('<div class="code-block">', unsafe_allow_html=True)
|
| 149 |
+
st.code(completed_text)
|
| 150 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
| 151 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
| 152 |
except Exception as e:
|
| 153 |
st.error(f"An error occurred: {e}")
|
| 154 |
+
st.error(traceback.format_exc())
|
| 155 |
+
|
| 156 |
+
# Display conversation history
|
| 157 |
+
st.markdown("## Conversation History")
|
| 158 |
+
for entry in st.session_state.history:
|
| 159 |
+
st.markdown(f"**User:** {entry['user']}")
|
| 160 |
+
st.markdown(f"**Assistant:**")
|
| 161 |
+
st.code(entry['assistant'])
|
| 162 |
|
| 163 |
st.markdown("""
|
| 164 |
<div style='text-align: center; margin-top: 2rem; color: #4a5568;'>
|