Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -16,7 +16,17 @@ from reportlab.lib.styles import getSampleStyleSheet
|
|
| 16 |
from reportlab.lib.pagesizes import A4
|
| 17 |
|
| 18 |
# =========================================================
|
| 19 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
# =========================================================
|
| 21 |
INTENTS = {
|
| 22 |
"LEAVE": ["leave", "holiday", "pto", "sick"],
|
|
@@ -44,7 +54,7 @@ st.set_page_config(
|
|
| 44 |
)
|
| 45 |
|
| 46 |
# =========================================================
|
| 47 |
-
# PHARMA UI
|
| 48 |
# =========================================================
|
| 49 |
st.markdown("""
|
| 50 |
<style>
|
|
@@ -52,10 +62,10 @@ st.markdown("""
|
|
| 52 |
section[data-testid="stSidebar"] { background-color: #ffffff; border-right: 1px solid #e5e7eb; }
|
| 53 |
.header-card { background: white; padding: 18px; border-radius: 18px; box-shadow: 0 2px 10px rgba(0,0,0,0.06); margin-bottom: 16px; }
|
| 54 |
.role-badge { padding: 6px 14px; border-radius: 999px; font-size: 12px; font-weight: 600; background-color: #e0e7ff; color: #3730a3; }
|
| 55 |
-
section[data-testid="stChatMessage"] { border-radius: 18px; padding: 14px; margin-bottom: 10px; font-size: 15px; }
|
| 56 |
div[data-testid="stChatMessage"][aria-label="user"] { background-color: #eef2ff; border-left: 5px solid #4f46e5; }
|
| 57 |
div[data-testid="stChatMessage"][aria-label="assistant"] { background-color: #ffffff; border-left: 5px solid #16a34a; }
|
| 58 |
-
textarea { border-radius: 16px !important; }
|
| 59 |
button { border-radius: 12px !important; }
|
| 60 |
</style>
|
| 61 |
""", unsafe_allow_html=True)
|
|
@@ -65,16 +75,17 @@ button { border-radius: 12px !important; }
|
|
| 65 |
# =========================================================
|
| 66 |
if "messages" not in st.session_state:
|
| 67 |
st.session_state.messages = []
|
|
|
|
| 68 |
if "vector_ready" not in st.session_state:
|
| 69 |
st.session_state.vector_ready = False
|
| 70 |
|
| 71 |
# =========================================================
|
| 72 |
-
# SIDEBAR
|
| 73 |
# =========================================================
|
| 74 |
with st.sidebar:
|
| 75 |
st.markdown("### 💼 HRMS Chatbot")
|
| 76 |
|
| 77 |
-
role = st.selectbox("
|
| 78 |
st.markdown("---")
|
| 79 |
|
| 80 |
uploaded = st.file_uploader("Upload HR Document", type=["pdf", "docx", "txt"])
|
|
@@ -92,7 +103,6 @@ with st.sidebar:
|
|
| 92 |
st.session_state.vector_ready = False
|
| 93 |
st.rerun()
|
| 94 |
|
| 95 |
-
# PDF EXPORT
|
| 96 |
if st.session_state.messages:
|
| 97 |
def generate_pdf():
|
| 98 |
buffer = BytesIO()
|
|
@@ -111,7 +121,9 @@ with st.sidebar:
|
|
| 111 |
|
| 112 |
for msg in st.session_state.messages:
|
| 113 |
prefix = "Q:" if msg["role"] == "user" else "A:"
|
| 114 |
-
story.append(
|
|
|
|
|
|
|
| 115 |
story.append(Spacer(1, 8))
|
| 116 |
|
| 117 |
doc.build(story)
|
|
@@ -137,47 +149,51 @@ st.markdown("""
|
|
| 137 |
</div>
|
| 138 |
""", unsafe_allow_html=True)
|
| 139 |
|
| 140 |
-
st.markdown(
|
|
|
|
|
|
|
|
|
|
| 141 |
|
| 142 |
# =========================================================
|
| 143 |
-
# CHAT HISTORY
|
| 144 |
# =========================================================
|
| 145 |
for msg in st.session_state.messages:
|
| 146 |
with st.chat_message(msg["role"]):
|
| 147 |
st.markdown(msg["content"])
|
| 148 |
|
| 149 |
# =========================================================
|
| 150 |
-
# CHAT INPUT (
|
| 151 |
# =========================================================
|
| 152 |
-
question = st.chat_input(
|
|
|
|
|
|
|
| 153 |
|
| 154 |
if question:
|
|
|
|
| 155 |
st.session_state.messages.append({"role": "user", "content": question})
|
| 156 |
with st.chat_message("user"):
|
| 157 |
st.markdown(question)
|
| 158 |
|
|
|
|
| 159 |
with st.chat_message("assistant"):
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
# Typing dots
|
| 163 |
-
typing = st.empty()
|
| 164 |
-
for dots in ["⏳", "⏳.", "⏳..", "⏳..."]:
|
| 165 |
-
typing.markdown(dots)
|
| 166 |
-
time.sleep(0.15)
|
| 167 |
-
typing.empty()
|
| 168 |
|
| 169 |
if not is_hrms_query(question) or not role_allowed(role, question):
|
| 170 |
answer = hrms_refusal()
|
| 171 |
-
|
| 172 |
else:
|
| 173 |
intent = detect_intent(question)
|
| 174 |
retriever = get_retriever()
|
|
|
|
| 175 |
context_docs = (
|
| 176 |
retriever.get_relevant_documents(intent)
|
| 177 |
if retriever and st.session_state.vector_ready
|
| 178 |
else []
|
| 179 |
)
|
| 180 |
|
|
|
|
|
|
|
| 181 |
final_prompt = f"""
|
| 182 |
{SYSTEM_PROMPT}
|
| 183 |
|
|
@@ -187,24 +203,29 @@ ROLE CONTEXT:
|
|
| 187 |
INTENT:
|
| 188 |
{intent}
|
| 189 |
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
- Explain key rules
|
| 193 |
-
- End with role responsibility
|
| 194 |
-
- Do not make decisions
|
| 195 |
|
| 196 |
HR DOCUMENT CONTEXT:
|
| 197 |
{context_docs}
|
| 198 |
|
| 199 |
-
QUESTION:
|
| 200 |
{question}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
"""
|
| 202 |
|
| 203 |
-
stream_handler = StreamHandler(
|
| 204 |
llm = get_llm(callbacks=[stream_handler])
|
| 205 |
llm.invoke(final_prompt)
|
| 206 |
-
answer = stream_handler.text
|
| 207 |
|
| 208 |
-
|
| 209 |
|
| 210 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
from reportlab.lib.pagesizes import A4
|
| 17 |
|
| 18 |
# =========================================================
|
| 19 |
+
# CHATGPT-STYLE MEMORY (RECENT CONTEXT ONLY)
|
| 20 |
+
# =========================================================
|
| 21 |
+
def build_chat_history(messages, max_turns=6):
|
| 22 |
+
history = []
|
| 23 |
+
for msg in messages[-max_turns:]:
|
| 24 |
+
role = "User" if msg["role"] == "user" else "Assistant"
|
| 25 |
+
history.append(f"{role}: {msg['content']}")
|
| 26 |
+
return "\n".join(history)
|
| 27 |
+
|
| 28 |
+
# =========================================================
|
| 29 |
+
# HRMS INTENT CLASSIFICATION
|
| 30 |
# =========================================================
|
| 31 |
INTENTS = {
|
| 32 |
"LEAVE": ["leave", "holiday", "pto", "sick"],
|
|
|
|
| 54 |
)
|
| 55 |
|
| 56 |
# =========================================================
|
| 57 |
+
# CHATGPT-LIKE PHARMA UI
|
| 58 |
# =========================================================
|
| 59 |
st.markdown("""
|
| 60 |
<style>
|
|
|
|
| 62 |
section[data-testid="stSidebar"] { background-color: #ffffff; border-right: 1px solid #e5e7eb; }
|
| 63 |
.header-card { background: white; padding: 18px; border-radius: 18px; box-shadow: 0 2px 10px rgba(0,0,0,0.06); margin-bottom: 16px; }
|
| 64 |
.role-badge { padding: 6px 14px; border-radius: 999px; font-size: 12px; font-weight: 600; background-color: #e0e7ff; color: #3730a3; }
|
| 65 |
+
section[data-testid="stChatMessage"] { border-radius: 18px; padding: 14px; margin-bottom: 10px; font-size: 15px; line-height: 1.6; }
|
| 66 |
div[data-testid="stChatMessage"][aria-label="user"] { background-color: #eef2ff; border-left: 5px solid #4f46e5; }
|
| 67 |
div[data-testid="stChatMessage"][aria-label="assistant"] { background-color: #ffffff; border-left: 5px solid #16a34a; }
|
| 68 |
+
textarea { border-radius: 16px !important; font-size: 15px !important; }
|
| 69 |
button { border-radius: 12px !important; }
|
| 70 |
</style>
|
| 71 |
""", unsafe_allow_html=True)
|
|
|
|
| 75 |
# =========================================================
|
| 76 |
if "messages" not in st.session_state:
|
| 77 |
st.session_state.messages = []
|
| 78 |
+
|
| 79 |
if "vector_ready" not in st.session_state:
|
| 80 |
st.session_state.vector_ready = False
|
| 81 |
|
| 82 |
# =========================================================
|
| 83 |
+
# SIDEBAR (MINIMAL – LIKE CHATGPT)
|
| 84 |
# =========================================================
|
| 85 |
with st.sidebar:
|
| 86 |
st.markdown("### 💼 HRMS Chatbot")
|
| 87 |
|
| 88 |
+
role = st.selectbox("Active Role", ["Employee", "HR", "Manager"])
|
| 89 |
st.markdown("---")
|
| 90 |
|
| 91 |
uploaded = st.file_uploader("Upload HR Document", type=["pdf", "docx", "txt"])
|
|
|
|
| 103 |
st.session_state.vector_ready = False
|
| 104 |
st.rerun()
|
| 105 |
|
|
|
|
| 106 |
if st.session_state.messages:
|
| 107 |
def generate_pdf():
|
| 108 |
buffer = BytesIO()
|
|
|
|
| 121 |
|
| 122 |
for msg in st.session_state.messages:
|
| 123 |
prefix = "Q:" if msg["role"] == "user" else "A:"
|
| 124 |
+
story.append(
|
| 125 |
+
Paragraph(f"<b>{prefix}</b> {msg['content']}", styles["Normal"])
|
| 126 |
+
)
|
| 127 |
story.append(Spacer(1, 8))
|
| 128 |
|
| 129 |
doc.build(story)
|
|
|
|
| 149 |
</div>
|
| 150 |
""", unsafe_allow_html=True)
|
| 151 |
|
| 152 |
+
st.markdown(
|
| 153 |
+
f"**Active Role:** <span class='role-badge'>{role}</span>",
|
| 154 |
+
unsafe_allow_html=True
|
| 155 |
+
)
|
| 156 |
|
| 157 |
# =========================================================
|
| 158 |
+
# CHAT HISTORY (CHATGPT STYLE)
|
| 159 |
# =========================================================
|
| 160 |
for msg in st.session_state.messages:
|
| 161 |
with st.chat_message(msg["role"]):
|
| 162 |
st.markdown(msg["content"])
|
| 163 |
|
| 164 |
# =========================================================
|
| 165 |
+
# CHAT INPUT (ENTER = SEND | SHIFT+ENTER = NEW LINE)
|
| 166 |
# =========================================================
|
| 167 |
+
question = st.chat_input(
|
| 168 |
+
"Message HRMS Chatbot… (Enter to send • Shift+Enter for new line)"
|
| 169 |
+
)
|
| 170 |
|
| 171 |
if question:
|
| 172 |
+
# ---------------- USER ----------------
|
| 173 |
st.session_state.messages.append({"role": "user", "content": question})
|
| 174 |
with st.chat_message("user"):
|
| 175 |
st.markdown(question)
|
| 176 |
|
| 177 |
+
# ---------------- ASSISTANT (CHATGPT STYLE) ----------------
|
| 178 |
with st.chat_message("assistant"):
|
| 179 |
+
response_container = st.empty()
|
| 180 |
+
response_container.markdown("_Thinking…_")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
|
| 182 |
if not is_hrms_query(question) or not role_allowed(role, question):
|
| 183 |
answer = hrms_refusal()
|
| 184 |
+
response_container.markdown(answer)
|
| 185 |
else:
|
| 186 |
intent = detect_intent(question)
|
| 187 |
retriever = get_retriever()
|
| 188 |
+
|
| 189 |
context_docs = (
|
| 190 |
retriever.get_relevant_documents(intent)
|
| 191 |
if retriever and st.session_state.vector_ready
|
| 192 |
else []
|
| 193 |
)
|
| 194 |
|
| 195 |
+
chat_history = build_chat_history(st.session_state.messages)
|
| 196 |
+
|
| 197 |
final_prompt = f"""
|
| 198 |
{SYSTEM_PROMPT}
|
| 199 |
|
|
|
|
| 203 |
INTENT:
|
| 204 |
{intent}
|
| 205 |
|
| 206 |
+
CONVERSATION HISTORY:
|
| 207 |
+
{chat_history}
|
|
|
|
|
|
|
|
|
|
| 208 |
|
| 209 |
HR DOCUMENT CONTEXT:
|
| 210 |
{context_docs}
|
| 211 |
|
| 212 |
+
USER QUESTION:
|
| 213 |
{question}
|
| 214 |
+
|
| 215 |
+
RESPONSE GUIDELINES:
|
| 216 |
+
- Use clear Markdown formatting
|
| 217 |
+
- Use bullet points and headings when helpful
|
| 218 |
+
- Be concise but complete
|
| 219 |
+
- Stay strictly within HRMS scope
|
| 220 |
"""
|
| 221 |
|
| 222 |
+
stream_handler = StreamHandler(response_container)
|
| 223 |
llm = get_llm(callbacks=[stream_handler])
|
| 224 |
llm.invoke(final_prompt)
|
|
|
|
| 225 |
|
| 226 |
+
answer = stream_handler.text
|
| 227 |
|
| 228 |
+
# ---------------- SAVE ASSISTANT MESSAGE ----------------
|
| 229 |
+
st.session_state.messages.append(
|
| 230 |
+
{"role": "assistant", "content": answer}
|
| 231 |
+
)
|