Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,6 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import google.generativeai as genai
|
| 3 |
|
| 4 |
-
# -------------------
|
| 5 |
-
# API Key Setup
|
| 6 |
-
# -------------------
|
| 7 |
-
gemini_api_key = st.secrets.get("GEN_API_KEY", "")
|
| 8 |
-
|
| 9 |
# -------------------
|
| 10 |
# Page Configuration
|
| 11 |
# -------------------
|
|
@@ -13,15 +8,73 @@ st.set_page_config(page_title="Your AI Buddy", layout="wide")
|
|
| 13 |
st.title("💡 Need answers? Just type below!")
|
| 14 |
|
| 15 |
# -------------------
|
| 16 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
# -------------------
|
|
|
|
|
|
|
|
|
|
| 18 |
if not gemini_api_key:
|
| 19 |
st.error("⚠️ Please set your 'GEN_API_KEY' in Streamlit secrets.")
|
| 20 |
st.stop()
|
| 21 |
|
| 22 |
genai.configure(api_key=gemini_api_key)
|
| 23 |
|
| 24 |
-
#
|
|
|
|
|
|
|
| 25 |
available_models = [
|
| 26 |
m.name for m in genai.list_models()
|
| 27 |
if "generateContent" in m.supported_generation_methods
|
|
@@ -31,13 +84,12 @@ if not available_models:
|
|
| 31 |
st.error("⚠️ No Gemini models available for your API key.")
|
| 32 |
st.stop()
|
| 33 |
|
| 34 |
-
# Reset session if old model is invalid
|
| 35 |
if "model" in st.session_state and st.session_state["model"] not in available_models:
|
| 36 |
del st.session_state["model"]
|
| 37 |
|
| 38 |
model = st.sidebar.selectbox("Model", available_models, index=0)
|
| 39 |
|
| 40 |
-
# Initialize Gemini chat
|
| 41 |
if "gemini_chat" not in st.session_state or st.session_state.get("model") != model:
|
| 42 |
st.session_state.model = model
|
| 43 |
try:
|
|
@@ -72,28 +124,30 @@ if st.sidebar.button("Reset Conversation"):
|
|
| 72 |
# Display Chat Messages
|
| 73 |
# -------------------
|
| 74 |
for msg in st.session_state.messages:
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
# -------------------
|
| 79 |
# User Input
|
| 80 |
# -------------------
|
| 81 |
if user_input := st.chat_input("Type your message..."):
|
| 82 |
-
#
|
| 83 |
-
st.chat_message("user").markdown(user_input)
|
| 84 |
st.session_state.messages.append({"role": "user", "content": user_input})
|
|
|
|
| 85 |
|
| 86 |
try:
|
| 87 |
with st.spinner("Gemini is thinking..."):
|
| 88 |
-
# Prepend system prompt for context
|
| 89 |
full_input = f"{system_prompt}\n\nUser: {user_input}"
|
| 90 |
resp = st.session_state.gemini_chat.send_message(full_input)
|
| 91 |
bot_text = resp.text
|
| 92 |
except Exception as e:
|
| 93 |
bot_text = f"⚠️ Gemini could not respond right now. Please try again. ({e})"
|
| 94 |
|
| 95 |
-
with st.chat_message("assistant"):
|
| 96 |
-
st.markdown(bot_text)
|
| 97 |
-
|
| 98 |
st.session_state.messages.append({"role": "assistant", "content": bot_text})
|
| 99 |
st.rerun()
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import google.generativeai as genai
|
| 3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
# -------------------
|
| 5 |
# Page Configuration
|
| 6 |
# -------------------
|
|
|
|
| 8 |
st.title("💡 Need answers? Just type below!")
|
| 9 |
|
| 10 |
# -------------------
|
| 11 |
+
# CSS Styling for Chat Bubbles
|
| 12 |
+
# -------------------
|
| 13 |
+
st.markdown("""
|
| 14 |
+
<style>
|
| 15 |
+
/* General chat bubble styling */
|
| 16 |
+
.stChatMessage > div {
|
| 17 |
+
border-radius: 15px;
|
| 18 |
+
padding: 10px 14px;
|
| 19 |
+
margin: 6px 0;
|
| 20 |
+
max-width: 80%;
|
| 21 |
+
font-size: 15px;
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
/* User messages (right side) */
|
| 25 |
+
.stChatMessage:has(.user-message-marker) > div {
|
| 26 |
+
flex-direction: row-reverse;
|
| 27 |
+
text-align: right;
|
| 28 |
+
background-color: #d1e9ff; /* Light blue bubble */
|
| 29 |
+
color: #0a2f5c; /* Dark blue text */
|
| 30 |
+
border: 1px solid #a3cfff;
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
/* Assistant messages (left side) */
|
| 34 |
+
.stChatMessage:has(.assistant-message-marker) > div {
|
| 35 |
+
flex-direction: row;
|
| 36 |
+
text-align: left;
|
| 37 |
+
background-color: #f1f1f1; /* Light gray bubble */
|
| 38 |
+
color: #1a1a1a;
|
| 39 |
+
border: 1px solid #ddd;
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
/* Ensure markdown text follows bubble alignment */
|
| 43 |
+
.stChatMessage:has(.user-message-marker) .stMarkdown {
|
| 44 |
+
text-align: right;
|
| 45 |
+
}
|
| 46 |
+
.stChatMessage:has(.assistant-message-marker) .stMarkdown {
|
| 47 |
+
text-align: left;
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
/* Avatars */
|
| 51 |
+
.stChatMessage [data-testid="stChatAvatar"] {
|
| 52 |
+
border-radius: 50%;
|
| 53 |
+
width: 32px;
|
| 54 |
+
height: 32px;
|
| 55 |
+
font-size: 18px;
|
| 56 |
+
background-color: #ffffff;
|
| 57 |
+
display: flex;
|
| 58 |
+
align-items: center;
|
| 59 |
+
justify-content: center;
|
| 60 |
+
box-shadow: 0 1px 3px rgba(0,0,0,0.2);
|
| 61 |
+
}
|
| 62 |
+
</style>
|
| 63 |
+
""", unsafe_allow_html=True)
|
| 64 |
+
|
| 65 |
# -------------------
|
| 66 |
+
# API Key Setup
|
| 67 |
+
# -------------------
|
| 68 |
+
gemini_api_key = st.secrets.get("GEN_API_KEY", "")
|
| 69 |
if not gemini_api_key:
|
| 70 |
st.error("⚠️ Please set your 'GEN_API_KEY' in Streamlit secrets.")
|
| 71 |
st.stop()
|
| 72 |
|
| 73 |
genai.configure(api_key=gemini_api_key)
|
| 74 |
|
| 75 |
+
# -------------------
|
| 76 |
+
# Gemini Setup
|
| 77 |
+
# -------------------
|
| 78 |
available_models = [
|
| 79 |
m.name for m in genai.list_models()
|
| 80 |
if "generateContent" in m.supported_generation_methods
|
|
|
|
| 84 |
st.error("⚠️ No Gemini models available for your API key.")
|
| 85 |
st.stop()
|
| 86 |
|
|
|
|
| 87 |
if "model" in st.session_state and st.session_state["model"] not in available_models:
|
| 88 |
del st.session_state["model"]
|
| 89 |
|
| 90 |
model = st.sidebar.selectbox("Model", available_models, index=0)
|
| 91 |
|
| 92 |
+
# Initialize Gemini chat
|
| 93 |
if "gemini_chat" not in st.session_state or st.session_state.get("model") != model:
|
| 94 |
st.session_state.model = model
|
| 95 |
try:
|
|
|
|
| 124 |
# Display Chat Messages
|
| 125 |
# -------------------
|
| 126 |
for msg in st.session_state.messages:
|
| 127 |
+
role = msg["role"]
|
| 128 |
+
avatar = "🙂" if role == "user" else "🤖"
|
| 129 |
+
with st.chat_message(role, avatar=avatar):
|
| 130 |
+
# Add hidden marker for CSS styling
|
| 131 |
+
st.markdown(
|
| 132 |
+
f"<span class='{role}-message-marker'></span>{msg['content']}",
|
| 133 |
+
unsafe_allow_html=True
|
| 134 |
+
)
|
| 135 |
|
| 136 |
# -------------------
|
| 137 |
# User Input
|
| 138 |
# -------------------
|
| 139 |
if user_input := st.chat_input("Type your message..."):
|
| 140 |
+
# Save and display user input
|
|
|
|
| 141 |
st.session_state.messages.append({"role": "user", "content": user_input})
|
| 142 |
+
st.rerun()
|
| 143 |
|
| 144 |
try:
|
| 145 |
with st.spinner("Gemini is thinking..."):
|
|
|
|
| 146 |
full_input = f"{system_prompt}\n\nUser: {user_input}"
|
| 147 |
resp = st.session_state.gemini_chat.send_message(full_input)
|
| 148 |
bot_text = resp.text
|
| 149 |
except Exception as e:
|
| 150 |
bot_text = f"⚠️ Gemini could not respond right now. Please try again. ({e})"
|
| 151 |
|
|
|
|
|
|
|
|
|
|
| 152 |
st.session_state.messages.append({"role": "assistant", "content": bot_text})
|
| 153 |
st.rerun()
|