Yatheshr commited on
Commit
70d33fc
Β·
verified Β·
1 Parent(s): 6aebdbe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -43
app.py CHANGED
@@ -9,93 +9,91 @@ from langchain.chains import RetrievalQA
9
 
10
  from pinecone import Pinecone, ServerlessSpec
11
 
12
- # Set a consistent index name
13
  INDEX_NAME = "rag-demo-index"
 
14
 
15
- def process_rag(api_key_gemini, api_key_pinecone, pinecone_region, pdf_file, user_question):
16
- if not api_key_gemini or not api_key_pinecone or not pinecone_region:
17
- return "❌ Please provide all required API keys and region."
 
 
 
 
 
18
 
19
  if not pdf_file:
20
- return "❌ Please upload a PDF file."
21
 
22
  try:
23
- # Step 1: Load and split PDF
24
  loader = PyPDFLoader(pdf_file.name)
25
  documents = loader.load()
26
 
27
- splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
28
- docs = splitter.split_documents(documents)
29
 
30
- # Step 2: Gemini embedding model
31
  embeddings = GoogleGenerativeAIEmbeddings(
32
  model="models/embedding-001",
33
- google_api_key=api_key_gemini
34
  )
35
 
36
- # Step 3: Init Pinecone client
37
- pc = Pinecone(api_key=api_key_pinecone)
38
 
39
- # Step 4: Create index if it doesn't exist
40
  if INDEX_NAME not in pc.list_indexes().names():
41
  pc.create_index(
42
  name=INDEX_NAME,
43
- dimension=768, # for Gemini embedding
44
  metric="cosine",
45
- spec=ServerlessSpec(cloud="aws", region=pinecone_region)
 
 
 
46
  )
47
 
48
- # Step 5: Store vectors using LangChain wrapper (not using pc.Index)
49
- vectordb = LangChainPinecone.from_documents(
50
- docs,
51
  embedding=embeddings,
52
- index_name=INDEX_NAME,
53
- api_key=api_key_pinecone,
54
- environment=pinecone_region
55
  )
56
 
57
- # Step 6: Create retriever
58
- retriever = vectordb.as_retriever()
59
 
60
- # Step 7: Load Gemini model for answering
61
  llm = ChatGoogleGenerativeAI(
62
  model="gemini-pro",
63
- google_api_key=api_key_gemini,
64
  temperature=0
65
  )
66
 
 
67
  qa_chain = RetrievalQA.from_chain_type(
68
  llm=llm,
69
  retriever=retriever,
70
  return_source_documents=False
71
  )
72
 
73
- # Step 8: Ask the question
74
  result = qa_chain({"query": user_question})
75
  return result["result"]
76
 
77
  except Exception as e:
78
  return f"❌ Error: {str(e)}"
79
 
80
- # Gradio Interface
81
- with gr.Blocks() as app:
82
- gr.Markdown("## πŸ“„πŸ” Ask Questions About Your PDF (Gemini + Pinecone RAG)")
83
 
84
  with gr.Row():
85
- gemini_key = gr.Textbox(label="πŸ” Gemini API Key", type="password", placeholder="Paste your Gemini API key")
86
- pinecone_key = gr.Textbox(label="🌲 Pinecone API Key", type="password", placeholder="Paste your Pinecone API key")
87
- pinecone_region = gr.Textbox(label="πŸ“ Pinecone Region", placeholder="e.g., us-east-1")
88
-
89
- pdf_file = gr.File(label="πŸ“„ Upload your PDF", file_types=[".pdf"])
90
- user_question = gr.Textbox(label="❓ Ask a Question")
91
- answer_output = gr.Textbox(label="πŸ€– Gemini Answer", lines=10)
92
 
93
- ask_button = gr.Button("πŸ” Ask")
94
 
95
- ask_button.click(
96
- fn=process_rag,
97
- inputs=[gemini_key, pinecone_key, pinecone_region, pdf_file, user_question],
98
- outputs=answer_output
99
- )
100
 
101
- app.launch()
 
9
 
10
  from pinecone import Pinecone, ServerlessSpec
11
 
12
+ # Constants
13
  INDEX_NAME = "rag-demo-index"
14
+ DIMENSION = 768 # Use 768 for Gemini embeddings
15
 
16
+ def process_rag(pdf_file, user_question):
17
+ # πŸ” Load from Hugging Face Secrets
18
+ pinecone_api_key = os.environ.get("PINECONE_API_KEY")
19
+ pinecone_env = os.environ.get("PINECONE_ENVIRONMENT")
20
+ google_api_key = os.environ.get("GOOGLE_API_KEY")
21
+
22
+ if not all([pinecone_api_key, pinecone_env, google_api_key]):
23
+ return "❌ Missing required secrets. Check PINECONE_API_KEY, PINECONE_ENVIRONMENT, or GOOGLE_API_KEY."
24
 
25
  if not pdf_file:
26
+ return "❌ Please upload a PDF file first."
27
 
28
  try:
29
+ # Step 1: Load PDF and chunk it
30
  loader = PyPDFLoader(pdf_file.name)
31
  documents = loader.load()
32
 
33
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
34
+ docs = text_splitter.split_documents(documents)
35
 
36
+ # Step 2: Embeddings via Gemini
37
  embeddings = GoogleGenerativeAIEmbeddings(
38
  model="models/embedding-001",
39
+ google_api_key=google_api_key
40
  )
41
 
42
+ # Step 3: Connect to Pinecone v3
43
+ pc = Pinecone(api_key=pinecone_api_key)
44
 
 
45
  if INDEX_NAME not in pc.list_indexes().names():
46
  pc.create_index(
47
  name=INDEX_NAME,
48
+ dimension=DIMENSION,
49
  metric="cosine",
50
+ spec=ServerlessSpec(
51
+ cloud="aws",
52
+ region=pinecone_env
53
+ )
54
  )
55
 
56
+ # Step 4: Store docs in Pinecone
57
+ vectorstore = LangChainPinecone.from_documents(
58
+ documents=docs,
59
  embedding=embeddings,
60
+ index_name=INDEX_NAME
 
 
61
  )
62
 
63
+ retriever = vectorstore.as_retriever()
 
64
 
65
+ # Step 5: Gemini chat model
66
  llm = ChatGoogleGenerativeAI(
67
  model="gemini-pro",
68
+ google_api_key=google_api_key,
69
  temperature=0
70
  )
71
 
72
+ # Step 6: RAG chain
73
  qa_chain = RetrievalQA.from_chain_type(
74
  llm=llm,
75
  retriever=retriever,
76
  return_source_documents=False
77
  )
78
 
 
79
  result = qa_chain({"query": user_question})
80
  return result["result"]
81
 
82
  except Exception as e:
83
  return f"❌ Error: {str(e)}"
84
 
85
+ # Gradio UI
86
+ with gr.Blocks() as demo:
87
+ gr.Markdown("## πŸ” Ask Questions from PDF using Gemini + Pinecone RAG")
88
 
89
  with gr.Row():
90
+ pdf_input = gr.File(label="πŸ“„ Upload PDF", file_types=[".pdf"])
91
+ question_input = gr.Textbox(label="❓ Ask your question")
92
+
93
+ answer_output = gr.Textbox(label="πŸ€– Gemini Answer", lines=8)
 
 
 
94
 
95
+ ask_button = gr.Button("πŸ”Ž Run RAG")
96
 
97
+ ask_button.click(fn=process_rag, inputs=[pdf_input, question_input], outputs=answer_output)
 
 
 
 
98
 
99
+ demo.launch()