prithvi1029 commited on
Commit
b98f9c6
·
verified ·
1 Parent(s): eefcb7c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -28
app.py CHANGED
@@ -4,56 +4,54 @@ from langchain_community.document_loaders import PyPDFLoader
4
  from langchain_text_splitters import RecursiveCharacterTextSplitter
5
  from langchain_community.embeddings import HuggingFaceEmbeddings
6
  from langchain_community.vectorstores import FAISS
7
-
8
  from langchain_openai import ChatOpenAI
9
- from langchain_core.prompts import ChatPromptTemplate
10
- from langchain.chains.combine_documents import create_stuff_documents_chain
11
- from langchain.chains import create_retrieval_chain
12
 
13
 
14
- def build_chain(pdf_path: str):
 
 
 
 
15
  loader = PyPDFLoader(pdf_path)
16
  docs = loader.load()
17
 
 
18
  splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=150)
19
  chunks = splitter.split_documents(docs)
20
 
 
21
  embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
22
  vectordb = FAISS.from_documents(chunks, embeddings)
 
 
23
  retriever = vectordb.as_retriever(search_kwargs={"k": 4})
 
 
 
24
 
 
25
  llm = ChatOpenAI(temperature=0)
26
 
27
- prompt = ChatPromptTemplate.from_template(
28
- """You are a helpful assistant. Answer the question using ONLY the provided context.
29
- If the answer is not in the context, say you don't know.
30
 
31
- <context>
32
  {context}
33
- </context>
34
 
35
- Question: {input}
36
- """
37
- )
38
-
39
- doc_chain = create_stuff_documents_chain(llm, prompt)
40
- retrieval_chain = create_retrieval_chain(retriever, doc_chain)
41
- return retrieval_chain
42
-
43
-
44
- def run_qa(pdf_path, question):
45
- if pdf_path is None or question.strip() == "":
46
- return "Please upload a PDF and enter a question."
47
 
48
- chain = build_chain(pdf_path)
49
- result = chain.invoke({"input": question})
50
 
51
- answer_text = result.get("answer", "")
52
- ctx_docs = result.get("context", []) or []
53
 
54
- sources = "\n\n".join([d.page_content[:500] for d in ctx_docs[:2]])
 
55
 
56
- return f"### Answer\n{answer_text}\n\n---\n### Sources\n{sources}"
57
 
58
 
59
  with gr.Blocks(title="Agentic Document Intelligence") as demo:
 
4
  from langchain_text_splitters import RecursiveCharacterTextSplitter
5
  from langchain_community.embeddings import HuggingFaceEmbeddings
6
  from langchain_community.vectorstores import FAISS
 
7
  from langchain_openai import ChatOpenAI
 
 
 
8
 
9
 
10
+ def run_qa(pdf_path, question):
11
+ if pdf_path is None or not question or question.strip() == "":
12
+ return "Please upload a PDF and enter a question."
13
+
14
+ # 1) Load PDF
15
  loader = PyPDFLoader(pdf_path)
16
  docs = loader.load()
17
 
18
+ # 2) Split
19
  splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=150)
20
  chunks = splitter.split_documents(docs)
21
 
22
+ # 3) Embed + Vector store
23
  embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
24
  vectordb = FAISS.from_documents(chunks, embeddings)
25
+
26
+ # 4) Retrieve relevant chunks
27
  retriever = vectordb.as_retriever(search_kwargs={"k": 4})
28
+ retrieved_docs = retriever.get_relevant_documents(question)
29
+
30
+ context = "\n\n".join([d.page_content for d in retrieved_docs])
31
 
32
+ # 5) LLM (OpenAI)
33
  llm = ChatOpenAI(temperature=0)
34
 
35
+ prompt = f"""
36
+ You are a helpful assistant. Answer the question using ONLY the context below.
37
+ If the answer is not in the context, say "I don't know".
38
 
39
+ CONTEXT:
40
  {context}
 
41
 
42
+ QUESTION:
43
+ {question}
 
 
 
 
 
 
 
 
 
 
44
 
45
+ Answer:
46
+ """.strip()
47
 
48
+ response = llm.invoke(prompt)
49
+ answer = response.content if hasattr(response, "content") else str(response)
50
 
51
+ # 6) Sources preview
52
+ sources = "\n\n".join([d.page_content[:500] for d in retrieved_docs[:2]])
53
 
54
+ return f"### Answer\n{answer}\n\n---\n### Sources\n{sources}"
55
 
56
 
57
  with gr.Blocks(title="Agentic Document Intelligence") as demo: