File size: 2,744 Bytes
2c98ffc
 
 
 
 
38f3a0b
2c98ffc
 
38f3a0b
2c98ffc
 
 
38f3a0b
2c98ffc
38f3a0b
2c98ffc
 
38f3a0b
2c98ffc
38f3a0b
2c98ffc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38f3a0b
2c98ffc
 
38f3a0b
2c98ffc
 
 
38f3a0b
 
 
2c98ffc
38f3a0b
 
 
2c98ffc
 
 
 
38f3a0b
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
import os
import tempfile
import gradio as gr
from langchain_community.vectorstores import FAISS
from langchain_groq import ChatGroq
from langchain_community.embeddings import HuggingFaceBgeEmbeddings
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_core.runnables import RunnablePassthrough
from langchain.document_loaders import PyPDFLoader
from langchain import hub

# Set API key (Replace with your actual key)
os.environ["GROQ_API_KEY"] = "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"

# Initialize LLM and Embeddings
llm = ChatGroq(model="llama3-8b-8192")
model_name = "BAAI/bge-small-en"
hf_embeddings = HuggingFaceBgeEmbeddings(
    model_name=model_name,
    model_kwargs={'device': 'cpu'},
    encode_kwargs={'normalize_embeddings': True}
)

# Function to process PDF
def process_pdf(file):
    if file is None:
        return "Please upload a PDF file."

    # Save PDF temporarily
    with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as temp_file:
        temp_file.write(file)
        temp_file_path = temp_file.name

    # Load and process PDF
    loader = PyPDFLoader(temp_file_path)
    docs = loader.load()

    # Split text
    text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
    splits = text_splitter.split_documents(docs)

    # Create FAISS vector store
    vectorstore = FAISS.from_documents(documents=splits, embedding=hf_embeddings)
    retriever = vectorstore.as_retriever()

    # Load RAG prompt
    prompt = hub.pull("rlm/rag-prompt")

    def format_docs(docs):
        return "\n\n".join(doc.page_content for doc in docs)

    # RAG Chain
    global rag_chain
    rag_chain = (
        {"context": retriever | format_docs, "question": RunnablePassthrough()}
        | prompt
        | llm
    )

    return "PDF processed successfully! Now ask questions."

# Function to answer queries
def ask_question(query):
    if "rag_chain" not in globals():
        return "Please upload and process a PDF first."
    
    response = rag_chain.invoke(query).content
    return response

# Gradio UI
with gr.Blocks() as demo:
    gr.Markdown("# πŸ“„ PDF Chatbot with RAG")
    gr.Markdown("Upload a PDF and ask questions!")
    
    pdf_input = gr.File(label="Upload PDF", type="binary")
    process_button = gr.Button("Process PDF")
    output_message = gr.Textbox(label="Status", interactive=False)
    
    query_input = gr.Textbox(label="Ask a Question")
    submit_button = gr.Button("Submit")
    response_output = gr.Textbox(label="AI Response")

    process_button.click(process_pdf, inputs=pdf_input, outputs=output_message)
    submit_button.click(ask_question, inputs=query_input, outputs=response_output)
demo.launch()