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()