archana2324 commited on
Commit
982d6fd
Β·
verified Β·
1 Parent(s): 1fb5170

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -31
app.py CHANGED
@@ -9,16 +9,15 @@ from langchain.schema import HumanMessage, SystemMessage, AIMessage
9
  warnings.filterwarnings("ignore")
10
 
11
  # Streamlit settings
12
- st.set_page_config(page_title="🌿 ArchanaCare πŸ§™β€β™€οΈ", page_icon="πŸ§™β€β™€οΈ", layout="centered")
 
 
13
  st.markdown("<h1 style='text-align: center; color: #4B0082;'>Welcome to ArchanaCare 🌿✨</h1>", unsafe_allow_html=True)
14
  st.markdown("<h3 style='color: #003366;'>How can I assist with your ailments or worries today? πŸ§ͺπŸ’«</h3>", unsafe_allow_html=True)
15
 
16
- # Adding vertical space without streamlit_extras
17
- st.markdown("<br><br>", unsafe_allow_html=True)
18
-
19
- # Initialize session state for messages with an introductory message
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
 
@@ -32,29 +31,24 @@ llm = ChatGoogleGenerativeAI(
32
  convert_system_message_to_human=True
33
  )
34
 
35
- # Define a function to create chat bubbles
36
  def chat_bubble(message, is_user=True):
37
  align = 'right' if is_user else 'left'
38
- color = '#ADD8E6' if is_user else '#E6E6FA'
39
- border_radius = '25px' if is_user else '25px'
40
  st.markdown(f"""
41
  <div style="text-align: {align}; padding: 10px;">
42
  <span style="display: inline-block; padding: 10px; background-color: {color}; color: black;
43
- border-radius: {border_radius}; max-width: 70%;">
44
  {message}
45
  </span>
46
  </div>
47
  """, unsafe_allow_html=True)
48
 
49
- # Response function
50
  def load_answer(question):
51
- # Add user question to the message history
52
  st.session_state.sessionMessages.append(HumanMessage(content=question))
53
-
54
- # Get AI's response
55
  assistant_answer = llm.invoke(st.session_state.sessionMessages)
56
 
57
- # Append AI's answer to the session messages
58
  if isinstance(assistant_answer, AIMessage):
59
  st.session_state.sessionMessages.append(assistant_answer)
60
  return assistant_answer.content
@@ -62,24 +56,24 @@ def load_answer(question):
62
  st.session_state.sessionMessages.append(AIMessage(content=assistant_answer))
63
  return assistant_answer
64
 
65
- # Capture user input
66
- def get_text():
67
- input_text = st.text_input("You: ", key="input", placeholder="Type your question here...")
68
- return str(input_text)
 
 
69
 
70
- # Main implementation
71
- user_input = get_text()
72
- submit = st.button("🌟 Get a Magical Answer 🌟")
73
 
74
- if submit and user_input:
75
- # Display the user's question
76
- chat_bubble(user_input, is_user=True)
77
-
78
- # Load the response and display it as a chat bubble
79
- response = load_answer(user_input)
80
- chat_bubble(response, is_user=False)
81
 
82
- # Background styling and layout enhancements
83
  st.markdown("""
84
  <style>
85
  .stApp {
@@ -88,15 +82,17 @@ st.markdown("""
88
  font-family: Arial, sans-serif;
89
  }
90
  input[type="text"] {
91
- padding: 8px;
92
  border: 2px solid #4B0082;
93
  border-radius: 15px;
94
  outline: none;
 
95
  }
96
  button {
97
  background-color: #4B0082;
98
  color: white;
99
  border-radius: 15px;
 
100
  }
101
  </style>
102
  """, unsafe_allow_html=True)
 
9
  warnings.filterwarnings("ignore")
10
 
11
  # Streamlit settings
12
+ st.set_page_config(page_title="🌿 ArchanaCare πŸ§™β€β™€οΈ", page_icon="πŸ§™β€β™€οΈ", layout="wide")
13
+
14
+ # Header
15
  st.markdown("<h1 style='text-align: center; color: #4B0082;'>Welcome to ArchanaCare 🌿✨</h1>", unsafe_allow_html=True)
16
  st.markdown("<h3 style='color: #003366;'>How can I assist with your ailments or worries today? πŸ§ͺπŸ’«</h3>", unsafe_allow_html=True)
17
 
18
+ # Initialize session state for messages
 
 
 
19
  if "sessionMessages" not in st.session_state:
20
+ st.session_state.sessionMessages = [
21
  SystemMessage(content="You are a medieval magical healer known for your peculiar sarcasm.")
22
  ]
23
 
 
31
  convert_system_message_to_human=True
32
  )
33
 
34
+ # Function to create chat bubbles
35
  def chat_bubble(message, is_user=True):
36
  align = 'right' if is_user else 'left'
37
+ color = '#E1F5FE' if is_user else '#FFEBEE'
 
38
  st.markdown(f"""
39
  <div style="text-align: {align}; padding: 10px;">
40
  <span style="display: inline-block; padding: 10px; background-color: {color}; color: black;
41
+ border-radius: 15px; max-width: 70%; word-wrap: break-word;">
42
  {message}
43
  </span>
44
  </div>
45
  """, unsafe_allow_html=True)
46
 
47
+ # Function to load answer from the model
48
  def load_answer(question):
 
49
  st.session_state.sessionMessages.append(HumanMessage(content=question))
 
 
50
  assistant_answer = llm.invoke(st.session_state.sessionMessages)
51
 
 
52
  if isinstance(assistant_answer, AIMessage):
53
  st.session_state.sessionMessages.append(assistant_answer)
54
  return assistant_answer.content
 
56
  st.session_state.sessionMessages.append(AIMessage(content=assistant_answer))
57
  return assistant_answer
58
 
59
+ # Chat history display
60
+ for message in st.session_state.sessionMessages:
61
+ if isinstance(message, HumanMessage):
62
+ chat_bubble(message.content, is_user=True)
63
+ elif isinstance(message, AIMessage):
64
+ chat_bubble(message.content, is_user=False)
65
 
66
+ # Input area
67
+ user_input = st.text_input("You: ", key="input", placeholder="Type your question here...")
 
68
 
69
+ # Button for submission
70
+ if st.button("🌟 Get a Magical Answer 🌟"):
71
+ if user_input:
72
+ chat_bubble(user_input, is_user=True) # Display user input
73
+ response = load_answer(user_input) # Get response
74
+ chat_bubble(response, is_user=False) # Display AI response
 
75
 
76
+ # Background styling
77
  st.markdown("""
78
  <style>
79
  .stApp {
 
82
  font-family: Arial, sans-serif;
83
  }
84
  input[type="text"] {
85
+ padding: 10px;
86
  border: 2px solid #4B0082;
87
  border-radius: 15px;
88
  outline: none;
89
+ width: 100%;
90
  }
91
  button {
92
  background-color: #4B0082;
93
  color: white;
94
  border-radius: 15px;
95
+ margin-top: 10px;
96
  }
97
  </style>
98
  """, unsafe_allow_html=True)