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 8 |
+
from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace
|
| 9 |
+
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 10 |
+
from langchain_community.vectorstores import Chroma
|
| 11 |
+
from langchain_community.document_loaders import PyPDFLoader
|
| 12 |
+
from langchain.chains import RetrievalQA
|
| 13 |
+
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
# -------------------------
|
| 17 |
+
# LLM (Hugging Face Router API)
|
| 18 |
+
# -------------------------
|
| 19 |
+
|
| 20 |
+
repo_id = "Qwen/Qwen2.5-7B-Instruct"
|
| 21 |
+
|
| 22 |
+
llm_endpoint = HuggingFaceEndpoint(
|
| 23 |
+
repo_id=repo_id,
|
| 24 |
+
task="text-generation",
|
| 25 |
+
max_new_tokens=512,
|
| 26 |
+
temperature=0.1,
|
| 27 |
+
huggingfacehub_api_token=os.getenv("HF_TOKEN"),
|
| 28 |
+
provider="hf-inference", # forces router.huggingface.co
|
| 29 |
+
model_kwargs={"wait_for_model": True},
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
llm = ChatHuggingFace(llm=llm_endpoint)
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
# -------------------------
|
| 36 |
+
# Document loader
|
| 37 |
+
# -------------------------
|
| 38 |
+
|
| 39 |
+
def document_loader(file_path: str):
|
| 40 |
+
loader = PyPDFLoader(file_path)
|
| 41 |
+
return loader.load()
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
# -------------------------
|
| 45 |
+
# Text splitter
|
| 46 |
+
# -------------------------
|
| 47 |
+
|
| 48 |
+
def text_splitter(data):
|
| 49 |
+
splitter = RecursiveCharacterTextSplitter(
|
| 50 |
+
chunk_size=1000,
|
| 51 |
+
chunk_overlap=50,
|
| 52 |
+
length_function=len,
|
| 53 |
+
)
|
| 54 |
+
return splitter.split_documents(data)
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
# -------------------------
|
| 58 |
+
# Embedding model
|
| 59 |
+
# -------------------------
|
| 60 |
+
|
| 61 |
+
def embedding_model():
|
| 62 |
+
return HuggingFaceEmbeddings(
|
| 63 |
+
model_name="sentence-transformers/all-MiniLM-L6-v2"
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
# -------------------------
|
| 68 |
+
# Vector DB
|
| 69 |
+
# -------------------------
|
| 70 |
+
|
| 71 |
+
def vector_database(chunks):
|
| 72 |
+
chunks = [
|
| 73 |
+
c for c in chunks
|
| 74 |
+
if c.page_content and c.page_content.strip() and len(c.page_content.strip()) > 10
|
| 75 |
+
]
|
| 76 |
+
|
| 77 |
+
vectordb = Chroma.from_documents(
|
| 78 |
+
documents=chunks,
|
| 79 |
+
embedding=embedding_model(),
|
| 80 |
+
collection_name=f"pdf_chat_{uuid.uuid4().hex}",
|
| 81 |
+
)
|
| 82 |
+
return vectordb
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
# -------------------------
|
| 86 |
+
# Retriever
|
| 87 |
+
# -------------------------
|
| 88 |
+
|
| 89 |
+
def build_retriever(file_path: str):
|
| 90 |
+
docs = document_loader(file_path)
|
| 91 |
+
chunks = text_splitter(docs)
|
| 92 |
+
vectordb = vector_database(chunks)
|
| 93 |
+
return vectordb.as_retriever()
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
# -------------------------
|
| 97 |
+
# QA Chain
|
| 98 |
+
# -------------------------
|
| 99 |
+
|
| 100 |
+
def retriever_qa(file, query):
|
| 101 |
+
retr = build_retriever(file)
|
| 102 |
+
|
| 103 |
+
qa = RetrievalQA.from_chain_type(
|
| 104 |
+
llm=llm,
|
| 105 |
+
chain_type="stuff",
|
| 106 |
+
retriever=retr,
|
| 107 |
+
return_source_documents=False,
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
result = qa.invoke({"query": query})
|
| 111 |
+
return result["result"]
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
# -------------------------
|
| 115 |
+
# Gradio interface
|
| 116 |
+
# -------------------------
|
| 117 |
+
|
| 118 |
+
rag_application = gr.Interface(
|
| 119 |
+
fn=retriever_qa,
|
| 120 |
+
allow_flagging="never",
|
| 121 |
+
inputs=[
|
| 122 |
+
gr.File(
|
| 123 |
+
label="Upload PDF File",
|
| 124 |
+
file_count="single",
|
| 125 |
+
file_types=[".pdf"],
|
| 126 |
+
type="filepath",
|
| 127 |
+
),
|
| 128 |
+
gr.Textbox(
|
| 129 |
+
label="Input Query",
|
| 130 |
+
lines=2,
|
| 131 |
+
placeholder="Type your question here...",
|
| 132 |
+
),
|
| 133 |
+
],
|
| 134 |
+
outputs=gr.Textbox(label="Output"),
|
| 135 |
+
title="PDF QA Chatbot",
|
| 136 |
+
description=(
|
| 137 |
+
"Upload a PDF document and ask any question. "
|
| 138 |
+
"The chatbot will try to answer using the provided document."
|
| 139 |
+
),
|
| 140 |
+
)
|
| 141 |
+
|
| 142 |
+
if __name__ == "__main__":
|
| 143 |
+
rag_application.launch()
|