advance feature add
Browse files
app.py
CHANGED
@@ -1,106 +1,3 @@
|
|
1 |
-
'''
|
2 |
-
import os
|
3 |
-
import keyfile
|
4 |
-
import warnings
|
5 |
-
import streamlit as st
|
6 |
-
from langchain_google_genai import ChatGoogleGenerativeAI
|
7 |
-
from langchain.schema import HumanMessage, SystemMessage, AIMessage
|
8 |
-
|
9 |
-
# Ignore warnings
|
10 |
-
warnings.filterwarnings("ignore")
|
11 |
-
|
12 |
-
# Streamlit settings
|
13 |
-
st.set_page_config(page_title="πΏ ChitChat π§ββοΈ", page_icon="π§ββοΈ", layout="wide")
|
14 |
-
|
15 |
-
# Header
|
16 |
-
st.markdown("<h1 style='text-align: center; color: #4B0082;'>Welcome to ChitChat πΏβ¨</h1>", unsafe_allow_html=True)
|
17 |
-
st.markdown("<h3 style='color: #003366;'>How can I assist with your ailments or worries today? π§ͺπ«</h3>", unsafe_allow_html=True)
|
18 |
-
|
19 |
-
# Initialize session state for messages
|
20 |
-
if "sessionMessages" not in st.session_state:
|
21 |
-
st.session_state.sessionMessages = [
|
22 |
-
SystemMessage(content="You are a medieval magical healer known for your peculiar sarcasm.")
|
23 |
-
]
|
24 |
-
|
25 |
-
# Set Google API key
|
26 |
-
os.environ["GOOGLE_API_KEY"] = keyfile.GOOGLEKEY
|
27 |
-
|
28 |
-
# Initialize the model
|
29 |
-
llm = ChatGoogleGenerativeAI(
|
30 |
-
model="gemini-1.5-pro",
|
31 |
-
temperature=0.7,
|
32 |
-
convert_system_message_to_human=True
|
33 |
-
)
|
34 |
-
|
35 |
-
# Function to create chat bubbles
|
36 |
-
def chat_bubble(message, is_user=True):
|
37 |
-
align = 'right' if is_user else 'left'
|
38 |
-
color = '#E1F5FE' if is_user else '#FFEBEE'
|
39 |
-
bubble_style = f"""
|
40 |
-
<div style="text-align: {align}; padding: 10px;">
|
41 |
-
<span style="display: inline-block; padding: 10px; background-color: {color}; color: black;
|
42 |
-
border-radius: 15px; max-width: 70%; word-wrap: break-word;">
|
43 |
-
{message}
|
44 |
-
</span>
|
45 |
-
</div>
|
46 |
-
"""
|
47 |
-
st.markdown(bubble_style, unsafe_allow_html=True)
|
48 |
-
|
49 |
-
# Function to load answer from the model
|
50 |
-
def load_answer(question):
|
51 |
-
# Add the user question to sessionMessages
|
52 |
-
st.session_state.sessionMessages.append(HumanMessage(content=question))
|
53 |
-
assistant_answer = llm.invoke(st.session_state.sessionMessages)
|
54 |
-
|
55 |
-
# Ensure the answer is clean and formatted correctly
|
56 |
-
if isinstance(assistant_answer, AIMessage):
|
57 |
-
st.session_state.sessionMessages.append(assistant_answer)
|
58 |
-
return assistant_answer.content.strip() # Strip any unnecessary whitespace or newlines
|
59 |
-
else:
|
60 |
-
st.session_state.sessionMessages.append(AIMessage(content=assistant_answer))
|
61 |
-
return assistant_answer.strip() # Also strip here for consistency
|
62 |
-
|
63 |
-
# Display the chat history
|
64 |
-
for message in st.session_state.sessionMessages:
|
65 |
-
if isinstance(message, HumanMessage):
|
66 |
-
chat_bubble(message.content, is_user=True)
|
67 |
-
elif isinstance(message, AIMessage):
|
68 |
-
chat_bubble(message.content, is_user=False)
|
69 |
-
|
70 |
-
# Input area for new prompt
|
71 |
-
user_input = st.text_input("You: ", key="input", placeholder="Type your question here...")
|
72 |
-
|
73 |
-
# Button for submission
|
74 |
-
if st.button("π Get a Magical Answer π"):
|
75 |
-
if user_input:
|
76 |
-
chat_bubble(user_input, is_user=True) # Display user input
|
77 |
-
response = load_answer(user_input) # Get response
|
78 |
-
chat_bubble(response, is_user=False) # Display AI response
|
79 |
-
|
80 |
-
# Background styling
|
81 |
-
st.markdown("""
|
82 |
-
<style>
|
83 |
-
.stApp {
|
84 |
-
background: linear-gradient(to right, #FFEFBA, #FFFFFF);
|
85 |
-
color: #4B0082;
|
86 |
-
font-family: Arial, sans-serif;
|
87 |
-
}
|
88 |
-
input[type="text"] {
|
89 |
-
padding: 10px;
|
90 |
-
border: 2px solid #4B0082;
|
91 |
-
border-radius: 15px;
|
92 |
-
outline: none;
|
93 |
-
width: 100%;
|
94 |
-
}
|
95 |
-
button {
|
96 |
-
background-color: #4B0082;
|
97 |
-
color: white;
|
98 |
-
border-radius: 15px;
|
99 |
-
margin-top: 10px;
|
100 |
-
}
|
101 |
-
</style>
|
102 |
-
""", unsafe_allow_html=True)
|
103 |
-
'''
|
104 |
import os
|
105 |
import keyfile
|
106 |
import warnings
|
@@ -171,7 +68,16 @@ for message in st.session_state.sessionMessages:
|
|
171 |
|
172 |
# Sidebar for additional options
|
173 |
st.sidebar.header("Additional Options")
|
174 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
import keyfile
|
3 |
import warnings
|
|
|
68 |
|
69 |
# Sidebar for additional options
|
70 |
st.sidebar.header("Additional Options")
|
71 |
+
|
72 |
+
# Mood selection with emojis
|
73 |
+
mood_options = {
|
74 |
+
"π": "Happy",
|
75 |
+
"π": "Neutral",
|
76 |
+
"π": "Concerned",
|
77 |
+
"π€": "Curious"
|
78 |
+
}
|
79 |
+
mood = st.sidebar.selectbox("Select your mood:", list(mood_options.keys()))
|
80 |
+
|
81 |
predefined_responses = st.sidebar.radio("Choose a predefined response:",
|
82 |
["Tell me a joke", "Give me advice on health", "Suggest a potion", "General inquiry"])
|
83 |
|