Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,11 +3,11 @@ import os
|
|
| 3 |
from langchain_community.document_loaders import PyPDFLoader
|
| 4 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 5 |
from langchain_google_genai import GoogleGenerativeAIEmbeddings, ChatGoogleGenerativeAI
|
| 6 |
-
from langchain_community.vectorstores import Pinecone
|
| 7 |
from langchain.chains import RetrievalQA
|
| 8 |
-
|
| 9 |
|
| 10 |
-
# Global
|
| 11 |
INDEX_NAME = "rag-demo-index"
|
| 12 |
|
| 13 |
def process_rag(api_key_gemini, api_key_pinecone, pinecone_env, pdf_file, user_question):
|
|
@@ -18,46 +18,72 @@ def process_rag(api_key_gemini, api_key_pinecone, pinecone_env, pdf_file, user_q
|
|
| 18 |
return "β Please upload a PDF file."
|
| 19 |
|
| 20 |
try:
|
| 21 |
-
# Step 1: Load and
|
| 22 |
loader = PyPDFLoader(pdf_file.name)
|
| 23 |
documents = loader.load()
|
| 24 |
splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
| 25 |
docs = splitter.split_documents(documents)
|
| 26 |
|
| 27 |
-
# Step 2:
|
| 28 |
-
embeddings = GoogleGenerativeAIEmbeddings(
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
#
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
# Step 5: Create retriever
|
| 40 |
retriever = vectordb.as_retriever()
|
| 41 |
|
| 42 |
-
# Step 6: Use Gemini
|
| 43 |
-
llm = ChatGoogleGenerativeAI(
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
result = qa_chain({"query": user_question})
|
| 48 |
return result["result"]
|
| 49 |
|
| 50 |
except Exception as e:
|
| 51 |
return f"β Error: {str(e)}"
|
| 52 |
|
| 53 |
-
# Gradio
|
| 54 |
with gr.Blocks() as app:
|
| 55 |
gr.Markdown("## ππ PDF Question Answering using Pinecone + Gemini (RAG)")
|
| 56 |
-
|
| 57 |
with gr.Row():
|
| 58 |
gemini_key = gr.Textbox(label="π Gemini API Key", type="password")
|
| 59 |
pinecone_key = gr.Textbox(label="π² Pinecone API Key", type="password")
|
| 60 |
-
pinecone_env = gr.Textbox(label="π Pinecone
|
| 61 |
|
| 62 |
pdf_file = gr.File(label="π Upload your PDF", file_types=[".pdf"])
|
| 63 |
user_question = gr.Textbox(label="β Ask your question")
|
|
|
|
| 3 |
from langchain_community.document_loaders import PyPDFLoader
|
| 4 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 5 |
from langchain_google_genai import GoogleGenerativeAIEmbeddings, ChatGoogleGenerativeAI
|
| 6 |
+
from langchain_community.vectorstores import Pinecone as LangchainPinecone
|
| 7 |
from langchain.chains import RetrievalQA
|
| 8 |
+
from pinecone import Pinecone, ServerlessSpec
|
| 9 |
|
| 10 |
+
# Global constants
|
| 11 |
INDEX_NAME = "rag-demo-index"
|
| 12 |
|
| 13 |
def process_rag(api_key_gemini, api_key_pinecone, pinecone_env, pdf_file, user_question):
|
|
|
|
| 18 |
return "β Please upload a PDF file."
|
| 19 |
|
| 20 |
try:
|
| 21 |
+
# Step 1: Load and split PDF
|
| 22 |
loader = PyPDFLoader(pdf_file.name)
|
| 23 |
documents = loader.load()
|
| 24 |
splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
| 25 |
docs = splitter.split_documents(documents)
|
| 26 |
|
| 27 |
+
# Step 2: Gemini Embeddings
|
| 28 |
+
embeddings = GoogleGenerativeAIEmbeddings(
|
| 29 |
+
model="models/embedding-001",
|
| 30 |
+
google_api_key=api_key_gemini
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
# Step 3: Pinecone setup (new SDK)
|
| 34 |
+
pc = Pinecone(api_key=api_key_pinecone)
|
| 35 |
+
|
| 36 |
+
# Create index if it doesn't exist
|
| 37 |
+
if INDEX_NAME not in pc.list_indexes().names():
|
| 38 |
+
pc.create_index(
|
| 39 |
+
name=INDEX_NAME,
|
| 40 |
+
dimension=768,
|
| 41 |
+
metric="cosine",
|
| 42 |
+
spec=ServerlessSpec(
|
| 43 |
+
cloud="aws", # or "gcp"
|
| 44 |
+
region=pinecone_env # example: "us-east-1"
|
| 45 |
+
)
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
index = pc.Index(INDEX_NAME)
|
| 49 |
+
|
| 50 |
+
# Step 4: Store documents in Pinecone
|
| 51 |
+
vectordb = LangchainPinecone.from_documents(
|
| 52 |
+
docs,
|
| 53 |
+
embedding=embeddings,
|
| 54 |
+
index=index
|
| 55 |
+
)
|
| 56 |
|
| 57 |
# Step 5: Create retriever
|
| 58 |
retriever = vectordb.as_retriever()
|
| 59 |
|
| 60 |
+
# Step 6: Use Gemini LLM
|
| 61 |
+
llm = ChatGoogleGenerativeAI(
|
| 62 |
+
model="gemini-pro",
|
| 63 |
+
google_api_key=api_key_gemini,
|
| 64 |
+
temperature=0
|
| 65 |
+
)
|
| 66 |
+
qa_chain = RetrievalQA.from_chain_type(
|
| 67 |
+
llm=llm,
|
| 68 |
+
retriever=retriever,
|
| 69 |
+
return_source_documents=True
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
# Step 7: Ask the question
|
| 73 |
result = qa_chain({"query": user_question})
|
| 74 |
return result["result"]
|
| 75 |
|
| 76 |
except Exception as e:
|
| 77 |
return f"β Error: {str(e)}"
|
| 78 |
|
| 79 |
+
# Gradio interface
|
| 80 |
with gr.Blocks() as app:
|
| 81 |
gr.Markdown("## ππ PDF Question Answering using Pinecone + Gemini (RAG)")
|
| 82 |
+
|
| 83 |
with gr.Row():
|
| 84 |
gemini_key = gr.Textbox(label="π Gemini API Key", type="password")
|
| 85 |
pinecone_key = gr.Textbox(label="π² Pinecone API Key", type="password")
|
| 86 |
+
pinecone_env = gr.Textbox(label="π Pinecone Region (e.g., us-east-1)", value="us-east-1")
|
| 87 |
|
| 88 |
pdf_file = gr.File(label="π Upload your PDF", file_types=[".pdf"])
|
| 89 |
user_question = gr.Textbox(label="β Ask your question")
|