"""
ui.py — Gradio layout and event wiring for PolicyAI Chatbot
Exports
-------
demo : gr.Blocks instance — passed to demo.launch() in app.py.
"""
import gradio as gr
from handlers import respond, clear_chat
SAMPLE_QUESTIONS = [
"What are the leave entitlements for employees?",
"What are the password and authentication requirements?",
"How should a security incident be reported and handled?",
"What security measures are required for remote work?",
"What are the disciplinary action procedures?",
"How should employees be trained on IT security awareness?",
]
# ── Layout ─────────────────────────────────────────────────────────────────────
with gr.Blocks(title="PolicyAI — HR & IT Security Intelligence") as demo:
# ── Header ─────────────────────────────────────────────────────────────────
gr.HTML("""
""")
# ── Main two-column layout ──────────────────────────────────────────────────
with gr.Row(equal_height=False):
# ── Left — chat interface ───────────────────────────────────────────
with gr.Column(scale=63):
chatbot = gr.Chatbot(
value=[],
height=500,
show_label=False,
layout="bubble",
elem_id="policyai-chat",
placeholder=(
""
"
🔐
"
"
"
"Ask PolicyAI anything
"
"
"
"HR policies, IT security standards, incident response, remote work — "
"select a quick question or type your own.
"
"
"
),
)
with gr.Row(elem_id="policyai-input-row"):
msg_input = gr.Textbox(
placeholder="Ask a policy question…",
show_label=False,
scale=6,
autofocus=True,
lines=1,
max_lines=5,
container=False,
elem_id="policyai-msg",
)
send_btn = gr.Button(
"Send ➤",
variant="primary",
scale=1,
min_width=78,
elem_id="policyai-send",
)
clear_btn = gr.Button(
"✕ Clear",
variant="secondary",
scale=1,
min_width=72,
elem_id="policyai-clear",
)
# ── Right — quick questions + sources ───────────────────────────────
with gr.Column(scale=37, min_width=260):
# Quick questions card
gr.HTML("""
💡 Quick Questions
""")
for q in SAMPLE_QUESTIONS:
gr.Button(q, variant="secondary", size="sm", elem_classes="q-btn").click(
fn=lambda question=q: question,
outputs=msg_input,
)
gr.HTML("
")
# Sources card
gr.HTML("""
📄 Retrieved Sources
""")
sources_box = gr.Textbox(
value="",
placeholder="Source chunks will appear here after your first question...",
interactive=False,
lines=13,
max_lines=22,
show_label=False,
container=False,
elem_id="policyai-sources",
)
gr.HTML("
")
# ── Footer ──────────────────────────────────────────────────────────────────
gr.HTML("""
""")
# ── Event wiring ────────────────────────────────────────────────────────────
send_btn.click(
fn=respond,
inputs=[msg_input, chatbot],
outputs=[chatbot, sources_box],
).then(fn=lambda: "", outputs=msg_input)
msg_input.submit(
fn=respond,
inputs=[msg_input, chatbot],
outputs=[chatbot, sources_box],
).then(fn=lambda: "", outputs=msg_input)
clear_btn.click(
fn=clear_chat,
outputs=[chatbot, msg_input, sources_box],
)