Spaces:
Build error
Build error
Update app.py
Browse files3 unmodified lines45678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758593 unmodified linesfrom langchain_text_splitters import RecursiveCharacterTextSplitterfrom langchain_community.embeddings import HuggingFaceEmbeddingsfrom langchain_community.vectorstores import FAISSfrom langchain_openai import ChatOpenAIfrom langchain_core.prompts import ChatPromptTemplatefrom langchain.chains.combine_documents import create_stuff_documents_chainfrom langchain.chains import create_retrieval_chaindef build_chain(pdf_path: str):loader = PyPDFLoader(pdf_path)docs = loader.load()splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=150)chunks = splitter.split_documents(docs)embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")vectordb = FAISS.from_documents(chunks, embeddings)retriever = vectordb.as_retriever(search_kwargs={"k": 4})llm = ChatOpenAI(temperature=0)prompt = ChatPromptTemplate.from_template("""You are a helpful assistant. Answer the question using ONLY the provided context.If the answer is not in the context, say you don't know.<context>{context}</context>Question: {input}""")doc_chain = create_stuff_documents_chain(llm, prompt)retrieval_chain = create_retrieval_chain(retriever, doc_chain)return retrieval_chaindef run_qa(pdf_path, question):if pdf_path is None or question.strip() == "":return "Please upload a PDF and enter a question."chain = build_chain(pdf_path)result = chain.invoke({"input": question})answer_text = result.get("answer", "")ctx_docs = result.get("context", []) or []sources = "\n\n".join([d.page_content[:500] for d in ctx_docs[:2]])return f"### Answer\n{answer_text}\n\n---\n### Sources\n{sources}"with gr.Blocks(title="Agentic Document Intelligence") as demo:3 unmodified lines4567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556573 unmodified linesfrom langchain_text_splitters import RecursiveCharacterTextSplitterfrom langchain_community.embeddings import HuggingFaceEmbeddingsfrom langchain_community.vectorstores import FAISSfrom langchain_openai import ChatOpenAIdef run_qa(pdf_path, question):if pdf_path is None or not question or question.strip() == "":return "Please upload a PDF and enter a question."# 1) Load PDFloader = PyPDFLoader(pdf_path)docs = loader.load()# 2) Splitsplitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=150)chunks = splitter.split_documents(docs)# 3) Embed + Vector storeembeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")vectordb = FAISS.from_documents(chunks, embeddings)# 4) Retrieve relevant chunksretriever = vectordb.as_retriever(search_kwargs={"k": 4})retrieved_docs = retriever.get_relevant_documents(question)context = "\n\n".join([d.page_content for d in retrieved_docs])# 5) LLM (OpenAI)llm = ChatOpenAI(temperature=0)prompt = f"""You are a helpful assistant. Answer the question using ONLY the context below.If the answer is not in the context, say "I don't know".CONTEXT:{context}QUESTION:{question}Answer:""".strip()response = llm.invoke(prompt)answer = response.content if hasattr(response, "content") else str(response)# 6) Sources previewsources = "\n\n".join([d.page_content[:500] for d in retrieved_docs[:2]])return f"### Answer\n{answer}\n\n---\n### Sources\n{sources}"with gr.Blocks(title="Agentic Document Intelligence") as demo:
app.py+26-28