File size: 1,361 Bytes
1f46dfd
 
 
 
11c3562
1f46dfd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from ninebit_ciq import NineBitCIQClient

# Initialize CIQ client with API key (replace with your own or use gr.Textbox)
ciq_client = NineBitCIQClient(api_key="28a8c113-5184-4532-ac52-b3be164b95e1")

pdf_ingested = {"status": False}

def ingest_pdf(file):
    if file is None:
        return "Please upload a PDF first."
    result = ciq_client.ingest_file(file=file.name)
    pdf_ingested["status"] = True
    return "✅ PDF ingested successfully!"

def ask_question(query):
    if not pdf_ingested["status"]:
        return "⚠️ Please upload and ingest a PDF first."
    response = ciq_client.rag_query(query=query)
    return response

with gr.Blocks() as demo:
    gr.Markdown("## 📄 CIQ PDF Chatbot")
    gr.Markdown("Upload a PDF, ask a question, and get answers using NineBit CIQ RAG")

    with gr.Row():
        pdf_input = gr.File(label="Upload PDF", file_types=[".pdf"])
        upload_btn = gr.Button("Ingest PDF")

    upload_output = gr.Textbox(label="Ingestion Status")

    upload_btn.click(fn=ingest_pdf, inputs=pdf_input, outputs=upload_output)

    with gr.Row():
        query_input = gr.Textbox(label="Ask a Question")
        query_btn = gr.Button("Submit Query")

    answer_output = gr.Textbox(label="RAG Answer")

    query_btn.click(fn=ask_question, inputs=query_input, outputs=answer_output)

demo.launch()