Spaces:
Build error
Build error
File size: 3,425 Bytes
ecfda12 f8999d7 bfb8470 5fd7b91 f8999d7 5061d0f bfb8470 314bb9f 5061d0f 314bb9f bfb8470 314bb9f 5061d0f 314bb9f 5061d0f f8999d7 bfb8470 b23dedc bfb8470 f8999d7 bfb8470 5061d0f bfb8470 5061d0f bfb8470 5061d0f bfb8470 5061d0f f8999d7 5061d0f | 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 | import os
from dotenv import load_dotenv
import gradio as gr
from langchain_core.document_loaders import BaseLoader
from langchain_core.documents import Document as LCDocument
from docling.document_converter import DocumentConverter
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_huggingface.embeddings import HuggingFaceEmbeddings
from langchain_milvus import Milvus
from langchain_huggingface import HuggingFaceEndpoint
from langchain_core.prompts import PromptTemplate
from langchain_core.runnables import RunnablePassthrough
from langchain_core.output_parsers import StrOutputParser
from tempfile import TemporaryDirectory
from typing import Iterator
# Load environment variables
load_dotenv()
HF_API_KEY = os.environ.get("HF_API_KEY")
# Constants
FILE_PATH = "10_Pages_Vol_5.pdf" # Your hardcoded PDF path
HF_EMBED_MODEL_ID = "BAAI/bge-small-en-v1.5"
HF_LLM_MODEL_ID = "mistralai/Mistral-7B-Instruct-v0.3"
class DoclingPDFLoader(BaseLoader):
def __init__(self, file_path: str | list[str]) -> None:
self._file_paths = file_path if isinstance(file_path, list) else [file_path]
self._converter = DocumentConverter()
def lazy_load(self) -> Iterator[LCDocument]:
for source in self._file_paths:
dl_doc = self._converter.convert(source).document
text = dl_doc.export_to_markdown()
yield LCDocument(page_content=text)
def format_docs(docs):
return "\n\n".join(doc.page_content for doc in docs)
# Setup the RAG pipeline
loader = DoclingPDFLoader(file_path=FILE_PATH)
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=2500,
chunk_overlap=500,
)
docs = loader.load()
splits = text_splitter.split_documents(docs)
embeddings = HuggingFaceEmbeddings(model_name=HF_EMBED_MODEL_ID)
# Setup Milvus
tmp_dir = TemporaryDirectory()
MILVUS_URI = f"{tmp_dir.name}/milvus_demo.db"
vectorstore = Milvus.from_documents(
splits,
embeddings,
connection_args={"uri": MILVUS_URI},
drop_old=True,
index_params={"index_type": "IVF_FLAT", "metric_type": "L2"},
)
# Setup LLM
llm = HuggingFaceEndpoint(
repo_id=HF_LLM_MODEL_ID,
huggingfacehub_api_token=HF_API_KEY,
task="text-generation",
)
# Setup RAG chain
retriever = vectorstore.as_retriever()
prompt = PromptTemplate.from_template(
"Context information is below.\n---------------------\n{context}\n---------------------\nUse the context of the work you have been currently trained on, not your prior knowledge, to answer the queries asked. Please use Chapter numbers and page numbers as references as well.\nQuery: {question}\nAnswer:\n"
)
rag_chain = (
{"context": retriever | format_docs, "question": RunnablePassthrough()}
| prompt
| llm
| StrOutputParser()
)
def process_query(query):
try:
response = rag_chain.invoke(query)
return response
except Exception as e:
return f"An error occurred: {str(e)}"
# Create Gradio interface
demo = gr.Interface(
fn=process_query,
inputs=gr.Textbox(label="Enter your question about the document"),
outputs=gr.Textbox(label="Answer"),
title="Document Q&A System",
description=f"Ask questions about {FILE_PATH}",
examples=[
["Who are the members of the Sanhedrin who are present?"],
["What are the main themes discussed in the document?"]
]
)
if __name__ == "__main__":
demo.launch() |