Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,57 +4,48 @@ import gradio as gr
|
|
| 4 |
from langchain_community.document_loaders import PyPDFLoader
|
| 5 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 6 |
from langchain_google_genai import GoogleGenerativeAIEmbeddings, ChatGoogleGenerativeAI
|
| 7 |
-
from langchain_community.vectorstores import Pinecone
|
| 8 |
from langchain.chains import RetrievalQA
|
| 9 |
|
| 10 |
-
|
| 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 |
-
#
|
| 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
|
| 24 |
|
| 25 |
if not pdf_file:
|
| 26 |
-
return "β Please upload a PDF file
|
| 27 |
|
| 28 |
try:
|
| 29 |
-
#
|
| 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 |
-
#
|
| 37 |
embeddings = GoogleGenerativeAIEmbeddings(
|
| 38 |
model="models/embedding-001",
|
| 39 |
google_api_key=google_api_key
|
| 40 |
)
|
| 41 |
|
| 42 |
-
#
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
if INDEX_NAME not in
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 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
|
|
@@ -62,20 +53,21 @@ def process_rag(pdf_file, user_question):
|
|
| 62 |
|
| 63 |
retriever = vectorstore.as_retriever()
|
| 64 |
|
| 65 |
-
#
|
| 66 |
llm = ChatGoogleGenerativeAI(
|
| 67 |
model="gemini-pro",
|
| 68 |
google_api_key=google_api_key,
|
| 69 |
temperature=0
|
| 70 |
)
|
| 71 |
|
| 72 |
-
#
|
| 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 |
|
|
@@ -84,7 +76,7 @@ def process_rag(pdf_file, user_question):
|
|
| 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"])
|
|
|
|
| 4 |
from langchain_community.document_loaders import PyPDFLoader
|
| 5 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 6 |
from langchain_google_genai import GoogleGenerativeAIEmbeddings, ChatGoogleGenerativeAI
|
| 7 |
+
from langchain_community.vectorstores import Pinecone
|
| 8 |
from langchain.chains import RetrievalQA
|
| 9 |
|
| 10 |
+
import pinecone
|
| 11 |
|
| 12 |
# Constants
|
| 13 |
INDEX_NAME = "rag-demo-index"
|
|
|
|
| 14 |
|
| 15 |
def process_rag(pdf_file, user_question):
|
| 16 |
+
# Load secrets from Hugging Face (or env manually)
|
| 17 |
pinecone_api_key = os.environ.get("PINECONE_API_KEY")
|
| 18 |
+
pinecone_env = os.environ.get("PINECONE_ENVIRONMENT") # Example: "gcp-starter"
|
| 19 |
google_api_key = os.environ.get("GOOGLE_API_KEY")
|
| 20 |
|
| 21 |
if not all([pinecone_api_key, pinecone_env, google_api_key]):
|
| 22 |
+
return "β Missing API key(s). Please check Pinecone & Google Gemini keys."
|
| 23 |
|
| 24 |
if not pdf_file:
|
| 25 |
+
return "β Please upload a PDF file."
|
| 26 |
|
| 27 |
try:
|
| 28 |
+
# 1. Load and split PDF
|
| 29 |
loader = PyPDFLoader(pdf_file.name)
|
| 30 |
documents = loader.load()
|
| 31 |
|
| 32 |
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
| 33 |
docs = text_splitter.split_documents(documents)
|
| 34 |
|
| 35 |
+
# 2. Gemini Embeddings
|
| 36 |
embeddings = GoogleGenerativeAIEmbeddings(
|
| 37 |
model="models/embedding-001",
|
| 38 |
google_api_key=google_api_key
|
| 39 |
)
|
| 40 |
|
| 41 |
+
# 3. Init Pinecone (old client)
|
| 42 |
+
pinecone.init(api_key=pinecone_api_key, environment=pinecone_env)
|
| 43 |
+
|
| 44 |
+
if INDEX_NAME not in pinecone.list_indexes():
|
| 45 |
+
pinecone.create_index(name=INDEX_NAME, dimension=768, metric="cosine")
|
| 46 |
+
|
| 47 |
+
# 4. Create LangChain-compatible Vector DB
|
| 48 |
+
vectorstore = Pinecone.from_documents(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
documents=docs,
|
| 50 |
embedding=embeddings,
|
| 51 |
index_name=INDEX_NAME
|
|
|
|
| 53 |
|
| 54 |
retriever = vectorstore.as_retriever()
|
| 55 |
|
| 56 |
+
# 5. Gemini LLM
|
| 57 |
llm = ChatGoogleGenerativeAI(
|
| 58 |
model="gemini-pro",
|
| 59 |
google_api_key=google_api_key,
|
| 60 |
temperature=0
|
| 61 |
)
|
| 62 |
|
| 63 |
+
# 6. Retrieval QA chain
|
| 64 |
qa_chain = RetrievalQA.from_chain_type(
|
| 65 |
llm=llm,
|
| 66 |
retriever=retriever,
|
| 67 |
return_source_documents=False
|
| 68 |
)
|
| 69 |
|
| 70 |
+
# 7. Ask question
|
| 71 |
result = qa_chain({"query": user_question})
|
| 72 |
return result["result"]
|
| 73 |
|
|
|
|
| 76 |
|
| 77 |
# Gradio UI
|
| 78 |
with gr.Blocks() as demo:
|
| 79 |
+
gr.Markdown("## π Ask Questions from PDF using Gemini + Pinecone (LangChain RAG)")
|
| 80 |
|
| 81 |
with gr.Row():
|
| 82 |
pdf_input = gr.File(label="π Upload PDF", file_types=[".pdf"])
|