adding some fancy UI
Browse files
app.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import os
|
| 2 |
import keyfile
|
| 3 |
import warnings
|
|
@@ -57,3 +58,108 @@ if submit and user_input:
|
|
| 57 |
response = load_answer(user_input)
|
| 58 |
st.subheader("Answer:")
|
| 59 |
st.write(response)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
import os
|
| 3 |
import keyfile
|
| 4 |
import warnings
|
|
|
|
| 58 |
response = load_answer(user_input)
|
| 59 |
st.subheader("Answer:")
|
| 60 |
st.write(response)
|
| 61 |
+
"""
|
| 62 |
+
|
| 63 |
+
import os
|
| 64 |
+
import keyfile
|
| 65 |
+
import warnings
|
| 66 |
+
import streamlit as st
|
| 67 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 68 |
+
from langchain.schema import HumanMessage, SystemMessage, AIMessage
|
| 69 |
+
from streamlit_extras.add_vertical_space import add_vertical_space
|
| 70 |
+
|
| 71 |
+
# Ignore warnings
|
| 72 |
+
warnings.filterwarnings("ignore")
|
| 73 |
+
|
| 74 |
+
# Streamlit settings
|
| 75 |
+
st.set_page_config(page_title="πΏ Magical Healer", page_icon="π§ββοΈ", layout="centered")
|
| 76 |
+
st.markdown("<h1 style='text-align: center; color: #4B0082;'>Welcome to the Magical Healer π§ββοΈπΏ</h1>", unsafe_allow_html=True)
|
| 77 |
+
st.write("### How can I assist with your ailments or worries? π§ͺβ¨")
|
| 78 |
+
|
| 79 |
+
# Add some vertical space
|
| 80 |
+
add_vertical_space(2)
|
| 81 |
+
|
| 82 |
+
# Initialize session state for messages with an introductory message
|
| 83 |
+
if "sessionMessages" not in st.session_state:
|
| 84 |
+
st.session_state["sessionMessages"] = [
|
| 85 |
+
SystemMessage(content="You are a medieval magical healer known for your peculiar sarcasm.")
|
| 86 |
+
]
|
| 87 |
+
|
| 88 |
+
# Set Google API key
|
| 89 |
+
os.environ["GOOGLE_API_KEY"] = keyfile.GOOGLEKEY
|
| 90 |
+
|
| 91 |
+
# Initialize the model
|
| 92 |
+
llm = ChatGoogleGenerativeAI(
|
| 93 |
+
model="gemini-1.5-pro",
|
| 94 |
+
temperature=0.7,
|
| 95 |
+
convert_system_message_to_human=True
|
| 96 |
+
)
|
| 97 |
+
|
| 98 |
+
# Define a function to create chat bubbles
|
| 99 |
+
def chat_bubble(message, is_user=True):
|
| 100 |
+
align = 'right' if is_user else 'left'
|
| 101 |
+
color = '#ADD8E6' if is_user else '#E6E6FA'
|
| 102 |
+
border_radius = '25px' if is_user else '25px'
|
| 103 |
+
st.markdown(f"""
|
| 104 |
+
<div style="text-align: {align}; padding: 10px;">
|
| 105 |
+
<span style="display: inline-block; padding: 10px; background-color: {color}; color: black;
|
| 106 |
+
border-radius: {border_radius}; max-width: 70%;">
|
| 107 |
+
{message}
|
| 108 |
+
</span>
|
| 109 |
+
</div>
|
| 110 |
+
""", unsafe_allow_html=True)
|
| 111 |
+
|
| 112 |
+
# Response function
|
| 113 |
+
def load_answer(question):
|
| 114 |
+
# Add user question to the message history
|
| 115 |
+
st.session_state.sessionMessages.append(HumanMessage(content=question))
|
| 116 |
+
|
| 117 |
+
# Get AI's response
|
| 118 |
+
assistant_answer = llm.invoke(st.session_state.sessionMessages)
|
| 119 |
+
|
| 120 |
+
# Append AI's answer to the session messages
|
| 121 |
+
if isinstance(assistant_answer, AIMessage):
|
| 122 |
+
st.session_state.sessionMessages.append(assistant_answer)
|
| 123 |
+
return assistant_answer.content
|
| 124 |
+
else:
|
| 125 |
+
st.session_state.sessionMessages.append(AIMessage(content=assistant_answer))
|
| 126 |
+
return assistant_answer
|
| 127 |
+
|
| 128 |
+
# Capture user input
|
| 129 |
+
def get_text():
|
| 130 |
+
input_text = st.text_input("You: ", key="input", placeholder="Type your question here...")
|
| 131 |
+
return str(input_text)
|
| 132 |
+
|
| 133 |
+
# Main implementation
|
| 134 |
+
user_input = get_text()
|
| 135 |
+
submit = st.button("π Get a Magical Answer π")
|
| 136 |
+
|
| 137 |
+
if submit and user_input:
|
| 138 |
+
# Display the user's question
|
| 139 |
+
chat_bubble(user_input, is_user=True)
|
| 140 |
+
|
| 141 |
+
# Load the response and display it as a chat bubble
|
| 142 |
+
response = load_answer(user_input)
|
| 143 |
+
chat_bubble(response, is_user=False)
|
| 144 |
+
|
| 145 |
+
# Background styling and layout enhancements
|
| 146 |
+
st.markdown("""
|
| 147 |
+
<style>
|
| 148 |
+
.stApp {
|
| 149 |
+
background: linear-gradient(to right, #FFEFBA, #FFFFFF);
|
| 150 |
+
color: #4B0082;
|
| 151 |
+
font-family: Arial, sans-serif;
|
| 152 |
+
}
|
| 153 |
+
input[type="text"] {
|
| 154 |
+
padding: 8px;
|
| 155 |
+
border: 2px solid #4B0082;
|
| 156 |
+
border-radius: 15px;
|
| 157 |
+
outline: none;
|
| 158 |
+
}
|
| 159 |
+
button {
|
| 160 |
+
background-color: #4B0082;
|
| 161 |
+
color: white;
|
| 162 |
+
border-radius: 15px;
|
| 163 |
+
}
|
| 164 |
+
</style>
|
| 165 |
+
""", unsafe_allow_html=True)
|