Yatheshr commited on
Commit
4b9e739
Β·
verified Β·
1 Parent(s): 236f327

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -23
app.py CHANGED
@@ -7,48 +7,57 @@ from langchain_community.embeddings import HuggingFaceEmbeddings
7
  from langchain_community.vectorstores import FAISS
8
  import google.generativeai as genai
9
 
10
- # Folder where FAISS index will be saved
11
  INDEX_DIR = "rag_multi_pdf_index"
12
 
13
- # Build knowledge base from multiple PDFs
14
  def create_knowledge_base(pdf_files: List[gr.File]) -> str:
 
 
 
15
  all_chunks = []
16
  splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=100)
17
 
18
  for file in pdf_files:
19
  loader = PyPDFLoader(file.name)
20
- docs = loader.load()
21
- chunks = splitter.split_documents(docs)
22
- all_chunks.extend(chunks)
 
 
 
 
 
 
23
 
24
  embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
25
  vectorstore = FAISS.from_documents(all_chunks, embeddings)
26
  vectorstore.save_local(INDEX_DIR)
27
 
28
- return f"βœ… Knowledge base created from {len(pdf_files)} PDFs."
29
 
30
- # Load existing vector index
31
  def load_vectorstore() -> FAISS:
32
  embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
33
  return FAISS.load_local(INDEX_DIR, embeddings)
34
 
35
- # Ask a question using Gemini + vector search
36
  def chat_with_rag(api_key: str, question: str) -> str:
37
  if not api_key or not api_key.startswith("AI"):
38
- return "❌ Invalid Gemini API Key. Please enter a valid key starting with 'AI'."
39
 
40
  try:
41
  genai.configure(api_key=api_key)
42
  model = genai.GenerativeModel("gemini-pro")
43
  except Exception as e:
44
- return f"❌ Failed to configure Gemini: {str(e)}"
45
 
46
  try:
47
  vs = load_vectorstore()
48
  top_docs = vs.similarity_search(question, k=3)
49
  context = "\n\n".join([doc.page_content for doc in top_docs])
50
  except Exception as e:
51
- return f"❌ Error retrieving context: {str(e)}"
52
 
53
  prompt = f"""Use the following context to answer the question:\n\n{context}\n\nQuestion: {question}"""
54
 
@@ -56,26 +65,26 @@ def chat_with_rag(api_key: str, question: str) -> str:
56
  response = model.generate_content(prompt)
57
  return response.text
58
  except Exception as e:
59
- return f"❌ Gemini Error: {str(e)}"
 
 
 
 
60
 
61
- # Gradio UI
62
- with gr.Blocks(title="πŸ“š RAG Chat with Gemini (Multi-PDF)") as demo:
63
- gr.Markdown("## 🧠 Upload Multiple PDFs & Chat Using Gemini")
64
 
65
- api_key = gr.Textbox(label="πŸ” Gemini API Key", placeholder="Paste your API key here", type="password")
66
-
67
- with gr.Row():
68
- pdfs = gr.File(label="πŸ“‚ Upload PDFs", file_types=[".pdf"], file_count="multiple")
69
- create_btn = gr.Button("πŸ“„ Create Knowledge Base")
70
- kb_status = gr.Textbox(label="πŸ“¦ Knowledge Base Status", interactive=False)
71
 
72
  create_btn.click(fn=create_knowledge_base, inputs=[pdfs], outputs=[kb_status])
73
 
74
- question = gr.Textbox(label="❓ Your Question", placeholder="Ask something based on your PDFs")
75
- answer = gr.Textbox(label="πŸ’¬ Gemini Answer", lines=10)
76
  ask_btn = gr.Button("πŸš€ Ask")
77
 
78
  ask_btn.click(fn=chat_with_rag, inputs=[api_key, question], outputs=[answer])
79
 
 
80
  if __name__ == "__main__":
81
  demo.launch()
 
7
  from langchain_community.vectorstores import FAISS
8
  import google.generativeai as genai
9
 
10
+ # Path to save vector index
11
  INDEX_DIR = "rag_multi_pdf_index"
12
 
13
+ # Step 1: Create knowledge base from PDFs
14
  def create_knowledge_base(pdf_files: List[gr.File]) -> str:
15
+ if not pdf_files:
16
+ return "❌ No PDFs uploaded."
17
+
18
  all_chunks = []
19
  splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=100)
20
 
21
  for file in pdf_files:
22
  loader = PyPDFLoader(file.name)
23
+ try:
24
+ docs = loader.load()
25
+ chunks = splitter.split_documents(docs)
26
+ all_chunks.extend(chunks)
27
+ except Exception as e:
28
+ return f"❌ Error reading {file.name}: {str(e)}"
29
+
30
+ if not all_chunks:
31
+ return "❌ No content extracted from PDFs."
32
 
33
  embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
34
  vectorstore = FAISS.from_documents(all_chunks, embeddings)
35
  vectorstore.save_local(INDEX_DIR)
36
 
37
+ return f"βœ… Knowledge base created with {len(all_chunks)} chunks from {len(pdf_files)} PDFs."
38
 
39
+ # Step 2: Load vectorstore
40
  def load_vectorstore() -> FAISS:
41
  embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
42
  return FAISS.load_local(INDEX_DIR, embeddings)
43
 
44
+ # Step 3: Ask question via Gemini using retrieved context
45
  def chat_with_rag(api_key: str, question: str) -> str:
46
  if not api_key or not api_key.startswith("AI"):
47
+ return "❌ Invalid Gemini API Key. It should start with 'AI'."
48
 
49
  try:
50
  genai.configure(api_key=api_key)
51
  model = genai.GenerativeModel("gemini-pro")
52
  except Exception as e:
53
+ return f"❌ Gemini configuration error: {str(e)}"
54
 
55
  try:
56
  vs = load_vectorstore()
57
  top_docs = vs.similarity_search(question, k=3)
58
  context = "\n\n".join([doc.page_content for doc in top_docs])
59
  except Exception as e:
60
+ return f"❌ Error loading vectorstore or retrieving context: {str(e)}"
61
 
62
  prompt = f"""Use the following context to answer the question:\n\n{context}\n\nQuestion: {question}"""
63
 
 
65
  response = model.generate_content(prompt)
66
  return response.text
67
  except Exception as e:
68
+ return f"❌ Gemini error: {str(e)}"
69
+
70
+ # Step 4: Gradio UI
71
+ with gr.Blocks(title="πŸ“š RAG Q&A with Gemini") as demo:
72
+ gr.Markdown("## πŸ“„ Upload multiple PDFs β†’ 🧠 Build Knowledge Base β†’ πŸ€– Ask Questions with Gemini")
73
 
74
+ api_key = gr.Textbox(label="πŸ” Gemini API Key", placeholder="Enter your Gemini API Key", type="password")
 
 
75
 
76
+ pdfs = gr.File(label="πŸ“‚ Upload PDFs", file_types=[".pdf"], file_count="multiple")
77
+ create_btn = gr.Button("πŸ“„ Create Knowledge Base")
78
+ kb_status = gr.Textbox(label="πŸ“¦ Knowledge Base Status", interactive=False)
 
 
 
79
 
80
  create_btn.click(fn=create_knowledge_base, inputs=[pdfs], outputs=[kb_status])
81
 
82
+ question = gr.Textbox(label="❓ Ask a Question")
83
+ answer = gr.Textbox(label="πŸ’¬ Gemini Answer", lines=10, interactive=False)
84
  ask_btn = gr.Button("πŸš€ Ask")
85
 
86
  ask_btn.click(fn=chat_with_rag, inputs=[api_key, question], outputs=[answer])
87
 
88
+ # Step 5: Launch app
89
  if __name__ == "__main__":
90
  demo.launch()