Amithsc2026 commited on
Commit
d68acf3
·
verified ·
1 Parent(s): 22033d3

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +106 -0
app.py ADDED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ from langchain_community.document_loaders import PyPDFLoader
4
+ from langchain_community.vectorstores import Chroma
5
+ from langchain_text_splitters import RecursiveCharacterTextSplitter
6
+ from langchain_ollama import OllamaEmbeddings, OllamaLLM
7
+
8
+ # -------------------------
9
+ # 1. LOAD MULTIPLE PDFs
10
+ # -------------------------
11
+
12
+ def load_documents(folder_path="documents"):
13
+ documents = []
14
+ for file in os.listdir(folder_path):
15
+ if file.endswith(".pdf"):
16
+ loader = PyPDFLoader(os.path.join(folder_path, file))
17
+ docs = loader.load()
18
+ for doc in docs:
19
+ doc.metadata["source"] = file
20
+ documents.extend(docs)
21
+ return documents
22
+
23
+ # -------------------------
24
+ # 2. BUILD VECTOR DATABASE
25
+ # -------------------------
26
+
27
+ documents = load_documents("documents")
28
+
29
+ text_splitter = RecursiveCharacterTextSplitter(
30
+ chunk_size=1000,
31
+ chunk_overlap=200
32
+ )
33
+
34
+ docs = text_splitter.split_documents(documents)
35
+
36
+ embeddings = OllamaEmbeddings(model="mistral")
37
+ db = Chroma.from_documents(docs, embeddings)
38
+
39
+ llm = OllamaLLM(model="mistral")
40
+
41
+ # -------------------------
42
+ # 3. CHAT MEMORY
43
+ # -------------------------
44
+
45
+ chat_history = []
46
+
47
+ # -------------------------
48
+ # 4. QUESTION FUNCTION
49
+ # -------------------------
50
+
51
+ def ask_pdf(question):
52
+
53
+ global chat_history
54
+
55
+ retrieved_docs = db.similarity_search(question, k=3)
56
+
57
+ context = "\n\n".join(
58
+ [doc.page_content for doc in retrieved_docs]
59
+ )
60
+
61
+ sources = list(set([doc.metadata["source"] for doc in retrieved_docs]))
62
+
63
+ # Guardrail: if no context found
64
+ if not context.strip():
65
+ return "I don't have enough information from the documents to answer this."
66
+
67
+ prompt = f"""
68
+ You are a helpful assistant.
69
+ Answer ONLY from the provided context.
70
+ If the answer is not in the context, say:
71
+ "I don't have enough information from the documents."
72
+
73
+ Chat history:
74
+ {chat_history}
75
+
76
+ Context:
77
+ {context}
78
+
79
+ Question:
80
+ {question}
81
+
82
+ Answer:
83
+ """
84
+
85
+ answer = llm.invoke(prompt)
86
+
87
+ chat_history.append({"question": question, "answer": answer})
88
+
89
+ citation_text = "\n\nSources: " + ", ".join(sources)
90
+
91
+ return answer + citation_text
92
+
93
+
94
+ # -------------------------
95
+ # 5. GRADIO UI
96
+ # -------------------------
97
+
98
+ interface = gr.Interface(
99
+ fn=ask_pdf,
100
+ inputs="text",
101
+ outputs="text",
102
+ title="Advanced Ask My PDF Bot",
103
+ description="Chat with multiple PDFs. Shows sources. Has memory. Guardrails enabled."
104
+ )
105
+
106
+ interface.launch()