Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import os
|
| 2 |
import keyfile
|
| 3 |
import warnings
|
|
@@ -99,3 +100,126 @@ st.markdown("""
|
|
| 99 |
}
|
| 100 |
</style>
|
| 101 |
""", unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
'''
|
| 2 |
import os
|
| 3 |
import keyfile
|
| 4 |
import warnings
|
|
|
|
| 100 |
}
|
| 101 |
</style>
|
| 102 |
""", unsafe_allow_html=True)
|
| 103 |
+
'''
|
| 104 |
+
import os
|
| 105 |
+
import keyfile
|
| 106 |
+
import warnings
|
| 107 |
+
import streamlit as st
|
| 108 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 109 |
+
from langchain.schema import HumanMessage, SystemMessage, AIMessage
|
| 110 |
+
|
| 111 |
+
# Ignore warnings
|
| 112 |
+
warnings.filterwarnings("ignore")
|
| 113 |
+
|
| 114 |
+
# Streamlit settings
|
| 115 |
+
st.set_page_config(page_title="πΏ ChitChat π§ββοΈ", page_icon="π§ββοΈ", layout="wide")
|
| 116 |
+
|
| 117 |
+
# Header
|
| 118 |
+
st.markdown("<h1 style='text-align: center; color: #4B0082;'>Welcome to ChitChat πΏβ¨</h1>", unsafe_allow_html=True)
|
| 119 |
+
st.markdown("<h3 style='color: #003366;'>How can I assist with your ailments or worries today? π§ͺπ«</h3>", unsafe_allow_html=True)
|
| 120 |
+
|
| 121 |
+
# Initialize session state for messages
|
| 122 |
+
if "sessionMessages" not in st.session_state:
|
| 123 |
+
st.session_state.sessionMessages = [
|
| 124 |
+
SystemMessage(content="You are a medieval magical healer known for your peculiar sarcasm.")
|
| 125 |
+
]
|
| 126 |
+
|
| 127 |
+
# Set Google API key
|
| 128 |
+
os.environ["GOOGLE_API_KEY"] = keyfile.GOOGLEKEY
|
| 129 |
+
|
| 130 |
+
# Initialize the model
|
| 131 |
+
llm = ChatGoogleGenerativeAI(
|
| 132 |
+
model="gemini-1.5-pro",
|
| 133 |
+
temperature=0.7,
|
| 134 |
+
convert_system_message_to_human=True
|
| 135 |
+
)
|
| 136 |
+
|
| 137 |
+
# Function to create chat bubbles
|
| 138 |
+
def chat_bubble(message, is_user=True):
|
| 139 |
+
align = 'right' if is_user else 'left'
|
| 140 |
+
color = '#E1F5FE' if is_user else '#FFEBEE'
|
| 141 |
+
bubble_style = f"""
|
| 142 |
+
<div style="text-align: {align}; padding: 10px;">
|
| 143 |
+
<span style="display: inline-block; padding: 10px; background-color: {color}; color: black;
|
| 144 |
+
border-radius: 15px; max-width: 70%; word-wrap: break-word;">
|
| 145 |
+
{message}
|
| 146 |
+
</span>
|
| 147 |
+
</div>
|
| 148 |
+
"""
|
| 149 |
+
st.markdown(bubble_style, unsafe_allow_html=True)
|
| 150 |
+
|
| 151 |
+
# Function to load answer from the model
|
| 152 |
+
def load_answer(question):
|
| 153 |
+
# Add the user question to sessionMessages
|
| 154 |
+
st.session_state.sessionMessages.append(HumanMessage(content=question))
|
| 155 |
+
assistant_answer = llm.invoke(st.session_state.sessionMessages)
|
| 156 |
+
|
| 157 |
+
# Ensure the answer is clean and formatted correctly
|
| 158 |
+
if isinstance(assistant_answer, AIMessage):
|
| 159 |
+
st.session_state.sessionMessages.append(assistant_answer)
|
| 160 |
+
return assistant_answer.content.strip() # Strip any unnecessary whitespace or newlines
|
| 161 |
+
else:
|
| 162 |
+
st.session_state.sessionMessages.append(AIMessage(content=assistant_answer))
|
| 163 |
+
return assistant_answer.strip() # Also strip here for consistency
|
| 164 |
+
|
| 165 |
+
# Display the chat history
|
| 166 |
+
for message in st.session_state.sessionMessages:
|
| 167 |
+
if isinstance(message, HumanMessage):
|
| 168 |
+
chat_bubble(message.content, is_user=True)
|
| 169 |
+
elif isinstance(message, AIMessage):
|
| 170 |
+
chat_bubble(message.content, is_user=False)
|
| 171 |
+
|
| 172 |
+
# Sidebar for additional options
|
| 173 |
+
st.sidebar.header("Additional Options")
|
| 174 |
+
mood = st.sidebar.selectbox("Select your mood:", ["Neutral", "Happy", "Concerned", "Curious"])
|
| 175 |
+
predefined_responses = st.sidebar.radio("Choose a predefined response:",
|
| 176 |
+
["Tell me a joke", "Give me advice on health", "Suggest a potion", "General inquiry"])
|
| 177 |
+
|
| 178 |
+
# Input area for new prompt
|
| 179 |
+
user_input = st.text_input("You: ", key="input", placeholder="Type your question here...")
|
| 180 |
+
|
| 181 |
+
# Button for submission
|
| 182 |
+
if st.button("π Get a Magical Answer π"):
|
| 183 |
+
if user_input:
|
| 184 |
+
chat_bubble(user_input, is_user=True) # Display user input
|
| 185 |
+
response = load_answer(user_input) # Get response
|
| 186 |
+
chat_bubble(response, is_user=False) # Display AI response
|
| 187 |
+
|
| 188 |
+
# Handle predefined responses
|
| 189 |
+
if predefined_responses == "Tell me a joke":
|
| 190 |
+
joke = "Why did the scarecrow win an award? Because he was outstanding in his field!"
|
| 191 |
+
chat_bubble(joke, is_user=False)
|
| 192 |
+
elif predefined_responses == "Give me advice on health":
|
| 193 |
+
health_advice = "Make sure to drink plenty of water and eat a balanced diet rich in fruits and vegetables."
|
| 194 |
+
chat_bubble(health_advice, is_user=False)
|
| 195 |
+
elif predefined_responses == "Suggest a potion":
|
| 196 |
+
potion_suggestion = "How about a calming lavender potion? It helps with relaxation and sleep!"
|
| 197 |
+
chat_bubble(potion_suggestion, is_user=False)
|
| 198 |
+
|
| 199 |
+
# Background styling
|
| 200 |
+
st.markdown("""
|
| 201 |
+
<style>
|
| 202 |
+
.stApp {
|
| 203 |
+
background: linear-gradient(to right, #FFEFBA, #FFFFFF);
|
| 204 |
+
color: #4B0082;
|
| 205 |
+
font-family: Arial, sans-serif;
|
| 206 |
+
}
|
| 207 |
+
input[type="text"] {
|
| 208 |
+
padding: 10px;
|
| 209 |
+
border: 2px solid #4B0082;
|
| 210 |
+
border-radius: 15px;
|
| 211 |
+
outline: none;
|
| 212 |
+
width: 100%;
|
| 213 |
+
}
|
| 214 |
+
button {
|
| 215 |
+
background-color: #4B0082;
|
| 216 |
+
color: white;
|
| 217 |
+
border-radius: 15px;
|
| 218 |
+
margin-top: 10px;
|
| 219 |
+
}
|
| 220 |
+
.sidebar .sidebar-content {
|
| 221 |
+
background-color: #f9f9f9;
|
| 222 |
+
border-radius: 10px;
|
| 223 |
+
}
|
| 224 |
+
</style>
|
| 225 |
+
""", unsafe_allow_html=True)
|