File size: 6,887 Bytes
12bb39f
 
 
 
 
 
 
 
 
 
 
 
 
 
bf32d17
 
 
12bb39f
bf32d17
12bb39f
 
 
bf32d17
12bb39f
 
 
 
 
bf32d17
12bb39f
 
 
bf32d17
 
 
12bb39f
 
bf32d17
12bb39f
 
bf32d17
12bb39f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bf32d17
12bb39f
 
 
bf32d17
 
12bb39f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
"""
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("""
    <div id="policyai-header">
        <div class="header-logo">
            <span class="logo-icon">&#128272;</span>
            <span class="logo-text">PolicyAI</span>
        </div>
        <p class="header-sub">
            AI-powered HR &amp; IT Security policy intelligence &mdash; instant answers grounded in
            your HR policies, NIST Cybersecurity Framework, incident response guides,
            and remote work security documents, with exact page citations.
        </p>
        <div class="badge-row">
            <span class="badge badge-b">&#9889; Groq &middot; llama-3.1-8b</span>
            <span class="badge badge-g">&#10003; Zero hallucination</span>
            <span class="badge badge-v">&#128196; Page-level citations</span>
            <span class="badge badge-o">&#128196; 5 Policy Sources</span>
        </div>
    </div>
    """)

    # ── 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=(
                    "<div style='text-align:center;padding:70px 20px;'>"
                    "<div style='font-size:44px;margin-bottom:14px;'>&#128272;</div>"
                    "<div style='font-size:15px;font-weight:600;color:#2a3f5a;'>"
                    "Ask PolicyAI anything</div>"
                    "<div style='font-size:13px;margin-top:8px;color:#1e2f42;'>"
                    "HR policies, IT security standards, incident response, remote work &mdash; "
                    "select a quick question or type your own.</div>"
                    "</div>"
                ),
            )

            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("""
            <div class="panel-section">
                <div class="panel-title">&#128161; &nbsp;Quick Questions</div>
                <div class="panel-body">
            """)
            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("</div></div>")

            # Sources card
            gr.HTML("""
            <div class="panel-section" style="margin-top:12px;">
                <div class="panel-title">&#128196; &nbsp;Retrieved Sources</div>
            """)
            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("</div>")

    # ── Footer ──────────────────────────────────────────────────────────────────
    gr.HTML("""
    <div id="policyai-footer">
        Powered by
        <a href="https://groq.com" target="_blank">Groq</a>
        <span class="sep">&middot;</span>
        <a href="https://qdrant.tech" target="_blank">Qdrant</a>
        <span class="sep">&middot;</span>
        <a href="https://python.langchain.com" target="_blank">LangChain</a>
        <span class="sep">&middot;</span>
        <a href="https://gradio.app" target="_blank">Gradio</a>
        <span class="sep">|</span>
        Codebasics AI Engineering Bootcamp
    </div>
    """)

    # ── 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],
    )