Update app.py
Browse files
app.py
CHANGED
|
@@ -1,66 +1,18 @@
|
|
| 1 |
import streamlit as st
|
|
|
|
| 2 |
import google.generativeai as genai
|
| 3 |
|
| 4 |
# -------------------
|
| 5 |
# Page Configuration
|
| 6 |
# -------------------
|
| 7 |
st.set_page_config(page_title="Your AI Buddy", layout="wide")
|
| 8 |
-
st.title("💡 Need answers? Just type below!")
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 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
|
|
@@ -73,7 +25,7 @@ if not gemini_api_key:
|
|
| 73 |
genai.configure(api_key=gemini_api_key)
|
| 74 |
|
| 75 |
# -------------------
|
| 76 |
-
#
|
| 77 |
# -------------------
|
| 78 |
available_models = [
|
| 79 |
m.name for m in genai.list_models()
|
|
@@ -87,62 +39,52 @@ if not available_models:
|
|
| 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:
|
| 96 |
-
gemini_model = genai.GenerativeModel(model)
|
| 97 |
-
st.session_state.gemini_chat = gemini_model.start_chat(history=[])
|
| 98 |
-
except Exception as e:
|
| 99 |
-
st.error(f"⚠️ Could not initialize Gemini model: {e}")
|
| 100 |
-
st.stop()
|
| 101 |
-
|
| 102 |
-
# -------------------
|
| 103 |
-
# System Prompt
|
| 104 |
-
# -------------------
|
| 105 |
-
system_prompt = st.sidebar.text_area(
|
| 106 |
-
"System Prompt",
|
| 107 |
-
"You are a helpful AI assistant. Provide concise and accurate answers."
|
| 108 |
-
)
|
| 109 |
|
| 110 |
# -------------------
|
| 111 |
-
# Chat
|
| 112 |
# -------------------
|
| 113 |
-
if "
|
|
|
|
|
|
|
|
|
|
| 114 |
st.session_state.messages = []
|
| 115 |
|
| 116 |
# Reset conversation button
|
| 117 |
-
if st.sidebar.button("Reset Conversation"):
|
| 118 |
st.session_state.messages = []
|
| 119 |
gemini_model = genai.GenerativeModel(model)
|
| 120 |
st.session_state.gemini_chat = gemini_model.start_chat(history=[])
|
| 121 |
st.rerun()
|
| 122 |
|
| 123 |
# -------------------
|
| 124 |
-
#
|
| 125 |
# -------------------
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
|
| 136 |
# -------------------
|
| 137 |
# User Input
|
| 138 |
# -------------------
|
| 139 |
-
|
| 140 |
-
|
|
|
|
| 141 |
st.session_state.messages.append({"role": "user", "content": user_input})
|
| 142 |
-
st.rerun()
|
| 143 |
|
| 144 |
try:
|
| 145 |
-
with st.spinner("
|
| 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
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from streamlit_chat import message
|
| 3 |
import google.generativeai as genai
|
| 4 |
|
| 5 |
# -------------------
|
| 6 |
# Page Configuration
|
| 7 |
# -------------------
|
| 8 |
st.set_page_config(page_title="Your AI Buddy", layout="wide")
|
|
|
|
| 9 |
|
| 10 |
+
st.title("💬 Your AI Buddy")
|
| 11 |
+
st.markdown("Ask me anything! 📘🤖\n\n"
|
| 12 |
+
"💡 *Examples:*\n"
|
| 13 |
+
"- Summarize a news article\n"
|
| 14 |
+
"- Explain a math problem\n"
|
| 15 |
+
"- Give me study tips\n")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
# -------------------
|
| 18 |
# API Key Setup
|
|
|
|
| 25 |
genai.configure(api_key=gemini_api_key)
|
| 26 |
|
| 27 |
# -------------------
|
| 28 |
+
# Model Selection
|
| 29 |
# -------------------
|
| 30 |
available_models = [
|
| 31 |
m.name for m in genai.list_models()
|
|
|
|
| 39 |
if "model" in st.session_state and st.session_state["model"] not in available_models:
|
| 40 |
del st.session_state["model"]
|
| 41 |
|
| 42 |
+
model = st.sidebar.selectbox("Choose a Gemini Model", available_models, index=0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
# -------------------
|
| 45 |
+
# Initialize Chat Session
|
| 46 |
# -------------------
|
| 47 |
+
if "gemini_chat" not in st.session_state or st.session_state.get("model") != model:
|
| 48 |
+
st.session_state.model = model
|
| 49 |
+
gemini_model = genai.GenerativeModel(model)
|
| 50 |
+
st.session_state.gemini_chat = gemini_model.start_chat(history=[])
|
| 51 |
st.session_state.messages = []
|
| 52 |
|
| 53 |
# Reset conversation button
|
| 54 |
+
if st.sidebar.button("🔄 Reset Conversation"):
|
| 55 |
st.session_state.messages = []
|
| 56 |
gemini_model = genai.GenerativeModel(model)
|
| 57 |
st.session_state.gemini_chat = gemini_model.start_chat(history=[])
|
| 58 |
st.rerun()
|
| 59 |
|
| 60 |
# -------------------
|
| 61 |
+
# System Prompt
|
| 62 |
# -------------------
|
| 63 |
+
system_prompt = st.sidebar.text_area(
|
| 64 |
+
"System Prompt (AI Behavior)",
|
| 65 |
+
"You are a helpful AI assistant. Provide concise and accurate answers."
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
# -------------------
|
| 69 |
+
# Chat History Display
|
| 70 |
+
# -------------------
|
| 71 |
+
if st.session_state.messages:
|
| 72 |
+
for i, msg in enumerate(st.session_state.messages):
|
| 73 |
+
if msg["role"] == "user":
|
| 74 |
+
message(msg["content"], is_user=True, key=f"user_{i}")
|
| 75 |
+
else:
|
| 76 |
+
message(msg["content"], key=f"bot_{i}")
|
| 77 |
|
| 78 |
# -------------------
|
| 79 |
# User Input
|
| 80 |
# -------------------
|
| 81 |
+
user_input = st.chat_input("Type your question here...")
|
| 82 |
+
if user_input:
|
| 83 |
+
# Save user message
|
| 84 |
st.session_state.messages.append({"role": "user", "content": user_input})
|
|
|
|
| 85 |
|
| 86 |
try:
|
| 87 |
+
with st.spinner("🤖 Thinking..."):
|
| 88 |
full_input = f"{system_prompt}\n\nUser: {user_input}"
|
| 89 |
resp = st.session_state.gemini_chat.send_message(full_input)
|
| 90 |
bot_text = resp.text
|