File size: 5,045 Bytes
9b91a9e
959c484
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92802b6
 
 
 
 
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import spaces
import os
import gradio as gr

from ui import upload_document, chat, clear_chat

custom_css = """
.gradio-container {
    max-width: 100% !important;
    padding: 1.5rem 2rem !important;
    margin: 0 !important;
}
.header-box {
    text-align: center;
    padding: 1rem;
    margin-bottom: 1rem;
    width: 100%;
}
.header-box h1 {
    margin-bottom: 0.5rem;
}
.header-box p {
    margin: 0.3rem 0;
}
.example-btn {
    border-radius: 8px !important;
    font-size: 0.88rem !important;
    flex: 1 !important;
}
.send-btn {
    background: linear-gradient(135deg, #2563eb 0%, #1d4ed8 100%) !important;
    color: white !important;
    border: none !important;
    font-weight: 600 !important;
    border-radius: 8px !important;
    box-shadow: 0 4px 12px rgba(37, 99, 235, 0.3) !important;
}
.send-btn:hover {
    background: linear-gradient(135deg, #1d4ed8 0%, #1e40af 100%) !important;
    box-shadow: 0 6px 16px rgba(37, 99, 235, 0.45) !important;
}
"""


def user_submit(user_message, history):
    if not user_message or not user_message.strip():
        return "", history
    updated_history = chat(user_message, history)
    return "", updated_history


with gr.Blocks(title="Enterprise AI Document Intelligence Platform") as demo:

    gr.Markdown(
        """
        <div style="text-align: center;">

        # πŸ€– Enterprise AI Document Intelligence Platform

        Upload a PDF and chat with your documents using<br>AI-powered Retrieval-Augmented Generation (RAG)

        *Powered by*<br>**Groq β€’ Sentence Transformers β€’ FAISS**

        </div>
        """
    )

    gr.Markdown("---")

    with gr.Row():
        # Sidebar for PDF Ingestion (responsive 1/4 screen width)
        with gr.Column(scale=1, min_width=320):
            gr.Markdown("### πŸ“„ Document Ingestion")

            upload = gr.File(
                label="Upload Enterprise PDF",
                file_types=[".pdf"],
                file_count="single"
            )

            status = gr.Markdown("*No document indexed yet. Upload a PDF above.*")

            active_doc = gr.Markdown("ℹ️ **Active Index:** Empty")

            clear_btn = gr.Button("πŸ—‘οΈ Clear Chat History", variant="secondary")

            gr.Markdown(
                """
                ---
                **System Stack:**
                - πŸ“„ PyMuPDF PDF Text Extraction
                - 🧠 `all-MiniLM-L6-v2` Vector Embeddings
                - ⚑ FAISS Vector Similarity Index
                - πŸ€– Groq `llama-3.3-70b-versatile` Engine
                """
            )

        # Main Chat Panel (expanding to fill remaining full width)
        with gr.Column(scale=3):
            gr.Markdown("### πŸ’¬ Enterprise Knowledge Chat")

            chatbot = gr.Chatbot(
                height=560,
                placeholder="πŸ’‘ Upload a PDF document on the left, then ask questions here."
            )

            gr.Markdown("#### πŸ’‘ Try Asking")
            with gr.Row():
                ex1 = gr.Button("πŸ“„ Summarize this document", variant="secondary", size="sm", elem_classes=["example-btn"])
                ex2 = gr.Button("πŸ› οΈ List technical skills", variant="secondary", size="sm", elem_classes=["example-btn"])
                ex3 = gr.Button("πŸš€ What projects are mentioned?", variant="secondary", size="sm", elem_classes=["example-btn"])
                ex4 = gr.Button("πŸ“‹ Give me a short overview", variant="secondary", size="sm", elem_classes=["example-btn"])

            with gr.Row():
                question = gr.Textbox(
                    placeholder="Ask anything about the uploaded document...",
                    show_label=False,
                    scale=5,
                    container=False
                )
                ask = gr.Button("Send πŸš€", variant="primary", scale=1, elem_classes=["send-btn"])

    # Event Connections
    upload.upload(
        upload_document,
        inputs=upload,
        outputs=[status, active_doc]
    )

    ask.click(
        user_submit,
        inputs=[question, chatbot],
        outputs=[question, chatbot]
    )

    question.submit(
        user_submit,
        inputs=[question, chatbot],
        outputs=[question, chatbot]
    )

    ex1.click(
        lambda h: user_submit("Summarize this document", h),
        inputs=[chatbot],
        outputs=[question, chatbot]
    )

    ex2.click(
        lambda h: user_submit("List technical skills", h),
        inputs=[chatbot],
        outputs=[question, chatbot]
    )

    ex3.click(
        lambda h: user_submit("What projects are mentioned?", h),
        inputs=[chatbot],
        outputs=[question, chatbot]
    )

    ex4.click(
        lambda h: user_submit("Give me a short overview", h),
        inputs=[chatbot],
        outputs=[question, chatbot]
    )

    clear_btn.click(
        clear_chat,
        inputs=[],
        outputs=[chatbot]
    )

if __name__ == "__main__":
    demo.launch(
        css=custom_css,
        theme=gr.themes.Soft(),
        ssr_mode=False
    )