File size: 13,403 Bytes
52e6ad6
b66efdf
67c26fd
b66efdf
52e6ad6
 
 
67c26fd
52e6ad6
 
 
 
b66efdf
52e6ad6
 
cdc6e99
67c26fd
b66efdf
 
 
 
 
 
 
 
 
 
67c26fd
52e6ad6
67c26fd
52e6ad6
 
67c26fd
 
52e6ad6
 
 
 
 
 
 
67c26fd
b66efdf
67c26fd
 
52e6ad6
 
 
 
 
 
 
 
 
 
 
67c26fd
b66efdf
67c26fd
 
52e6ad6
 
67c26fd
52e6ad6
 
 
 
67c26fd
b66efdf
67c26fd
 
52e6ad6
 
 
 
 
 
 
 
 
67c26fd
52e6ad6
 
 
 
 
67c26fd
b66efdf
67c26fd
52e6ad6
67c26fd
52e6ad6
67c26fd
 
 
 
 
 
52e6ad6
67c26fd
 
 
 
 
 
52e6ad6
67c26fd
 
 
 
 
 
 
 
 
 
 
52e6ad6
67c26fd
 
 
 
 
 
52e6ad6
67c26fd
52e6ad6
67c26fd
 
 
 
 
 
52e6ad6
67c26fd
 
 
 
52e6ad6
 
67c26fd
 
 
 
 
 
 
 
 
b66efdf
67c26fd
52e6ad6
67c26fd
 
 
52e6ad6
67c26fd
52e6ad6
67c26fd
 
 
52e6ad6
67c26fd
 
 
 
 
 
 
52e6ad6
67c26fd
52e6ad6
67c26fd
 
52e6ad6
67c26fd
 
 
 
 
52e6ad6
67c26fd
 
52e6ad6
67c26fd
 
 
 
 
 
 
52e6ad6
67c26fd
 
 
 
b66efdf
67c26fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52e6ad6
67c26fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b66efdf
67c26fd
 
 
 
 
 
 
b66efdf
67c26fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b66efdf
67c26fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b66efdf
67c26fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b66efdf
67c26fd
 
 
9b5201a
67c26fd
 
b66efdf
67c26fd
 
b66efdf
 
 
 
 
 
 
 
67c26fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b66efdf
67c26fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cdc6e99
67c26fd
b66efdf
67c26fd
cdc6e99
 
b66efdf
 
 
 
 
 
 
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
import os

import gradio as gr
import spaces
from dotenv import load_dotenv
from PyPDF2 import PdfReader

from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_cohere import CohereEmbeddings, ChatCohere
from langchain_community.vectorstores import FAISS
from langchain_classic.chains import ConversationalRetrievalChain


load_dotenv()


# =========================================================
# 1. ZERO-GPU STARTUP CHECK
# =========================================================

@spaces.GPU(duration=1)
def zerogpu_check():
    return None


# =========================================================
# 2. EXTRACT TEXT FROM PDF FILES
# =========================================================

def get_pdf_text(pdf_files):
    text = ""

    for pdf_file in pdf_files:
        pdf_reader = PdfReader(pdf_file)

        for page in pdf_reader.pages:
            text += page.extract_text() or ""

    return text


# =========================================================
# 3. SPLIT TEXT INTO CHUNKS
# =========================================================

def get_text_chunks(text):
    text_splitter = RecursiveCharacterTextSplitter(
        separators=["\n\n", "\n", " ", ""],
        chunk_size=1000,
        chunk_overlap=200,
        length_function=len
    )

    return text_splitter.split_text(text)


# =========================================================
# 4. CREATE COHERE EMBEDDINGS
# =========================================================

def get_embeddings():
    return CohereEmbeddings(
        model="embed-english-v3.0",
        user_agent="langchain"
    )


# =========================================================
# 5. CREATE CONVERSATIONAL RETRIEVAL CHAIN
# =========================================================

def get_conversational_chain(vectorstore):
    llm = ChatCohere(
        model="command-r-08-2024",
        temperature=0
    )

    conversational_chain = ConversationalRetrievalChain.from_llm(
        llm=llm,
        retriever=vectorstore.as_retriever(),
        return_source_documents=False
    )

    return conversational_chain


# =========================================================
# 6. PROCESS UPLOADED DOCUMENTS
# =========================================================

def process_documents(pdf_files):

    if not pdf_files:
        return (
            None,
            [],
            "Please upload at least one PDF."
        )

    if not os.getenv("COHERE_API_KEY"):
        return (
            None,
            [],
            "Error: Cohere API key is missing."
        )

    try:
        raw_text = get_pdf_text(pdf_files)

        if not raw_text.strip():
            return (
                None,
                [],
                "No extractable text found in the PDFs."
            )

        chunks = get_text_chunks(raw_text)

        if not chunks:
            return (
                None,
                [],
                "No text chunks were created."
            )

        embeddings = get_embeddings()

        vectorstore = FAISS.from_texts(
            texts=chunks,
            embedding=embeddings
        )

        chain = get_conversational_chain(vectorstore)

        return (
            chain,
            [],
            "Documents processed successfully! You can now ask questions."
        )

    except Exception as e:
        return (
            None,
            [],
            f"Error processing documents: {str(e)}"
        )


# =========================================================
# 7. CONVERT GRADIO CHAT HISTORY TO LANGCHAIN FORMAT
# =========================================================

def convert_chat_history(history):
    chat_history = []
    pending_question = None

    for message in history or []:

        if isinstance(message, dict):
            role = message.get("role")
            content = message.get("content", "")

            if isinstance(content, list):
                content = " ".join(
                    str(item.get("text", ""))
                    if isinstance(item, dict)
                    else str(item)
                    for item in content
                )

            content = str(content)

            if role == "user":
                pending_question = content

            elif role == "assistant" and pending_question is not None:
                chat_history.append(
                    (pending_question, content)
                )
                pending_question = None

        elif isinstance(message, (list, tuple)) and len(message) == 2:
            user_message, assistant_message = message

            if user_message:
                chat_history.append(
                    (
                        str(user_message),
                        str(assistant_message or "")
                    )
                )

    return chat_history


# =========================================================
# 8. HANDLE USER QUESTIONS
# =========================================================

def handle_user_question(question, history, chain):

    history = list(history or [])

    if not question or not question.strip():
        return history, ""

    question = question.strip()

    if chain is None:
        history.append({
            "role": "user",
            "content": question
        })

        history.append({
            "role": "assistant",
            "content": "Please upload and process your PDFs first."
        })

        return history, ""

    try:
        chat_history = convert_chat_history(history)

        response = chain.invoke({
            "question": question,
            "chat_history": chat_history
        })

        answer = response.get(
            "answer",
            "No answer was generated."
        )

        if isinstance(answer, list):
            answer = " ".join(
                str(item.get("text", ""))
                if isinstance(item, dict)
                else str(item)
                for item in answer
            )
        else:
            answer = str(answer)

        history.append({
            "role": "user",
            "content": question
        })

        history.append({
            "role": "assistant",
            "content": answer
        })

        return history, ""

    except Exception as e:
        history.append({
            "role": "user",
            "content": question
        })

        history.append({
            "role": "assistant",
            "content": f"Error generating answer: {str(e)}"
        })

        return history, ""


# =========================================================
# 9. CLEAR CHAT
# =========================================================

def clear_chat():
    return []


# =========================================================
# 10. CUSTOM CSS
# =========================================================

custom_css = """
.gradio-container {
    background: #0e0f14 !important;
    color: #f8fafc !important;
    max-width: 100% !important;
    min-height: 100vh;
    padding: 16px !important;
}

#main-row {
    display: flex !important;
    align-items: flex-start !important;
    gap: 20px !important;
    max-width: 1500px;
    margin: 0 auto !important;
}

#sidebar {
    background: #24252f !important;
    border: 1px solid #343642 !important;
    border-radius: 14px !important;
    padding: 16px !important;
    height: fit-content !important;
    min-height: 0 !important;
    align-self: flex-start !important;
    margin-top: 40px;
}

#sidebar h2 {
    font-size: 1.15rem !important;
    margin: 0 0 12px 0 !important;
    color: #ffffff !important;
}

#sidebar [data-testid="file-upload"] {
    min-height: 110px !important;
}

#process-button {
    margin-top: 10px !important;
    border-radius: 8px !important;
    transition: transform 0.2s ease, box-shadow 0.2s ease;
}

#process-button:hover {
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(37, 99, 235, 0.3);
}

#status {
    margin-top: 10px !important;
}

#main-panel {
    padding: 0 8px !important;
    min-width: 0 !important;
}

#hero {
    background: linear-gradient(120deg, #0f172a 0%, #1d4ed8 100%);
    padding: 28px 32px;
    border-radius: 18px;
    box-shadow: 0 10px 30px rgba(29, 78, 216, 0.18);
    margin: 24px 0 18px 0;
    transition: box-shadow 0.2s ease;
}

#hero:hover {
    box-shadow: 0 12px 34px rgba(29, 78, 216, 0.28);
}

#hero h1 {
    color: white !important;
    font-size: clamp(1.5rem, 2.5vw, 2.3rem) !important;
    font-weight: 800 !important;
    line-height: 1.25 !important;
    margin: 0 !important;
}

#hero p {
    color: #dbeafe !important;
    font-size: 1rem !important;
    margin: 10px 0 0 0 !important;
}

#chatbot {
    border-radius: 12px !important;
    min-height: 280px !important;
    height: calc(100vh - 390px) !important;
    max-height: 650px !important;
    overflow: auto !important;
}

#question-box {
    margin-top: 12px !important;
}

#question-box textarea {
    min-height: 48px !important;
    border-radius: 10px !important;
}

#main-panel button {
    border-radius: 8px !important;
    transition: background 0.2s ease;
}

footer {
    display: none !important;
}

@media (max-width: 900px) {
    .gradio-container {
        padding: 12px !important;
    }

    #main-row {
        gap: 14px !important;
    }

    #sidebar {
        margin-top: 30px;
        padding: 12px !important;
    }

    #hero {
        padding: 22px;
    }

    #chatbot {
        height: 55vh !important;
    }
}

@media (max-width: 700px) {
    .gradio-container {
        padding: 8px !important;
    }

    #main-row {
        flex-direction: column !important;
        gap: 12px !important;
    }

    #sidebar {
        width: 100% !important;
        max-width: 100% !important;
        margin-top: 0 !important;
        padding: 14px !important;
    }

    #main-panel {
        width: 100% !important;
        max-width: 100% !important;
        padding: 0 !important;
    }

    #hero {
        margin: 8px 0 14px 0;
        padding: 20px;
        border-radius: 14px;
    }

    #hero h1 {
        font-size: 1.5rem !important;
    }

    #hero p {
        font-size: 0.9rem !important;
    }

    #chatbot {
        height: 55vh !important;
        min-height: 300px !important;
        max-height: none !important;
    }

    #question-box {
        margin-top: 10px !important;
    }
}
"""


# =========================================================
# 11. BUILD GRADIO INTERFACE
# =========================================================

with gr.Blocks(
    title="Chat with Documents"
) as demo:

    # State to store the conversational chain
    chain_state = gr.State(None)

    # Hidden event to satisfy ZeroGPU startup detection
    hidden_button = gr.Button(visible=False)
    hidden_button.click(
        fn=zerogpu_check,
        inputs=[],
        outputs=[]
    )

    with gr.Row(
        elem_id="main-row",
        equal_height=False
    ):

        # LEFT SIDEBAR
        with gr.Column(
            scale=1,
            min_width=250,
            elem_id="sidebar"
        ):

            gr.Markdown("## Upload Documents")

            pdf_upload = gr.File(
                file_types=[".pdf"],
                file_count="multiple",
                type="filepath",
                height=120
            )

            process_button = gr.Button(
                "Process Documents",
                variant="primary",
                elem_id="process-button"
            )

            status = gr.Textbox(
                label="Status",
                interactive=False,
                lines=2,
                elem_id="status"
            )

        # RIGHT CHAT AREA
        with gr.Column(
            scale=4,
            min_width=300,
            elem_id="main-panel"
        ):

            gr.HTML("""
                <div id="hero">
                    <h1>AI Chatbot for Your Documents</h1>
                    <p>
                        Upload a PDF and ask questions in natural language.
                    </p>
                </div>
            """)

            chatbot = gr.Chatbot(
                label="Chat with your PDFs",
                height=500,
                elem_id="chatbot"
            )

            question = gr.Textbox(
                label="Ask a question",
                placeholder="Ask a question about your PDFs",
                lines=1,
                max_lines=1,
                elem_id="question-box"
            )

            clear_button = gr.Button("Clear Chat")

    # Process PDF documents
    process_button.click(
        fn=process_documents,
        inputs=[pdf_upload],
        outputs=[chain_state, chatbot, status]
    )

    # Press Enter to send a question
    question.submit(
        fn=handle_user_question,
        inputs=[question, chatbot, chain_state],
        outputs=[chatbot, question]
    )

    # Clear conversation
    clear_button.click(
        fn=clear_chat,
        inputs=[],
        outputs=[chatbot]
    )


# =========================================================
# 12. LAUNCH APPLICATION
# =========================================================

if __name__ == "__main__":
    demo.queue().launch(
        theme=gr.themes.Base(),
        css=custom_css,
        server_name="0.0.0.0",
        server_port=7860,
        ssr_mode=False
    )